【发布时间】:2010-06-22 17:56:49
【问题描述】:
我有一个小的 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
这里是 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
这是我用来创建 CircleColorView 并将其添加到按钮图像的代码。它位于一个循环中,该循环正在遍历一个字符串数组,其中颜色值由 ;
分隔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];
我已经记录下来看看会发生什么。看起来它运行 CircleColorView 的 initWithFrame:andColor: 就好了。然后当 drawRect: 被调用时,它会在第一次引用属性 'color' 时崩溃。即使它只是要求它提供类似 [颜色描述] 的描述。
任何想法。我创建这个 UIColor *color 错误吗?还是错误地保留它?这是另一个奇怪的事情。这段代码可以正常运行一段时间。然后当我退出并重新启动应用程序时,它会崩溃。为了让它再次工作,我删除了 iPhone 模拟器中的构建文件和 app 文件夹。这将允许它再次工作。唯一能让它持续工作的另一件事是只需将 UIColor *color @property 更改为 assign ,反之亦然。这将允许我重新构建应用程序并再次运行它而没有问题。然后它崩溃了。哦,它在设备上做同样的事情。有任何想法吗???
提前致谢,
标记
【问题讨论】:
-
您的问题解决了吗?请将答案标记为已接受。