如何使用触摸触摸结束后再次开始

我的代码结果是在我已经画线之后,它将检测到正确或错误的位置并显示结果,但是在我已经画线之后,它将突然显示结果。我希望在绘制多于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)
    }
}
windger2008 回答:如何使用触摸触摸结束后再次开始

首先,使用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'更改这些设置,否则在这种情况下,数据封装会有些混乱。

本文链接:https://www.f2er.com/3128919.html

大家都在问