Iphone 自定义iOS按钮类为';t在运行时显示
我有一个使用Paintcode生成的自定义按钮类。看起来是这样的:Iphone 自定义iOS按钮类为';t在运行时显示,iphone,ios,cgrect,Iphone,Ios,Cgrect,我有一个使用Paintcode生成的自定义按钮类。看起来是这样的: // // TGCustomConfirmButton.m // Indego // // Created by // Copyright // #import "TGCustomConfirmButton.h" @implementation TGCustomConfirmButton + (TGCustomConfirmButton *)buttonWithType:(UIButtonType)type {
//
// 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,其类型已设置为custom。该按钮在应用程序中有两个位置(随着开发人员的发展,还有更多),一个是在UITableView下面的视图中,另一个是在普通的平面UIViewController中。在这两种情况下,按钮的标签文本都会在运行时呈现,但按钮本身的设计却看不到
有很多教程介绍如何使用PaintCode绘制东西(我不需要任何帮助),但是没有介绍如何在创建自定义按钮类后实现它。在使用非PaintCode创建的类之前,我已经这样做了,我非常确定我做对了。知道我哪里出了问题吗?请参阅我的评论,也可以从苹果文档中获得:
+ (id)buttonWithType:(UIButtonType)buttonType
此方法是一个方便的构造函数,用于创建具有特定配置的按钮对象。如果您使用子类UIButton,此方法不会返回子类的实例。如果要创建特定子类的实例,必须直接alloc/init按钮
请参见我的评论,也可从苹果文档中获取:
+ (id)buttonWithType:(UIButtonType)buttonType
此方法是一个方便的构造函数,用于创建具有特定配置的按钮对象。如果您使用子类UIButton,此方法不会返回子类的实例。如果要创建特定子类的实例,必须直接alloc/init按钮
避免使用带类型的按钮
TGCustomConfirmButton *button = [[TGCustomConfirmButton alloc] initWithFrame:CGRectZero];
还是这样做
+ (TGCustomConfirmButton *)buttonWithType:(UIButtonType)type {
return type==UIButtonTypeCustom ? [[self alloc] init] : [super buttonType:type];
}
用法:
TGCustomConfirmButton *button = [TGCustomConfirmButton buttonWithType:UIButtonTypeCustom];
避免使用带类型的按钮
TGCustomConfirmButton *button = [[TGCustomConfirmButton alloc] initWithFrame:CGRectZero];
还是这样做
+ (TGCustomConfirmButton *)buttonWithType:(UIButtonType)type {
return type==UIButtonTypeCustom ? [[self alloc] init] : [super buttonType:type];
}
用法:
TGCustomConfirmButton *button = [TGCustomConfirmButton buttonWithType:UIButtonTypeCustom];
没有看到背景色/渐变的原因是drawRect代码上的CGPoints是硬编码的。因此,笔划和填充可能不会最终发生在按钮矩形中 我替换了: 带有rect的CGRectMake(36913577126) CGPointMake(324.5913)和rect.origin CGPointMake(323.51012.5)和CGPointMake(rect.origin.x+rect.size.width,rect.origin.y+rect.size.height) 我几乎可以得到背景填充和渐变。您可能需要根据您的需求进行更多的调整
我使用了这个按钮,在xib中添加了一个按钮,并将其类更改为TGCustomConfirmButton。您没有看到背景色/渐变的原因是因为drawRect代码上的CGPoints是硬编码的。因此,笔划和填充可能不会最终发生在按钮矩形中 我替换了: 带有rect的CGRectMake(36913577126) CGPointMake(324.5913)和rect.origin CGPointMake(323.51012.5)和CGPointMake(rect.origin.x+rect.size.width,rect.origin.y+rect.size.height) 我几乎可以得到背景填充和渐变。您可能需要根据您的需求进行更多的调整
我使用了这个按钮,通过向xib添加一个按钮,并将其类更改为TGCustomConfirmButton,为后代和其他寻求PaintCode帮助的人提供了解决方案 在PaintCode中,您可以创建任意大小的画布。我的印象是,像Illustrator一样,PaintCode只能在有艺术的地方渲染艺术。相反,它尝试在提供的空间中渲染画布中的所有内容。因此,在我的例子中,它试图将640x1136画布渲染为300x44按钮。从某种意义上说,这是可行的;画布顶部的白色正在渲染到按钮的画布中 简而言之,在PaintCode中创建按钮和其他控件时,需要注意所设计元素的大小。即使是这样,300x44的PaintCode设计也无法完美地渲染成那样大小的按钮。我发现我需要在调用自定义类的元素的右侧和底部规划一些丢失的像素。我会处理得更多,可能也会解决这个问题
不管怎样,谢谢你的回答。正如我所说,我希望这能帮助一些人。对于子孙后代和其他寻求PaintCode帮助的人来说,这是我们的解决方案 在PaintCode中,您可以创建任意大小的画布。我的印象是,像Illustrator一样,PaintCode只能在有艺术的地方渲染艺术。相反,它尝试在提供的空间中渲染画布中的所有内容。因此,在我的例子中,它试图将640x1136画布渲染为300x44按钮。从某种意义上说,这是可行的;画布顶部的白色正在渲染到按钮的画布中 简而言之,在PaintCode中创建按钮和其他控件时,需要注意所设计元素的大小。即使是这样,300x44的PaintCode设计也无法完美地渲染成那样大小的按钮。我发现我需要在调用自定义类的元素的右侧和底部规划一些丢失的像素。我会处理得更多,可能也会解决这个问题
不管怎样,谢谢你的回答。正如我所说的,我希望这能帮助一些人。UIButton不太适合子类化,请尝试将UIControl子类化,以实现您试图实现的目标。Kaan Degeblu:错误提示,只是不要使用便利方法初始化它
UIButton不太适合子类化。
根本不正确。苹果公司会在文档中明确说明什么时候类不适合子类化。@sosborn我也很惊讶苹果公司的文档中没有提到任何关于子类化的内容,但我已经看过几次讨论。此外,子类化也很好。如果您在为按钮配音分类时遇到问题,而您只是做得不正确。UIButton并不真正适合子类化,请尝试为您试图实现的内容为UIControl子类化。Kaan Degeblu:错误提示,只是不要使用方便的方法初始化它ui按钮并不真正适合子类化
根本不正确。苹果会变甜的