如何禁用按钮边界外的TouchUpInside Action [英] How to disable TouchUpInside Action outside the boundary of button

查看:25
本文介绍了如何禁用按钮边界外的TouchUpInside Action的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 Xib.我只是拖了一个 UIButton 并改变了它的背景颜色.

This is my Xib. I just dragged a UIButton and changed its background color.

我使用此代码创建了一个半圆形图层

I created a semi circle layer using this code

    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

这是我的输出.

它是我想要的完美.但问题是此按钮可在其圆形区域外单击(根据按钮的实际形状).我希望它只能作为圆形点击.

Its perfect as I want it. But the problem is that this button is clickable outside its rounded area(as per actuall shape of button). I want it to be clickable only as rounded shape.

我怎样才能做到这一点?谢谢.

How can I achieve this ? Thanks.

推荐答案

创建签名动作

- (IBAction)buttonPressed:(id)sender forEvent:(UIEvent*)event{
//your code
} 

用于您的按钮

使用此答案找出触摸位置UIButton TouchUpInside Touch Location

Find out touch location using this answer UIButton TouchUpInside Touch Location

使用

- (IBAction)buttonPressed:(id)sender forEvent:(UIEvent*)event{
  //find location point using above answer link
  if([button.layer.mask containsPoint: location])
  {
   //proceed with the code
  }
} 

如果它包含点,则返回一个布尔值.

it returns a bool if it contains point.

即如果它包含点,您可以继续,否则您可以返回.

i.e if it contains point you can proceed otherwise you can return.

希望有帮助:)

这篇关于如何禁用按钮边界外的TouchUpInside Action的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