ios - touchesEnded 后如何使用 touchesBegan
问题描述
我的代码结果是在我已经画线之后,它会检测正确或错误的位置并显示结果,但是在我已经画完之后它会突然显示结果。我希望它在绘制超过 1 条线后显示结果。我能怎么做 ?
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
let touch = touches.first
swiped = false
lastPoint = touch?.location(in: self)
firstPoint = lastPoint
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesMoved(touches, with: event)
if let touch = touches.first {
swiped = true
currentPoint = touch.location(in: self)
drawShapeLayer(from: lastPoint, to: currentPoint)
lastPoint = currentPoint
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesEnded(touches, with: event)
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)
}
}
解决方案
首先,使用 UISwipeGestureRecognizer 可能更容易 https://developer.apple.com/documentation/uikit/uiswipegesturerecognizer
它们抽象出很多东西并简化了状态和路径之类的东西。
其中一些让我感到困惑
if !swiped {
drawShapeLayer(from: lastPoint, to: lastPoint!)
}
那不应该是
if !swiped {
let touch = touches.first
lastPoint = touch?.location(in: self)
drawShapeLayer(from: firstPoint, to: lastPoint!)
}
也在这
if c1.contains(firstPoint) && c3.contains(linePoint) && c2.contains(lastPoint){
AlertView.instance.showAlert(title: "Hooray!", message: "You made it!", alertType : .success)
}
不会有 linePoint != lastPoint 的时候。
除非“drawShapeLayer”改变了这些,在这种情况下你的数据封装有点混乱。
推荐阅读
- c++ - 对棋程序实施限时迭代深化搜索
- php - 具有持久性的 Laravel 模拟模型
- r - 从带有反应变量的渲染图中排除数据点
- scala - 如何使用while循环将数据收集到单个列表中?
- django - ImageField url 属性
- python - Python Pandas DataFrame 中具有 IF 条件的 Lambda 函数行为异常
- c - 如何通过 liburing 实现每秒零纳秒的计时器?
- flutter - Flutter:TextFormField:调用setState()后光标重置为开始
- security - 如何保护我的应用程序中写入的数据,如密码
- java - H2 java.lang.ClassNotFoundException 用于在类路径中传递的触发器