【问题标题】:How to highlight a UIButton subclass?如何突出显示 UIButton 子类?
【发布时间】:2012-04-22 11:58:18
【问题描述】:

我有一个 UIButton 的子类,我在其中覆盖 drawRect 以获得自定义外观按钮。

但是现在单元格没有突出显示。

我该如何解决这个问题?

我已经为按下单元格时的另一个自定义 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);
}

【问题讨论】:

    标签: iphone objective-c cocoa-touch


    【解决方案1】:

    子类化 UIButton 没有意义,因为它是一种程序集/集群类。

    在继承 UIControl 并添加一些自定义行为时,我拥有创建自己的按钮的最佳体验。

    还要检查this(你不应该继承 UIButton)。

    然后还有check how you highlight UIControl subclass

    【讨论】:

    • 哦,好的。那么如何在不创建子类的情况下合并我发布的代码呢?
    • 为什么不将 drawRect 放在 UIControl 子类中。然后像这样添加一个自定义控件事件:[self addTarget:self action:@selector(myMethode) forControlEvents:UIControlEventTouchUpInside];
    • 您能详细说明一下吗?现在我只有一个空的 UIControl 子类,只有我在上面发布的 drawRect 方法。
    • 在创建类(UIControl 子类)时,是否使用适当的框架调用了 initWithFrame?在你看到你的 drawRect 图纸之后,你可以在这里阅读:stackoverflow.com/questions/4428437/…
    【解决方案2】:

    我认为您可以使用以下这些方法处理突出显示问题

    - (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;
    

    您可以通过这些方法更改颜色或图像

    【讨论】:

      猜你喜欢
      • 2012-02-03
      • 1970-01-01
      • 1970-01-01
      • 2014-07-04
      • 2013-07-30
      • 2011-05-24
      • 1970-01-01
      • 2016-03-19
      • 2014-02-03
      相关资源
      最近更新 更多