swift - 如何检测cgRect中的cgPath

标签 swift xcode uibezierpath cgrect cgpath

我只能检测到 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)
    }
}
}

最佳答案

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

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

(还可以安全地解开 touch 来删除可选内容,并避免在 Touches.first 为 nil 的情况下发生崩溃)

关于swift - 如何检测cgRect中的cgPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58616521/

相关文章:

ios - Swift 允许扩展除一个以外的所有单元格?

swift - 在 UILabel 中查看具有两位小数的数字

iphone - 如何从 TableView IOS 中删除一行

c++ - 仅当 .h 位于特定目录中时,g++ 才会神秘地失败

objective-c - Xcode 中 NSManagedObject 属性的默认值

ios - 如何在 UILabel 的文本下绘制内容?

swift - 具有多个线宽和背景图像的 uibezierpath (swift)

ios - 在 iOS 的 Realm 中添加数据的问题

ios - 如何检测 UITextField 何时更改并触发警报?

ios - 如何遮盖部分重叠的UIBezierPaths