I have a shape like below and I want set it as background to my UIButton but touchable area is a rectangle, any suggestion to change touchable area to shape boarders?
相关问题
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
- SwiftUI: UIImage (QRCode) does not load after call
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- Using if let syntax in switch statement
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- Enum with associated value conforming to CaseItera
- Swift - hide pickerView after value selected
- Is there a Github markdown language identifier for
Actually I found it on this like and working perfectly:
How to know that if the only visible area of a .png is touched in XCode (swift or objective C)
But must change code like below:
}
Swift 4.2
It's better to write an extension for UIButton. Override the touchesBegan function and check if the touch area is transparent or not.
The idea is to have a subclass of UIButton that overrides the following methods:
and
There is a tutorial in Objective-C that utilizes hit test (just to catch the idea). In your case, the hardest problem is to detect if the received touch location is within the bounds of your custom shape (the tutorial above relies on pixel transparency, which is not the case for you). I assume, you draw the shape using a Bezier path. If that's what you do, you can implement the point inside evaluation with the func
containsPoint(_ point: CGPoint) -> Bool
of UIBezierPath. Good luck. P.S.There's also one tricky thing about UIBezierPath: