嘿,大家好, 我想给一个UIButtonTypeRoundedRect添加一个漂亮的渐变效果。以下代码已经实现:
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 错误。但是,如果我注释掉 标签,问题就会消失。
[downButtonLayer addSublayer:hintergrundButtonLayer];
执行命令后一切正常。
调试器没有显示任何有关泄漏的信息。分配模板也没有显示任何奇怪的东西。控制台上除了EXC_BAD_ACCESS消息之外没有其他输出。"构建和分析"没有显示错误。
有谁能看到我的问题吗?