Mar*_* A. 2 iphone uiview uicolor
我有一个小的UIView对象CircleColorView.m,它只是创建一个带有彩色圆圈的视图。然后,我将该视图用作一堆按钮(所有不同颜色)的背景。
我的问题发生在调用drawRect:方法时。当我引用对象颜色(即UIColor)时,会崩溃,但仅在某些情况下会崩溃。
我很迷茫。这是我的UIView:
ColorCircleView.h
#import <UIKit/UIKit.h>
#import "Constants.h"
@interface CircleColorView : UIView {
UIColor *color;
}
@property (nonatomic, retain) UIColor *color;
- (id)initWithFrame:(CGRect)frame andColor:(UIColor *)circleColor;
@end
Run Code Online (Sandbox Code Playgroud)
这是ColorCircleView.m
#import "CircleColorView.h"
@implementation CircleColorView
@synthesize color;
- (id)initWithFrame:(CGRect)frame andColor:(UIColor *)circleColor {
if ((self = [super initWithFrame:frame])) {
color = [UIColor colorWithCGColor:[circleColor CGColor]];
// have also tried
// color = circleColor;
}
return self;
}
- (void) drawRect: (CGRect) aRect
{
CGFloat iconSize = self.frame.size.width;
// Create a new path
CGContextRef context = UIGraphicsGetCurrentContext();
CGMutablePathRef path = CGPathCreateMutable();
// Set up fill for circle
const CGFloat* fill = CGColorGetComponents(color.CGColor);
CGContextSetFillColor(context, fill);
// Add circle to path
CGRect limits = CGRectMake(8.0f, 8.0f, iconSize - 16.0f, iconSize - 16.0f);
CGPathAddEllipseInRect(path, NULL, limits);
CGContextAddPath(context, path);
CGContextFillEllipseInRect(context, limits);
CGContextFillPath(context);
CFRelease(path);
}
- (void)dealloc {
[color release];
[super dealloc];
}
@end
Run Code Online (Sandbox Code Playgroud)
这是我用来创建CircleColorView并将其添加到按钮图像的代码。它位于循环中,循环中的字符串数组的颜色值之间用a分隔;
NSArray *values = [[NSArray alloc] initWithArray:[[[colorListArray objectAtIndex:i] objectAtIndex:1] componentsSeparatedByString:@";"]];
float red = [[values objectAtIndex:0] floatValue];
float green = [[values objectAtIndex:1] floatValue];
float blue = [[values objectAtIndex:2] floatValue];
UIColor *color = [[UIColor alloc]
initWithRed: (float) (red/255.0f)
green: (float) (green/255.0f)
blue: (float) (blue/255.0f)
alpha: 1.0];
UIButton *newColorButton = [UIButton buttonWithType:0];
//Create Colored Circle
CircleColorView *circle = [[CircleColorView alloc] initWithFrame:CGRectMake(0, 0, 75, 75) andColor:color ];
circle.backgroundColor = [UIColor clearColor];
//Set Button Attributes
[newColorButton setTitle:[[colorListArray objectAtIndex:i] objectAtIndex:1] forState:UIControlStateDisabled];
[newColorButton setFrame:CGRectMake(600+(i*82), 12, 75, 75)]; //set location of each button in scrollview
[newColorButton addTarget:self action:@selector(changeColor:) forControlEvents:UIControlEventTouchDown];
[newColorButton setTag:tagNum];
[barContentView addSubview:newColorButton];
[circle release];
[color release];
[values release];
Run Code Online (Sandbox Code Playgroud)
我已记录下来以查看会发生什么。看起来它运行CircleColorView的initWithFrame:andColor:很好。然后,当调用drawRect:时,它将在首次引用属性'color'时崩溃。即使只是要求提供诸如[颜色描述]之类的说明。
有任何想法吗。我是否创建此UIColor * color错误?还是保留错误?这是另一件事。这段代码可以运行一会儿。然后,当我退出并重新启动应用程序时,它将崩溃。为了使其再次正常工作,我在iPhone模拟器中删除了构建文件和app文件夹。这将使其重新工作。唯一能够使它正常工作的其他事情是,只需更改UIColor * color @property即可分配,反之亦然。这样一来,我便可以重新构建应用程序并运行一次或两次,而不会出现任何问题。然后它崩溃了。哦,它在设备上做同样的事情。有任何想法吗???
提前致谢,
标记
该行声明了color属性,该属性将在分配时保留颜色。
@property (nonatomic, retain) UIColor *color;
Run Code Online (Sandbox Code Playgroud)
该行绕过属性设置器,并直接将自动释放的对象分配给成员。
color = [UIColor colorWith...
Run Code Online (Sandbox Code Playgroud)
您可以使用以下任一方法修复它:
self.color = [UIColor colorWith...
Run Code Online (Sandbox Code Playgroud)
要么
[color retain];
Run Code Online (Sandbox Code Playgroud)
要么
color = [[UIColor alloc] initWith...
Run Code Online (Sandbox Code Playgroud)