我正在使用CAShapeLayer在视图中绘制一些形状。我做了一个功能来处理这件事。因此,例如:
在viewDidLoad()中
drawQuad(UIColor(red: 0.600, green: 0.224, blue: 0.341, alpha: 1.00), firstPoint: CGPointMake(screenWidth * -0.159, screenHeight * 0.249), secondPoint: CGPointMake(screenWidth * 0.65, screenHeight * 0.249), thirdPoint: CGPointMake(screenWidth * 1.01, screenHeight * 0.50), fourthPoint: CGPointMake(screenWidth * 0.399, screenHeight * 0.50))在drawQuad(...)中
func drawQuad(fillColor: UIColor, firstPoint: CGPoint, secondPoint: CGPoint, thirdPoint: CGPoint, fourthPoint: CGPoint) {
let screenWidth = screenSize.width
let screenHeight = screenSize.height
let shape = CAShapeLayer()
containerLayer.addSublayer(shape)
shape.lineJoin = kCALineJoinMiter
shape.fillColor = fillColor.CGColor
let path = UIBezierPath()
path.moveToPoint(firstPoint)
path.addLineToPoint(secondPoint)
path.addLineToPoint(thirdPoint)
path.addLineToPoint(fourthPoint)
path.closePath()
shape.path = path.CGPath
}我被困在试图添加点击识别到每一个形状。我试图做的是创建一个容器CAShapeLayer,它将每个形状保持为子层。如果您查看上面的函数,您将在第6行看到这一点。然后,在viewDidLoad()中,我将最后一个容器层添加到视图中。
这样做的目的是能够这样做一个hitTest:
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
let touch = touches.first as! UITouch
let location = touch.locationInView(self.view)
for layer in containerLayer.sublayers {
if let hitLayer = layer.hitTest(location) {
println("Tapped")
}
}
}不幸的是,这似乎不起作用。有人知道我犯了什么错吗?还是我走错路了?
谢谢!
发布于 2015-04-17 13:15:31
尝试使用CGPathContainsPoint检查触摸的位置是否包含在您层的路径中。
https://stackoverflow.com/questions/29699534
复制相似问题