我无法弄清楚我应该如何初始化这个自定义类,代码不是我的,而且我是Obj-C的新手,所以我的速度很慢。
PFTintedButton.h
#import <UIKit/UIKit.h>
@class CALayer;
typedef enum
{
PFTintedButtonRenderTypeStandard,
PFTintedButtonRenderTypeCandy,
PFTintedButtonRenderTypeOpal,
} PFTintedButtonRenderType;
@interface PFTintedButton : UIButton
{
@private
UIColor * tint;
CGFloat cornerRadius;
PFTintedButtonRenderType renderType;
UIImage * stretchImage;
CALayer * glowLayer;
UILabel * subLabel;
BOOL customShadow;
BOOL customTitleColor;
}
@property( nonatomic, retain ) UIColor * tint;
@property( nonatomic, assign ) CGFloat cornerRadius;
@property( nonatomic, assign ) PFTintedButtonRenderType renderType;
@property( nonatomic, readonly ) UILabel * subLabel;
@end
PFTintedButton.m摘录
#import "PFTintedButton.h"
#import "PFDrawTools.h"
#import "UIColor+PFExtensions.h"
#import <QuartzCore/QuartzCore.h>
#define glowRadius 30
@interface PFTintedButton()
-(void) createBackgroundImage;
-(void) createGlowLayer;
@end
@implementation PFTintedButton
-(void) dealloc
{
SafeRelease( tint );
SafeRelease( stretchImage );
SafeRelease( subLabel );
SafeRelease( glowLayer );
[NSObject cancelPreviousPerformRequestsWithTarget: self selector: @selector(createGlowLayer) object: nil];
[NSObject cancelPreviousPerformRequestsWithTarget: self selector: @selector(renderGlowLayerOnMainThread) object: nil];
[super dealloc];
}
-(void) initCommon
{
cornerRadius = 6;
}
-(id) initWithFrame: (CGRect) frame
{
if( self = [super initWithFrame: frame] )
{
[self initCommon];
}
return self;
}
-(id) initWithCoder: (NSCoder *) coder
{
if( self = [super initWithCoder: coder] )
{
// this is such a hack but there's no other way to determine if the title label's settings
// have been modified in IB.
NSDictionary * dic = [coder decodeObjectForKey: @"UIButtonStatefulContent"];
NSObject * st = [dic objectForKey: [NSNumber numberWithInt: UIControlStateNormal]];
NSString * bc = [st description];
customTitleColor = [bc rangeOfString: @"TitleColor = UIDeviceRGBColorSpace 0.196078 0.309804 0.521569 1"].location == NSNotFound;
customShadow = [bc rangeOfString: @"ShadowColor = UIDeviceRGBColorSpace 0.5 0.5 0.5 1"].location == NSNotFound;
[self initCommon];
//[super setBackgroundColor: [[UIColor redColor] colorWithAlphaComponent: .5]];
}
return self;
}
我刚刚在the full source can be found here.添加了一小部分实现As a part of this repository Paul-Alexander。
我像这样初始化它:
PFTintedButton* buttony = [PFTintedButton buttonWithType:UIButtonTypeCustom];
[buttony setTint:[UIColor redColor]];
[buttony setBackgroundColor:[UIColor redColor]];
[buttony setRenderType:PFTintedButtonRenderTypeCandy];
[buttony setTitle:@"Title for Button" forState:UIControlStateNormal];
它在PFDrawTools中打嗝,似乎色调变量为零。那是因为我正在初始化错误吗?有人有这个成功吗?
答案 0 :(得分:1)
请改为尝试:
PFTintedButton* buttony = [[PFTintedButton alloc] initWithFrame:CGRectMake(x, y, width, height)];
答案 1 :(得分:1)
它似乎显式处理initWithFrame,我会尝试。
PFTintedButton *buttony = [[[PFTintedButton alloc] initWithFrame:<some_frame>] autorelease];
答案 2 :(得分:0)
您是否在buttonWithType:
类中定义了PFTintedButton
初始化方法?