簡體   English   中英

PaintCode不適用於情節提要

[英]PaintCode doesn't work with the Storyboard

我購買了Paintcode,並在此示例中嘗試了此示例

如果我使用筆尖,則可以使用,但使用情節提要時,它不起作用(它不會繪制自定義按鈕)。 我知道要處理的代碼很多,但是我相信有經驗的人可以找到錯誤。

有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

這可能嗎?

我剛才遇到了這個問題,很容易解決了。 我認為SDK中肯定有一個錯誤,因為awakeFromNib應該在initWithCoder方法之后在ViewController中的對象上調用,但是按鈕具有CGRectZero矩形。

本質上,這就是我為解決問題所做的事情。

在SBButtonCustomizer.m中,我將方法名稱awakeFromNib更改為例如-(void) didLoad並將其在SBButton.h中公開

在IB中的viewController中,我對SBButton對象做了一個出口,然后在viewDidLoad方法的末尾,我簡單地調用了該對象的didLoad方法。

為我排序,希望對您有所幫助。

我不知道如何向Apple提交錯誤,因為我最近才注冊開發者程序和CBATBH。

如它們在頭文件中所述

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

這三種方法都必須被覆蓋。 否則,圖形上下文為nil。

將此插入您的SBButton.m

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

並根據您的需要調整大小。

我改變了這條線

@interface SBButtonCustomizer : NSObject

進入這個

@interface SBButtonCustomizer : UIButton

因為我需要NSObject不提供的更多屬性。 對我來說效果很好!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM