我無法弄清楚我應該如何初始化這個自定義類,代碼不是我的,而且我對Obj-C來說還是比較新的,所以我對速度很慢。如何初始化這個從UIButton繼承的自定義類?
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打嗝,好像色調變量是零。那是因爲我錯誤地初始化了它嗎?有沒有人有過這個成功?