2014-09-04 83 views
1

嘿,我有一个类RqButton派生自UIButton,我用它来个性化我的按钮。用initWithFrame传递另一个参数:CGRectMake()

如果如果做button = [[RqButton alloc] initWithFrame:CGRectMake(xz, yz, sq, sq)];一切都按预期工作,但我想通过另一个参数RqButton,我不知道如何。

这是我RqButton.m

#import "RqButton.h" 

@implementation RqButton 


+ (RqButton *)buttonWithType:(UIButtonType)type 
{return [super buttonWithType:UIButtonTypeCustom];} 

- (void)drawRect:(CGRect)rect r:(int)r 
{ 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 

    float width = CGRectGetWidth(rect); 
    float height = CGRectGetHeight(rect); 

    UIColor *borderColor = [UIColor colorWithRed:0.99f green:0.95f blue:0.99f alpha:1.00f]; 


    CGFloat BGLocations[2] = { 0.0, 1.0 }; 
    CGFloat BgComponents[8] = { 0.99, 0.99, 0.0 , 1.0, 
     0.00, 0.00, 0.00, 1.0 }; 
    CGColorSpaceRef BgRGBColorspace = CGColorSpaceCreateDeviceRGB(); 
    CGGradientRef bgRadialGradient = CGGradientCreateWithColorComponents(BgRGBColorspace, BgComponents, BGLocations, 2); 

    UIBezierPath *roundedRectanglePath = [UIBezierPath bezierPathWithRoundedRect: CGRectMake(0, 0, width, height) cornerRadius: 5]; 
    [roundedRectanglePath addClip]; 

    CGPoint startBg = CGPointMake (width*0.5, height*0.5); 
    CGFloat endRadius= r; 

    CGContextDrawRadialGradient(ctx, bgRadialGradient, startBg, 0, startBg, endRadius, kCGGradientDrawsAfterEndLocation); 
    CGColorSpaceRelease(BgRGBColorspace); 
    CGGradientRelease(bgRadialGradient); 

    [borderColor setStroke]; 
    roundedRectanglePath.lineWidth = 2; 
    [roundedRectanglePath stroke]; 
} 

@end 

你看,我希望能够调用类,而经过CGrect,为了在该行使用它INT R CGFloat的endRadius = R;

当然,button = [[RqButton alloc] initWithFrame:CGRectMake(xz, yz, sq, sq) :1];不会像这样工作,但现在,真正做到这一点的方式是什么?

感谢您的帮助,亚历克斯

回答

5

所有你需要做的是建立内RqButton新init方法将使用initWithFramesuper。将另一个参数添加到自定义初始化中使用的自定义init中。

RqButton.m

- (id)initWithFrame:(CGRect)rect radius:(CGFloat)r 
{ 
    if(self = [super initWithFrame:rect]) 
    { 
     // apply your radius value 'r' to your custom button as needed. 
    } 
    return self; 
} 

确保此方法添加到您的头文件,以及让你可以公开访问。现在您还可以从任何你想要的这个方法来init您RqButton像这样:

RqButton *customButton = [[RqButton alloc] initWithFrame:CGRectMake(xz, yz, sq, sq) radius:2.0]; 
+0

干杯伴侣,完美的解决方案! – user2875404 2014-09-04 16:57:53

相关问题