iphone:将 CALayer 作为子层添加到 UIButton 的层会导致 EXC_BAD_ACCESS

发布于 2024-11-08 06:31:53 字数 2005 浏览 0 评论 0原文

嘿人们, 我尝试向 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 消息之外,控制台上没有打印任何其他内容。 “构建和分析”显示没有错误。

有人看到我的问题吗?

hey people,
I try to add a nice gradient to a UIButtonTypeRoundedRect. the following code is implemented in the

viewWillAppear:(BOOL)animated   

function of my view controller.

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];

the code runs fine until the app tries to display the view - it crashs with a EXC_BAD_ACCESS. But if I comment out the

[downButtonLayer addSublayer:hintergrundButtonLayer];   

command, then everything is fine.

The debugger tells me nothing about leaks. Also the allocation template shows nothing strange. There is nothing else printed to the console than the EXC_BAD_ACCESS message. The "build and analyse" shows no errors.

Does anyone see my problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

找个人就嫁了吧 2024-11-15 06:31:53

setColors 采用 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:] ()

setColors takes CGColors, not 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]];

If you read the top stack frames in the crash (while running under the debugger), they suggest a problem with 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:] ()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文