CAShapeLayer攻略
问题描述:
我正在使用CAShapeLayer
在视图中绘制一些形状。我做了一个功能来管理这个。因此,举例来说: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")
}
}
}
不幸的是,这似乎并不奏效。有谁知道我正在犯的错误?或者,也许我正在以这种错误的方式去做?
谢谢!
感谢您的快速响应!你可以给我一个我如何用我的代码做这个例子吗? –
'let hitLayer = CGPathContainsPoint(layer.path,nil,location,true)' –
获取错误'条件绑定中的绑定值必须为可选类型' –