Как обнаружить cgPath в cgRect - PullRequest
       108

Как обнаружить cgPath в cgRect

0 голосов
/ 30 октября 2019

Я могу обнаружить только начальную точку (touchBegan), которая содержится в c1, и конечную точку (touchEnded), которая содержится в c2, но я не могу определить путь между ними в c3. Как мне это сделать?

Изображение приложения

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

1 Ответ

0 голосов
/ 30 октября 2019

Согласно моим комментариям выше, я думаю, что в 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
   }
}

(также безопасно разверните касание, чтобы удалить необязательное и избежать сбоев в маловероятном сценарии, где touches.first равен нулю)

...