如何在cgRect中检测cgPath

问题描述 投票:0回答:1

我只能检测c1中包含的起点(touchBegan)和c2中包含的终点(touchEnded),但是我无法在c3中检测它们之间的路径,我该怎么办?

Application image

override func layoutSubviews() {
    self.clipsToBounds = true
    self.isMultipleTouchEnabled = false
    self.contentMode = .scaleToFill
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touch = touches.first
    swiped = false
    lastPoint = touch?.location(in: self)
    firstPoint = lastPoint
}


override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touch = touches.first
    swiped = true
    let currentPoint = touch?.location(in: self) 
    lastPoint = currentPoint
    linePoint = lastPoint
    drawShapeLayer(from: lastPoint, to: currentPoint!)
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    if !swiped {
        drawShapeLayer(from: lastPoint, to: lastPoint!)
    }

    if  c1.contains(firstPoint) && c3.contains(linePoint) && c2.contains(lastPoint){
        AlertView.instance.showAlert(title: "Hooray!", message: "You made it!", alertType : .success)
    }

    else {
         AlertView.instance.showAlert(title: "Oops!", message: "You've almost got it.", alertType : .failure)
    }
}
}
swift xcode uibezierpath cgrect cgpath
1个回答
0
投票

根据我上面的评论,我认为touchesMoved中存在逻辑错误,如果您试图绘制三点线,则需要对代码重新排序,然后再检查每个点是否在特定视图中区域。

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first {
      swiped = true
      currentPoint = touch.location(in: self)
      drawShapeLayer(from: lastPoint, to: currentPoint)
      lastPoint = currentPoint
      linePoint = currentPoint
   }
}

(也可以安全地展开触摸以删除可选的控件,并避免在touchs.first为n的不太可能发生的情况下崩溃)

© www.soinside.com 2019 - 2024. All rights reserved.