uibezierpath具有“contains”方法。
func contains(_ point: CGPoint) -> Bool
所以,在touchesStarted或toucheSend中使用此方法保存形状并测试命中率。
class SomeV: UIView {
var shape1 = UIBezierPath()
var shape2 = UIBezierPath()
var shape3 = UIBezierPath()
override func draw(_ rect: CGRect) {
shape1 = UIBezierPath() // code for segment 1
shape2 = UIBezierPath() // code for segment 2
shape3 = UIBezierPath() // code for segment 3
// etc
}
override func
touchesEnded( _ touches: Set<UITouch>, with event: UIEvent? ) {
if let w = touches.first?.location( in: self ) {
if shape1.contains( w ) { print( "Touches in shape1" ) }
if shape2.contains( w ) { print( "Touches in shape1" ) }
if shape3.contains( w ) { print( "Touches in shape1" ) }
}
}
}