我有一个使用 Paintcode 生成的自定义按钮类。它看起来像这样:
//
// TGCustomConfirmButton.m
// Indego
//
// Created by
// Copyright
//
#import "TGCustomConfirmButton.h"
@implementation TGCustomConfirmButton
+ (TGCustomConfirmButton *)buttonWithType:(UIButtonType)type
{
return [super buttonWithType:UIButtonTypeCustom];
}
- (void)drawRect:(CGRect)rect
{
NSLog(@"drawRect enter");
//// General Declarations
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = UIGraphicsGetCurrentContext();
//// Color Declarations
UIColor* strokeColor = [UIColor colorWithRed: 0.143 green: 0.429 blue: 0
alpha: 1];
UIColor* shadowColor2 = [UIColor colorWithRed: 0.712 green: 0.972 blue: 0.489
alpha: 1];
UIColor* gradientColor = [UIColor colorWithRed: 0.391 green: 0.925 blue: 0.262
alpha: 1];
UIColor* gradientColor2 = [UIColor colorWithRed: 0.052 green: 0.454 blue: 0.044
alpha: 1];
UIColor* fillColor2 = [UIColor colorWithRed: 0.833 green: 0.833 blue: 0.833
alpha: 1];
UIColor* gradientColor3 = [UIColor colorWithRed: 1 green: 1 blue: 1
alpha: 1];
//// Gradient Declarations
NSArray* greenHighlightColors = [NSArray arrayWithObjects:
(id)gradientColor.CGColor,
(id)[UIColor colorWithRed: 0.221 green: 0.69
blue: 0.153 alpha: 1].CGColor,
(id)gradientColor2.CGColor, nil];
CGFloat greenHighlightLocations[] = {0, 0.23, 0.58};
CGGradientRef greenHighlight = CGGradientCreateWithColors(colorSpace,
(__bridge CFArrayRef)greenHighlightColors,
greenHighlightLocations);
NSArray* innerShadow002Colors = [NSArray arrayWithObjects:
(id)fillColor2.CGColor,
(id)[UIColor colorWithRed: 0.917 green: 0.917
blue: 0.917 alpha: 1].CGColor,
(id)gradientColor3.CGColor, nil];
CGFloat innerShadow002Locations[] = {0, 0, 0.66};
CGGradientRef innerShadow002 = CGGradientCreateWithColors(colorSpace,
(__bridge CFArrayRef)innerShadow002Colors,
innerShadow002Locations);
//// Shadow Declarations
UIColor* shadow3 = shadowColor2;
CGSize shadow3Offset = CGSizeMake(0.1, 3.1);
CGFloat shadow3BlurRadius = 0;
//// Rounded Rectangle 2 Drawing
UIBezierPath* roundedRectangle2Path = [UIBezierPath bezierPathWithRoundedRect:
CGRectMake(36, 913, 577, 126) cornerRadius: 11];
CGContextSaveGState(context);
[roundedRectangle2Path addClip];
CGContextDrawLinearGradient(context, innerShadow002, CGPointMake(324.5, 913),
CGPointMake(324.5, 1039), 0);
CGContextRestoreGState(context);
//// Rounded Rectangle Drawing
UIBezierPath* roundedRectanglePath=[UIBezierPath bezierPathWithRoundedRect:
CGRectMake(48.5, 926, 550, 86.5) cornerRadius: 6];
CGContextSaveGState(context);
[roundedRectanglePath addClip];
CGContextDrawLinearGradient(context, greenHighlight, CGPointMake(323.5, 926),
CGPointMake(323.5, 1012.5), 0);
CGContextRestoreGState(context);
////// Rounded Rectangle Inner Shadow
CGRect roundedRectangleBorderRect = CGRectInset([roundedRectanglePath bounds],
-shadow3BlurRadius, -shadow3BlurRadius);
roundedRectangleBorderRect = CGRectOffset(roundedRectangleBorderRect,
-shadow3Offset.width, -shadow3Offset.height);
roundedRectangleBorderRect = CGRectInset(CGRectUnion(roundedRectangleBorderRect,
[roundedRectanglePath bounds]), -1, -1);
UIBezierPath* roundedRectangleNegativePath = [UIBezierPath bezierPathWithRect:
roundedRectangleBorderRect];
[roundedRectangleNegativePath appendPath: roundedRectanglePath];
roundedRectangleNegativePath.usesEvenOddFillRule = YES;
CGContextSaveGState(context);
{
CGFloat xOffset = shadow3Offset.width
+ round(roundedRectangleBorderRect.size.width);
CGFloat yOffset = shadow3Offset.height;
CGContextSetShadowWithColor(context,
CGSizeMake(xOffset + copysign(0.1, xOffset),
yOffset + copysign(0.1, yOffset)),
shadow3BlurRadius,
shadow3.CGColor);
[roundedRectanglePath addClip];
CGAffineTransform transform =
CGAffineTransformMakeTranslation
(-round(roundedRectangleBorderRect.size.width), 0);
[roundedRectangleNegativePath applyTransform: transform];
[[UIColor grayColor] setFill];
[roundedRectangleNegativePath fill];
}
CGContextRestoreGState(context);
[strokeColor setStroke];
roundedRectanglePath.lineWidth = 1.5;
[roundedRectanglePath stroke];
//// Cleanup
CGGradientRelease(greenHighlight);
CGGradientRelease(innerShadow002);
CGColorSpaceRelease(colorSpace);
NSLog(@"drawRect exit");
}
@end
此代码正在应用于类型已设置为自定义的 UIButton。该按钮位于应用程序中的两个位置(随着开发的进行还有更多),一次位于 UITableView 下方的视图中,一次位于普通平面 Jane UIViewController 中。在这两种情况下,按钮的标签文本都是在运行时呈现的,但按钮本身的设计却无处可寻。
有很多关于使用 PaintCode 绘制东西的教程(我不需要帮助),但在创建自定义按钮类后如何实现它却为零。在使用非 PaintCode 创建的类之前,我已经这样做了,我很确定我做对了。知道我在哪里出错了吗?