2012-04-10 12 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

サブクラス化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

私はここにいくつかのコードを投稿してくださいあなたはこれらの方法

関連する問題