2014-09-04 3 views
1

안녕하세요 저는 버튼을 개인 설정하는 데 사용하는 UIButton에서 파생되는 클래스 RqButton을 가지고 있습니다.initWithFrame으로 다른 인수를 전달하십시오. CGRectMake()

button = [[RqButton alloc] initWithFrame:CGRectMake(xz, yz, sq, sq)]; 모든 것이 예상대로 작동하지만 다른 매개 변수를 RqButton으로 전달하려는 경우 어떻게되는지 모릅니다.

이것은 내 RqButton.m 당신은 내가 CGrect와 라인에서 사용하기 위해서 INT의 R을 통과하는 동안 클래스를 호출 할 수 있도록하려는 것을 볼

#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 

CGFloat endRadius = R;

물론 button = [[RqButton alloc] initWithFrame:CGRectMake(xz, yz, sq, sq) :1];은 이와 같이 작동하지 않지만 실제로는 실제로 어떻게해야할까요? 당신의 도움에 대한

감사합니다, 알렉스

답변

5

당신이 initWithFramesuper를 사용합니다 RqButton에서 새 init 방법을 생성하기 만하면됩니다. 다른 매개 변수를 사용자 정의 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; 
} 

공개적으로 액세스 할 수 있도록뿐만 아니라 당신의 헤더 파일에이 방법을 추가해야합니다. 이제 다음처럼 RqButton을 init하는 당신이 원하는 곳에서이 메소드를 호출 할 수 있습니다

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

환호 동료, 완벽한 솔루션을! – user2875404

관련 문제