如何突出显示一个UIButton子类? [英] How to highlight a UIButton subclass?
问题描述
我有一个子类 UIButton
其中我覆盖 drawRect
一个自定义的按钮。
但现在单元格没有突出显示。
如何解决这个问题?
我有代码准备好另一个自定义drawRect
- (void)drawRect:(CGRect)rect
{
////声明
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = UIGraphicsGetCurrentContext();
////颜色声明
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)];
////渐变声明
NSArray * redGradientColors = [NSArray arrayWithObjects:
(id)darkRed.CGColor,
(id)lightRed.CGColor,nil] ;
CGFloat redGradientLocations [] = {0,1};
CGGradientRef redGradient = CGGradientCreateWithColors(colorSpace,(__bridge CFArrayRef)redGradientColors,redGradientLocations);
////阴影声明
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;
////圆角矩形绘图
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);
//////圆角矩形内阴影
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];
////清理
CGGradientRelease(redGradient);
CGColorSpaceRelease(colorSpace);
}
子类化UIButton没有意义因为它是一种汇编/集群类。
我有最好的经验创建自己的按钮,当子类化UIControl和添加一些自定义行为。
另请选中此(您不应该子类化UIButton)。
然后还要检查如何突出显示UIControl子类。 / p>
I have a subclass of UIButton
where I overwrite drawRect
for a custom looking button.
But now the cell doesn't highlight.
How can I fix this?
I have the code ready for another custom drawRect for when the cell is pressed.
- (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);
}
subclassing UIButton does not make sense because it's a kind of assembly/cluster class.
I had best experience creating own buttons when subclassing UIControl and add some custom behavior.
Check also this (you should not subclass UIButton).
Then also check how you highlight UIControl subclass.
这篇关于如何突出显示一个UIButton子类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!