Ios 卡萨佩尔攻丝
我正在使用Ios 卡萨佩尔攻丝,ios,swift,calayer,Ios,Swift,Calayer,我正在使用CAShapeLayer在视图中绘制一些形状。我做了一个函数来管理这个。例如: 在viewDidLoad()中 在drawQuad(…) 我一直在尝试将敲击识别添加到每个形状。我试图做的是创建一个容器CAShapeLayer,将每个形状保持为一个子层。如果您查看上面的函数,您将在第6行看到这一点。然后,在viewDidLoad()中,我将最后一个容器层添加到视图中 这样做的目的是能够进行如下hitTest: override func touchesBegan(touches: Set
CAShapeLayer
在视图中绘制一些形状。我做了一个函数来管理这个。例如:
在viewDidLoad()中
在drawQuad(…)
我一直在尝试将敲击识别添加到每个形状。我试图做的是创建一个容器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")
}
}
}
override func touchsbegined(touchs:Set,withEvent-event:UIEvent){
让touch=touch.first as!UITouch
让位置=touch.locationInView(self.view)
对于containerLayer.sublayers中的层{
如果let hitLayer=layer.hitTest(位置){
println(“抽头”)
}
}
}
不幸的是,这似乎不起作用。有人知道我犯的错误吗?或者我走错了方向
谢谢大家! 尝试使用来检查触摸的位置是否包含在图层的路径中 谢谢你的快速回复!你能给我一个例子说明我如何使用我的代码做到这一点吗?让hitLayer=CGPathContainsPoint(layer.path,nil,location,true)
在条件绑定中获取错误绑定值必须是可选类型
对不起,我在Swift中不是很好。I目标c可以是:BOOL hitLayer=CGPathContainsPoint(layer.path,nil,location,true)代码>
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
}
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")
}
}
}