iphone:将CALayer作为子层添加到UIButton';s层导致EXC\u访问错误
大家好, 我试着给UIButtonTypeUndedrect添加一个很好的渐变。以下代码在中实现iphone:将CALayer作为子层添加到UIButton';s层导致EXC\u访问错误,iphone,uibutton,calayer,exc-bad-access,gradient,Iphone,Uibutton,Calayer,Exc Bad Access,Gradient,大家好, 我试着给UIButtonTypeUndedrect添加一个很好的渐变。以下代码在中实现 viewWillAppear:(BOOL)animated 我的视图控制器的功能 UIButton *tempButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; tempButton.frame = CGRectMake(left, top, 80.0f, 80.0f); CAGradientLayer * hinte
viewWillAppear:(BOOL)animated
我的视图控制器的功能
UIButton *tempButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
tempButton.frame = CGRectMake(left, top, 80.0f, 80.0f);
CAGradientLayer * hintergrundButtonLayer = [[CAGradientLayer alloc] init];
[hintergrundButtonLayer setBounds:tempButton.bounds];
[hintergrundButtonLayer setColors:[NSArray arrayWithObjects:
[UIColor colorWithRed:0.2 green:0.3 blue:0.4 alpha:1.0],
[UIColor colorWithRed:0.4 green:0.5 blue:0.6 alpha:1.0],
nil]];
[hintergrundButtonLayer setPosition:
CGPointMake([tempButton bounds].size.width/2,
[tempButton bounds].size.height/2)];
[hintergrundButtonLayer setLocations:
[NSArray arrayWithObjects:
[NSNumber numberWithFloat:0.0],
[NSNumber numberWithFloat:1.0],
nil]];
// hintergrundButtonLayer.zPosition = -10;
// hintergrundButtonLayer.frame = [tempButton bounds];
[tempButton addTarget:self action:@selector(switchVendorOnOff:) forControlEvents:UIControlEventTouchUpInside];
CALayer * downButtonLayer = [tempButton layer];
[downButtonLayer setMasksToBounds:YES];
[downButtonLayer setCornerRadius:10.0];
[downButtonLayer setBorderWidth:1.2];
[downButtonLayer setBorderColor:[[UIColor blackColor] CGColor]];
[downButtonLayer addSublayer:hintergrundButtonLayer];
[hintergrundButtonLayer release];
hintergrundButtonLayer = nil;
[self.view addSubview:tempButton];
在应用程序尝试显示视图之前,代码运行正常-它会在执行错误访问时崩溃。但是如果我把这个评论删掉
[downButtonLayer addSublayer:hintergrundButtonLayer];
命令,那么一切都好了
调试器没有告诉我任何关于泄漏的信息。分配模板也没有显示任何奇怪的内容。控制台上除了显示EXC\u BAD\u ACCESS消息外,没有其他内容。“构建和分析”没有显示错误
有人看到我的问题了吗?设置颜色采用CGColors,而不是UIColors
[hintergrundButtonLayer setColors:[NSArray arrayWithObjects:
[[UIColor colorWithRed:0.2 green:0.3 blue:0.4 alpha:1.0] CGColor],
[[UIColor colorWithRed:0.4 green:0.5 blue:0.6 alpha:1.0] CGColor],
nil]];
如果读取崩溃中的顶层堆栈帧(在调试器下运行时),它们表明CGColors存在问题:
(gdb) bt
#0 0x00bd407c in CGColorSpaceGetMD5Digest ()
#1 0x00b4f8b2 in CGColorTransformConvertColorFloatComponents ()
#2 0x00b4fea2 in CGColorTransformConvertColorComponents ()
#3 0x00cb0d62 in CA_CGColorGetRGBComponents ()
#4 0x00d509aa in -[CAGradientLayer _copyRenderLayer:layerFlags:commitFlags:] ()
我试过你的成功,但没有成功!我认为[downButtonLayer addSublayer:hintergrundButtonLayer]的调用;只需将hintergrundButtonLayer对象的指针指定给downButtonLayer对象。。如果我将我的指针HinterRundButtonLayer设置为nil,downButtonLayer对象应该仍然有一个指向HinterRundButtonLayer-object的指针。你做到了!!!非常感谢你的帮助!!!!正是这个问题,错误的颜色类型;)