2017-09-16 3 views
1

길게 누르는 동안 작업을하려고합니다 (지금은 데이터를 인쇄하고 있습니다). 하지만 시뮬레이터/휴대 전화에서 길쭉한 동작을 할 때마다 여러 번 반복됩니다. 길게 누르는 제스처가 활성화 될 때마다 정확히 한 번만 작업을 수행하도록하려면 어떻게해야합니까?길게 누르기 제스처에서 여러 작업 수행

사과, 나는 iOS 개발에 상당히 익숙하다. 현재 상태로 여러 번 호출

@IBAction func addRegion(_ sender: Any) { 
    guard let longPress = sender as? UILongPressGestureRecognizer else 
    { return } 
    let touchLocation = longPress.location(in: mapView) 
    let coordinate = mapView.convert(touchLocation, toCoordinateFrom: mapView) 
    let region = CLCircularRegion(center: coordinate, radius: 50, identifier: "geofence") 
    mapView.removeOverlays(mapView.overlays) 
    locationManager.startMonitoring(for: region) 
    let circle = MKCircle(center: coordinate, radius: region.radius) 
    mapView.add(circle) 
    print(coordinate.latitude) 
    print(coordinate.longitude) 

    //returns: 
    // 27.4146234860156 
    // 123.172249486142 
    // ... (a lot of these) 
    // 27.4146234860156 
    // 123.172249486142 

} 

답변

3

제스처 인식 기능 :

여기 내 코드입니다. 당신이 길게 누르면 제스처는 당신은 아래와 같은 동작 상태에 대한 확인을 신청해야

을 액티브하게 할 때 뭔가를하고 싶은 경우

 guard let longPress = sender as? UILongPressGestureRecognizer else 
     { return } 

     if longPress.state == .began { // When gesture activated 

     } 
     else if longPress.state == .changed { // Calls multiple times with updated gesture value 

     } 
     else if longPress.state == .ended { // When gesture end 

     } 
+0

을 그리고 필요한 경우'.changed' 상태를 확인하십시오. – rmaddy