4
嘿人, 我尝试向UIButtonTypeRoundedRect添加一个很好的渐变。iphone:将CALayer作为子图层添加到UIButton的图层原因EXC_BAD_ACCESS
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];
代码运行正常,直到该应用尝试显示视图 - 它与一个EXC_BAD_ACCESS crashs。但如果我注释掉
[downButtonLayer addSublayer:hintergrundButtonLayer];
命令,那么一切都很好。
调试器告诉我没有任何泄漏。此外,分配模板并不奇怪。除了消息之外,没有任何其他内容被打印到控制台。 “构建和分析”显示没有错误。
有没有人看到我的问题?
我想你的succestion,但它没有工作!我认为[downButtonLayer addSublayer:hintergrundButtonLayer]的调用;只是将指向hintergrundButtonLayer对象的指针指向downButtonLayer对象。如果我将指针hintergrundButtonLayer设置为nil,那么downButtonLayer对象应该仍然有一个指向hintergrundButtonLayer对象的指针。 – dac 2011-05-20 16:31:19
你是对的无效。 – diciu 2011-05-20 17:07:09
你做到了!非常感谢你的帮助 !!!!正是这个问题,错误的颜色类型;) – dac 2011-05-20 18:13:06