2017-10-14 2 views
0

내가 배열 저장소에있는 모든 첫 번째 터치 마지막 터치를 , 연결된 모든 라인에 대해 신속캐치 신속하고 핵심 그래픽

드로잉 응용 프로그램의를 사용하여 그리기 응용 프로그램을 만드는 중이라서, 그래서 내가 원하는에 지점

import UIKit 

class ViewController: UIViewController { 
    @IBOutlet weak var drawingPlace: UIImageView! 

    var startTouch : CGPoint? 
    var finalTouch : CGPoint? 
    var storePoints : [CGPoint] = [] // this will store the first touch and the last touch 

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
     let touch = touches.first 
     let location = touch?.location(in: drawingPlace) // 
     if startTouch == nil{ 
     startTouch = touch?.location(in: drawingPlace) 
      storePoints.append(startTouch!) 
     }else{ 
      for point in storePoints{ 

       if location == point { // i want to say if location == point or near the point 
        startTouch = point // so the start touch equal the point 
       } 
      } 
     } 
    } 

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 
     let touch = touches.first 
     finalTouch = touch?.location(in: drawingPlace) 
     storePoints.append(finalTouch!) 
     if startTouch != nil{ 
      UIGraphicsBeginImageContext(drawingPlace.frame.size) 
      drawingPlace.image?.draw(in: drawingPlace.bounds) 
      let context = UIGraphicsGetCurrentContext() 

      context?.beginPath() 
      context?.move(to: startTouch!) 
      context?.addLine(to: finalTouch!) 
      context?.strokePath() 

      let img = UIGraphicsGetImageFromCurrentImageContext() 
      UIGraphicsEndImageContext() 
      drawingPlace.image = img 

     } 
    } 

} 

문제는 다음과 같습니다 :

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
     let touch = touches.first 
     let location = touch?.location(in: drawingPlace) // 
     if startTouch == nil{ 
     startTouch = touch?.location(in: drawingPlace) 
      storePoints.append(startTouch!) 
     }else{ 
      for point in storePoints{ 

       if location == point { // i want to say if location == point or near the point 
        startTouch = point // so the start touch equal the point 
       } 
      } 
     } 
    } 

내가 위치하면 원하는보기에서 사용자를 눌러 시작 터치는 배열에서 가까운 포인트가 될 때 touch는 storePoints 배열의 임의의 점 주위에 있으며, 그 다음 startTouch는 점과 같습니다.

나는 감사 할 것입니다.

마젠

+0

어떤 문제가 있습니까? 너 충돌하고있어? 문제를 명확하게 설명하십시오. –

+0

@RoboticCat 모든 것이 괜찮습니다.하지만 사용자가 시작 터치를 누르면 배열에서 가까운 지점이됩니다. – mazen

+0

@RoboticCat touchesBegin 함수 설명을 확인하십시오. – mazen

답변

0

이이 작업을 수행 할 수있는 올바른 방법이지만,이 문제를 75 % 해결, 코드인지 모르겠어요 :

func catchPoint(points : [CGPoint] , location : CGPoint){ 
     let qal = points[0].x - points[0].y 
     for point in points{ 

      let x = location.x - point.x 
      let y = location.y - point.y 

      if abs(x) + abs(y) < abs(qal){ 
       startTouch = point 
      } 
     } 
    } 

왜 75 %, 때문에 다음의 경우 배열 너무 많은 포인트를 가지고 더 지점을 잡으려고 열심히하지 왜.

코드가 도움이되기를 바랍니다.