Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.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 在Swift中检测到一个现金流贩卖机上的窃听?_Ios_Swift_Cashapelayer - Fatal编程技术网

Ios 在Swift中检测到一个现金流贩卖机上的窃听?

Ios 在Swift中检测到一个现金流贩卖机上的窃听?,ios,swift,cashapelayer,Ios,Swift,Cashapelayer,我对iOS开发非常陌生(请原谅我的无能——我到处都找过了!),我一直在寻找一种方法来检测对CAShapeLayer的点击。到目前为止,我遇到了hitTest。hitTest是最好的方法吗?如果是,它是如何在Swift中使用的,尤其是与CAShapeLayers一起使用的?另外,如果我有许多cashapelayer,我将如何使用hitTest方法分别引用它们 这就是我如何创建的CAShapeLayer: let newBounds = CGRect(x: 0, y: 0, width: 2

我对iOS开发非常陌生(请原谅我的无能——我到处都找过了!),我一直在寻找一种方法来检测对
CAShapeLayer
的点击。到目前为止,我遇到了
hitTest
hitTest
是最好的方法吗?如果是,它是如何在Swift中使用的,尤其是与
CAShapeLayer
s一起使用的?另外,如果我有许多
cashapelayer
,我将如何使用
hitTest
方法分别引用它们

这就是我如何创建的
CAShapeLayer

    let newBounds = CGRect(x: 0, y: 0, width: 200, height: 200)
    let newShape = CAShapeLayer()
    newShape.bounds = newBounds
    newShape.position = view.center
    newShape.cornerRadius = newBounds.width / 2
    newShape.path = UIBezierPath(ovalInRect: newShape.bounds).CGPath

    newShape.lineWidth = 42
    newShape.strokeColor = UIColor(red: 222/255.0, green: 171/255.0, blue: 66/255.0, alpha: 1.0).CGColor
    newShape.fillColor = UIColor.clearColor().CGColor

    newShape.strokeStart = 0.2
    newShape.strokeEnd = 0.4

    view.layer.addSublayer(newShape)

使用以下代码触摸
CAShapeLayer

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    for (UITouch *touch in touches) {
        CGPoint touchLocation = [touch locationInView:self.view];
        for (id sublayer in self.view.layer.sublayers) {
            BOOL touchInLayer = NO;
            if ([sublayer isKindOfClass:[CAShapeLayer class]]) {
                CAShapeLayer *shapeLayer = sublayer;
                if (CGPathContainsPoint(shapeLayer.path, 0, touchLocation, YES)) {
                    // Here your code for do any opration.
                    touchInLayer = YES;
                }
            } else {
                CALayer *layer = sublayer;
                if (CGRectContainsPoint(layer.frame, touchLocation)) {
                    // Touch is in this rectangular layer
                    touchInLayer = YES;
                }
            }
        }
    }
}

以下是实现目标的最佳方法:

// First add the shapelayer
let layer = CAShapeLayer()
layer.anchorPoint = CGPointZero
layer.path = UIBezierPath(ovalInRect: CGRect(x: 0, y: 0, width: 100, height: 200)).CGPath
layer.bounds = CGPathGetBoundingBox(layer.path) // IMPORTANT, without this hitTest wont work
layer.fillColor = UIColor.redColor().CGColor
self.view.layer.addSublayer(layer)


// Check for touches
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    let point = touches.anyObject()!.locationInView(self.view) // Where you pressed

    if let layer = self.view.layer.hitTest(point) as? CAShapeLayer { // If you hit a layer and if its a Shapelayer
        if CGPathContainsPoint(layer.path, nil, point, false) { // Optional, if you are inside its content path
            println("Hit shapeLayer") // Do something
        }
    }
}

Swift 4&5

My
UIImageView
具有多个嵌入的
CAShapeLayer
对象。下面是我如何能够检测到他们的窃听。引用自

