使用CALayer无法按下按钮

时间:2014-07-30 21:30:44

标签: ios objective-c cocoa-touch

我有一个按钮(iOS),其CALAYer设置了掩码:

self.aButton.layer.backgroundColor = [UIColor redColor].CGColor;
button.layer.masksToBounds = YES;

然后定义的边界比按钮区域小得多(按钮大小约为50x50pts)。

button.layer.bounds = CGRectMake(0, 0, 29, 29);

现在只能在图层区域内单击按钮。允许整个按钮区域(50x50pts)仍可点击的最简单方法是什么?

1 个答案:

答案 0 :(得分:0)

屏蔽UIView的图层,将其作为子视图添加到按钮中。

UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 29, 29)];
redView.layer.backgroundColor = [UIColor redColor].CGColor;

UIButton *aButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];

redView.center = aButton.center;
redView.userInteractionEnabled = NO;
[aButton addSubview:redView];

或作为子图层

CALayer *layer = [[CALayer alloc] init];
layer.backgroundColor = [UIColor redColor].CGColor;
layer.frame = CGRectMake(10.5, 10.5, 29, 29);

UIButton *aButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
[aButton.layer addSublayer:layer];