2014-09-04 7 views
1

Привет, у меня есть класс RqButton, полученный из UIButton, который я использую для персонализации своих кнопок.Передайте еще один аргумент с initWithFrame: CGRectMake()

If If do 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 = г;

Конечно, button = [[RqButton alloc] initWithFrame:CGRectMake(xz, yz, sq, sq) :1]; не будет работать так, но теперь, каков способ сделать это?

Спасибо за вашу помощь, Алекс

ответ

5

Все, что вам нужно сделать, это создать новый метод init внутри RqButton, который будет использовать initWithFrame «ы super. Добавьте другой параметр в свой пользовательский 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