使用initWithFrame传递另一个参数:CGRectMake()

use*_*404 1 objective-c button ios

嘿,我有一个类RqButton从推导UIButton,我使用个性化我的按钮.

如果如果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
Run Code Online (Sandbox Code Playgroud)

你看到我希望能够在传递CGrect和int 时调用该类,以便在行CGFloat endRadius = r中使用它;

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

谢谢你的帮助,Alex

Sto*_*nz2 5

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

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;
}
Run Code Online (Sandbox Code Playgroud)

确保将此方法添加到头文件中,以便您可以公开访问它.你现在可以从你想要init的RqButton中调用这个方法,如下所示:

RqButton *customButton = [[RqButton alloc] initWithFrame:CGRectMake(xz, yz, sq, sq) radius:2.0];
Run Code Online (Sandbox Code Playgroud)