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