如何在按钮边界外禁用TouchUpInside Action
这是我的Xib。 我只是拖了一个UIButton并改变了它的背景颜色。
我使用此代码创建了一个半圆图层
let circlePath = UIBezierPath.init(arcCenter: CGPointMake(mybutton.bounds.size.width / 4, 0), radius: mybutton.bounds.size.height, startAngle: 0.0, endAngle: CGFloat(M_PI), clockwise: true) let circleShape = CAShapeLayer() circleShape.path = circlePath.CGPath mybutton.layer.mask = circleShape
这是我的输出。
它完美,因为我想要它。 但问题是这个按钮可以在其圆形区域外点击(根据按钮的实际形状)。 我希望它只能作为圆形可点击。
我怎样才能做到这一点? 谢谢。
创建签名操作
- (IBAction)buttonPressed:(id)sender forEvent:(UIEvent*)event{ //your code }
为你的按钮
使用此答案找出触摸位置UIButton TouchUpInside Touch Location
检查您的CGPath中是否包含该位置
- (IBAction)buttonPressed:(id)sender forEvent:(UIEvent*)event{ //find location point using above answer link if([button.layer.mask containsPoint: location]) { //proceed with the code } else return; }
如果它包含点,它返回一个bool。
即如果它包含点,你可以继续,否则你可以返回。
希望能帮助到你 :)