override func touchsbegind(touch:Set,带有事件:UIEvent?){
让我们先接触
guard let point=touch?.location(在:imageView中)else{return}
guard let sublayers=imageView.layer.sublayers as?[CAShapeLayer]else{return}
对于子层中的层{
如果let path=layer.path,则path.contains(点){
打印(图层)
}
}
}

我使用了仲裁人的代码,但出现了一些错误。 这是一个我没有错误的代码。对于swift 3.2/4.0

override func viewDidLoad() {
    super.viewDidLoad()

    let layer = CAShapeLayer()
    layer.anchorPoint = CGPoint.zero
    layer.path = UIBezierPath.init(ovalIn: CGRect(x: 0, y: 0, width: 100, height: 200)).cgPath
    layer.bounds = (layer.path?.boundingBox)! // IMPORTANT, without this hitTest wont work
    layer.fillColor = UIColor.red.cgColor
    self.view.layer.addSublayer(layer)

}
    // Check for touches

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let point = touches.first?.location(in: self.view) // Where you pressed

    if let layer = self.view.layer.hitTest(point!) as? CAShapeLayer { // If you hit a layer and if its a Shapelayer
        if (layer.path?.contains(point!))! { // Optional, if you are inside its content path
            print("Hit shapeLayer") // Do something
        }
    }
}
override func viewDidLoad(){
super.viewDidLoad()
let layer=CAShapeLayer()
layer.anchorPoint=CGPoint.zero
layer.path=UIBezierPath.init(ovalIn:CGRect(x:0,y:0,宽度:100,高度:200)).cgPath
layer.bounds=(layer.path?.boundingBox)!//重要信息,没有此命中测试将无法工作
layer.fillColor=UIColor.red.cgColor
self.view.layer.addSublayer(层)
}
//检查触摸
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
让point=touchs.first?.location(在:self.view中)//按下的位置
如果让layer=self.view.layer.hitTest(point!)作为?CAShapeLayer{//如果你碰到一个层,如果它是一个Shapelayer
如果(layer.path?.contains(point!))!{//可选,如果您在其内容路径内
print(“Hit shapeLayer”)//做点什么
}
}
}

谢谢。你知道我能否在Swift中获取此代码吗?我强烈建议在此基础上进行hitTest。但我们无法直接在
CAShapeLayer
-(void)点击:(UITapGestureRecognitizer*)识别器{//我已将TapGestureRecognitizer设置为viewDidLoad CGLayer*tappedLayer=[rainbowView.layer.presentationlayer hitTest:[识别器位置查看:rainbowView];如果(tappedLayer==purpleLayer)//例如NSLog(@“PURPLE!”);}也不起作用我试过你的代码,但由于某种原因,当我点击CAShapeLayer时,没有任何东西被打印到控制台上。告诉我你是如何创建ShapeLayer的我已经让它工作了,谢谢!另外,如果我有很多CAShapeLayer,我将如何使用hitTest方法单独引用它们?你可以对它进行子类化,并给它一个名为tag或id的属性entifier amd然后检查该属性。或者您可以在视图或viewcontroller中将它们设置为属性,并检查eatch属性。
override func viewDidLoad() {
    super.viewDidLoad()

    let layer = CAShapeLayer()
    layer.anchorPoint = CGPoint.zero
    layer.path = UIBezierPath.init(ovalIn: CGRect(x: 0, y: 0, width: 100, height: 200)).cgPath
    layer.bounds = (layer.path?.boundingBox)! // IMPORTANT, without this hitTest wont work
    layer.fillColor = UIColor.red.cgColor
    self.view.layer.addSublayer(layer)

}
    // Check for touches

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let point = touches.first?.location(in: self.view) // Where you pressed

    if let layer = self.view.layer.hitTest(point!) as? CAShapeLayer { // If you hit a layer and if its a Shapelayer
        if (layer.path?.contains(point!))! { // Optional, if you are inside its content path
            print("Hit shapeLayer") // Do something
        }
    }
}