PaintCode不适用于Storyboard

时间:2013-09-19 17:17:03

标签: ios iphone objective-c paint-code

我买了Paintcode,我尝试了这个例子this example

如果我使用笔尖,但是当我使用故事板时,它不起作用(它不会绘制自定义按钮)。我知道它需要经过很多代码,但我确信更有经验的人可以找到错误。

共有4个文件:

SBButton.h

  #import "SBButtonCustomizer.h"

   @interface SBButton : SBButtonCustomizer
   @property (retain) UIColor* buttonColor;
   @property (retain) UIColor* buttonHighLightColor;
   @property (retain) UIColor* titleColor;
   @end

SBButton.m

@implementation SBButton


- (void)drawButtonHighlighted: (BOOL)isHighlighted{
//// General Declarations
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = UIGraphicsGetCurrentContext();

//// Color Declarations
UIColor* iconShadow = [UIColor colorWithRed: 0 green: 0 blue: 0 alpha: 0.8];
UIColor* buttonColor = [UIColor colorWithRed: 0.18 green: 0.631 blue: 0 alpha: 1];
CGFloat buttonColorRGBA[4];
[buttonColor getRed: &buttonColorRGBA[0] green: &buttonColorRGBA[1] blue:   &buttonColorRGBA[2] alpha: &buttonColorRGBA[3]];

UIColor* baseGradientBottomColor = [UIColor colorWithRed: (buttonColorRGBA[0] * 0.6) green: (buttonColorRGBA[1] * 0.6) blue: (buttonColorRGBA[2] * 0.6) alpha: (buttonColorRGBA[3] * 0.6 + 0.4)];
UIColor* upperShine = [UIColor colorWithRed: 0.948 green: 0.948 blue: 0.948 alpha: 0.82];
UIColor* topShine = [upperShine colorWithAlphaComponent: 0.5];
UIColor* bottomShine = [upperShine colorWithAlphaComponent: 0.1];

//// Gradient Declarations
NSArray* baseGradientColors = [NSArray arrayWithObjects:
                               (id)buttonColor.CGColor,
                               (id)baseGradientBottomColor.CGColor, nil];
CGFloat baseGradientLocations[] = {0, 1};
CGGradientRef baseGradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef)baseGradientColors, baseGradientLocations);
NSArray* shineGradientColors = [NSArray arrayWithObjects:
                                (id)upperShine.CGColor,
                                (id)[UIColor colorWithRed: 0.948 green: 0.948 blue: 0.948 alpha: 0.66].CGColor,
                                (id)topShine.CGColor,
                                (id)[UIColor colorWithRed: 0.948 green: 0.948 blue: 0.948 alpha: 0.3].CGColor,
                                (id)bottomShine.CGColor, nil];
CGFloat shineGradientLocations[] = {0, 0.05, 0.09, 0.66, 1};
CGGradientRef shineGradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef)shineGradientColors, shineGradientLocations);

//// Shadow Declarations
UIColor* buttonShadow = iconShadow;
CGSize buttonShadowOffset = CGSizeMake(0.1, 1.1);
CGFloat buttonShadowBlurRadius = 2;

//// Frames
CGRect frame = CGRectMake(0, 0, 229, 38);


//// Button
{
    CGContextSaveGState(context);
    CGContextSetAlpha(context, 0.75);
    CGContextBeginTransparencyLayer(context, NULL);


    //// ButtonRectangle Drawing
    CGRect buttonRectangleRect = CGRectMake(CGRectGetMinX(frame) + 2, CGRectGetMinY(frame) + 1, CGRectGetWidth(frame) - 4, CGRectGetHeight(frame) - 4);
    UIBezierPath* buttonRectanglePath = [UIBezierPath bezierPathWithRoundedRect: buttonRectangleRect cornerRadius: 7];
    CGContextSaveGState(context);
    CGContextSetShadowWithColor(context, buttonShadowOffset, buttonShadowBlurRadius, buttonShadow.CGColor);
    CGContextBeginTransparencyLayer(context, NULL);
    [buttonRectanglePath addClip];
    CGContextDrawLinearGradient(context, baseGradient,
                                CGPointMake(CGRectGetMidX(buttonRectangleRect), CGRectGetMinY(buttonRectangleRect)),
                                CGPointMake(CGRectGetMidX(buttonRectangleRect), CGRectGetMaxY(buttonRectangleRect)),
                                0);
    CGContextEndTransparencyLayer(context);
    CGContextRestoreGState(context);



    //// Rounded Rectangle Drawing
    CGRect roundedRectangleRect = CGRectMake(CGRectGetMinX(frame) + 2, CGRectGetMinY(frame) + 1, CGRectGetWidth(frame) - 4, floor((CGRectGetHeight(frame) - 1) * 0.48649 + 0.5));
    UIBezierPath* roundedRectanglePath = [UIBezierPath bezierPathWithRoundedRect: roundedRectangleRect cornerRadius: 7];
    CGContextSaveGState(context);
    [roundedRectanglePath addClip];
    CGContextDrawLinearGradient(context, shineGradient,
                                CGPointMake(CGRectGetMidX(roundedRectangleRect), CGRectGetMinY(roundedRectangleRect)),
                                CGPointMake(CGRectGetMidX(roundedRectangleRect), CGRectGetMaxY(roundedRectangleRect)),
                                0);
    CGContextRestoreGState(context);


    CGContextEndTransparencyLayer(context);
    CGContextRestoreGState(context);
}


