[英]Button click event (UIButton TouchUpInside action) not working - IOS/Swift
[英]How to disable TouchUpInside Action outside the boundary of button
這是我的錫伯族。 我只是拖動了一個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觸摸位置
使用以下命令檢查該位置是否包含在您的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
}
}
如果包含點,則返回布爾值。
即如果它包含點,則可以繼續進行,否則可以返回。
希望能幫助到你 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.