我的代码结果是在我画完线之后,它会检测出正确或错误的位置并显示结果,但在我画好线之后,会突然显示结果。我想让它在我画了一条以上的线后显示结果。我该怎么办?
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"更改这些内容,否则在这种情况下,数据封装会有点混乱。