2012-04-10 3 views
0

UIButton의 하위 클래스가 있습니다. 여기에서 사용자 정의 찾고 단추에 대해 drawRect을 덮어 씁니다.UIButton 하위 클래스를 강조 표시하는 방법은 무엇입니까?

하지만 이제 셀이 강조 표시되지 않습니다.

어떻게 해결할 수 있습니까?

셀을 눌렀을 때 다른 사용자 정의 drawRect에 대한 코드를 준비했습니다.

- (void)drawRect:(CGRect)rect 
{ 
    //// General Declarations 
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    //// Color Declarations 
    UIColor* red = [UIColor colorWithRed: 1 green: 0 blue: 0 alpha: 1]; 
    CGFloat redRGBA[4]; 
    [red getRed: &redRGBA[0] green: &redRGBA[1] blue: &redRGBA[2] alpha: &redRGBA[3]]; 

    UIColor* darkRed = [UIColor colorWithRed: (redRGBA[0] * 0.8) green: (redRGBA[1] * 0.8) blue: (redRGBA[2] * 0.8) alpha: (redRGBA[3] * 0.8 + 0.2)]; 
    UIColor* lightRed = [UIColor colorWithRed: (redRGBA[0] * 0.8 + 0.2) green: (redRGBA[1] * 0.8 + 0.2) blue: (redRGBA[2] * 0.8 + 0.2) alpha: (redRGBA[3] * 0.8 + 0.2)]; 

    //// Gradient Declarations 
    NSArray* redGradientColors = [NSArray arrayWithObjects: 
            (id)darkRed.CGColor, 
            (id)lightRed.CGColor, nil]; 
    CGFloat redGradientLocations[] = {0, 1}; 
    CGGradientRef redGradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)redGradientColors, redGradientLocations); 

    //// Shadow Declarations 
    CGColorRef shadow = [UIColor lightGrayColor].CGColor; 
    CGSize shadowOffset = CGSizeMake(0, -0); 
    CGFloat shadowBlurRadius = 1; 
    CGColorRef shadow2 = [UIColor blackColor].CGColor; 
    CGSize shadow2Offset = CGSizeMake(0, -0); 
    CGFloat shadow2BlurRadius = 2; 


    //// Rounded Rectangle Drawing 
    UIBezierPath* roundedRectanglePath = [UIBezierPath bezierPathWithRoundedRect: CGRectMake(35, 5, 250, 50) cornerRadius: 6]; 
    CGContextSaveGState(context); 
    CGContextSetShadowWithColor(context, shadow2Offset, shadow2BlurRadius, shadow2); 
    CGContextSetFillColorWithColor(context, shadow2); 
    [roundedRectanglePath fill]; 
    [roundedRectanglePath addClip]; 
    CGContextDrawLinearGradient(context, redGradient, CGPointMake(160, 55), CGPointMake(160, 5), 0); 

    ////// Rounded Rectangle Inner Shadow 
    CGRect roundedRectangleBorderRect = CGRectInset([roundedRectanglePath bounds], -shadowBlurRadius, -shadowBlurRadius); 
    roundedRectangleBorderRect = CGRectOffset(roundedRectangleBorderRect, -shadowOffset.width, -shadowOffset.height); 
    roundedRectangleBorderRect = CGRectInset(CGRectUnion(roundedRectangleBorderRect, [roundedRectanglePath bounds]), -1, -1); 

    UIBezierPath* roundedRectangleNegativePath = [UIBezierPath bezierPathWithRect: roundedRectangleBorderRect]; 
    [roundedRectangleNegativePath appendPath: roundedRectanglePath]; 
    roundedRectangleNegativePath.usesEvenOddFillRule = YES; 

    CGContextSaveGState(context); 
    { 
     CGFloat xOffset = shadowOffset.width + round(roundedRectangleBorderRect.size.width); 
     CGFloat yOffset = shadowOffset.height; 
     CGContextSetShadowWithColor(context, 
            CGSizeMake(xOffset + copysign(0.1, xOffset), yOffset + copysign(0.1, yOffset)), 
            shadowBlurRadius, 
            shadow); 

     [roundedRectanglePath addClip]; 
     CGAffineTransform transform = CGAffineTransformMakeTranslation(-round(roundedRectangleBorderRect.size.width), 0); 
     [roundedRectangleNegativePath applyTransform: transform]; 
     [[UIColor grayColor] setFill]; 
     [roundedRectangleNegativePath fill]; 
    } 
    CGContextRestoreGState(context); 

    CGContextRestoreGState(context); 

    [[UIColor blackColor] setStroke]; 
    roundedRectanglePath.lineWidth = 1; 
    [roundedRectanglePath stroke]; 

    //// Cleanup 
    CGGradientRelease(redGradient); 
    CGColorSpaceRelease(colorSpace); 
} 
+1

여기에 몇 가지 코드를 게시하시기 바랍니다에 색상이나 이미지를 변경할 수 있습니다

- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event; - (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event; - (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event; - (void)cancelTrackingWithEvent:(UIEvent *)event; 

아래에 이러한 방법으로 하이라이트 문제를 해결 수 있다고 생각합니다. –

+0

게시 된 코드 위 – Jon

답변

0

subclassing UIButton은 일종의 어셈블리/클러스터 클래스이므로 이해가되지 않습니다.

UIControl을 서브 클래 싱하고 사용자 정의 동작을 추가 할 때 자신의 버튼을 만드는 것이 가장 좋았습니다.

도 확인하십시오. this을 확인하십시오 (UIButton을 하위 클래스로 분류하면 안됩니다).

또한 check how you highlight UIControl subclass입니다.

+0

오. 그렇다면 하위 클래스를 만들지 않고 게시 한 코드를 어떻게 작성합니까? – Jon

+0

UIControl 하위 클래스에 drawRect를 배치하지 않는 이유는 무엇입니까? 다음과 같이 사용자 정의 컨트롤 이벤트를 추가하십시오.'[self addTarget : self action : @selector (myMethode) forControlEvents : UIControlEventTouchUpInside]'; –

+0

확대 해 주시겠습니까? 지금 당장 나는 위의 게시 된 drawRect 메서드를 사용하여 빈 UIControl 서브 클래 싱을 사용합니다. – Jon

0

내가 당신이이 방법

관련 문제