//// Cleanup
CGGradientRelease(baseGradient);
CGGradientRelease(shineGradient);
CGColorSpaceRelease(colorSpace);
 }
 #pragma mark Overrides


 - (void)drawOnState{
[self drawButtonHighlighted: YES];
}

- (void)drawOffState{
[self drawButtonHighlighted: NO];
}
 @end

SBButtonCustomizer.h

#import <Foundation/Foundation.h>

@interface SBButtonCustomizer : NSObject
@property(retain) IBOutlet UIButton* customButton;

 // These 3 should be overrided
 - (CGSize)size;
 - (void)drawOnState;
 - (void)drawOffState;

 @end

SBButtonCustomizer.m

#import "SBButtonCustomizer.h"

@interface SBButtonCustomizer ()
@property(retain) UIImage* onStateImage;
@property(retain) UIImage* offStateImage;

 - (UIImage*)imageForSelector: (SEL)selector;
 @end



 @implementation SBButtonCustomizer

 - (void)awakeFromNib{
  [super awakeFromNib];

self.onStateImage = [[self imageForSelector: @selector(drawOnState)]       resizableImageWithCapInsets: [self capInsets]];
self.offStateImage = [[self imageForSelector: @selector(drawOffState)] resizableImageWithCapInsets: [self capInsets]];

[self.customButton setBackgroundImage: self.onStateImage forState: UIControlStateNormal];
[self.customButton setBackgroundImage: self.offStateImage forState: UIControlStateHighlighted];
}

   - (void)dealloc
   {
    [super dealloc];
  self.onStateImage = nil;
  self.offStateImage = nil;
      self.customButton = nil;
    }


 - (UIImage*)imageForSelector: (SEL)selector
{
UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0f);

  #pragma clang diagnostic push
   #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[self performSelector: selector];
  #pragma clang diagnostic pop

UIImage* result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return result;
}

- (UIEdgeInsets)capInsets
{
return UIEdgeInsetsMake(0, 15, 0, 15);
 }

 - (CGSize)size
 {
return self.customButton.bounds.size;
}


  - (void)drawOnState
  {

  }

 - (void)drawOffState
 {

}



  @end

我在某处读到了我不应该使用的

- (void)awakeFromNib

有故事板但是

initWithCoder

这可能吗?

2 个答案:

答案 0 :(得分:0)

我刚才遇到这个问题并且很容易修复它。我认为SDK中必定存在一个错误,因为在它的initWithCoder方法之后应该在ViewController中的对象上调用awakeFromNib,但是按钮有一个CGRectZero的矩形。

这基本上就是我为解决问题所做的工作。

在SBButtonCustomizer.m中,我将方法名称awakeFromNib更改为例如-(void) didLoad并在SBButton.h中将其公开

在我的IB中的viewController中,我为SBButton对象创建了一个出口,然后在我的viewDidLoad方法的末尾,我简单地调用了对象的didLoad方法。

为我排序,我希望它可以帮助你。

我不知道如何向Apple提交错误,因为我最近刚刚注册了开发者计划和CBATBH。

答案 1 :(得分:0)

正如他们在头文件中所述

    // These 3 should be overrided
- (CGSize)size;
- (void)drawOnState;
- (void)drawOffState;

必须覆盖所有三种方法。否则图形上下文为零。

将其插入SBButton.m

- (CGSize)size
{
    return CGSizeMake(48, 48);
}

根据您的需要调整大小。

我已更改此行

@interface SBButtonCustomizer : NSObject

进入这个

@interface SBButtonCustomizer : UIButton

因为我需要更多NSObject不提供的属性。对我来说效果很好!