Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/121.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 卡萨佩尔攻丝_Ios_Swift_Calayer - Fatal编程技术网

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")
        }
    }
}