簡體   English   中英

UILongPressGestureRecognizer被解雇了兩次

[英]UILongPressGestureRecognizer getting fired twice

當用戶長時間按下地圖超過2-4秒時,UILongPressGestureRecognizer會被觸發兩次。 我怎樣才能確保它只被解雇一次?

func action(gestureRecognizer:UIGestureRecognizer) {

    println("long pressed on map")


override func viewDidLoad() {
    super.viewDidLoad()

    manager = CLLocationManager()
    manager.delegate = self
    manager.desiredAccuracy = kCLLocationAccuracyBest

    if activePlace == -1 {

        manager.requestWhenInUseAuthorization()
        manager.startUpdatingLocation()



    } else {

        var uilpgr = UILongPressGestureRecognizer(target: self, action: "action:")
        uilpgr.minimumPressDuration = 2.0
        myMap.addGestureRecognizer(uilpgr)

    }        
}

func action(gestureRecognizer:UIGestureRecognizer) {

    println("long pressed on map")
    var touchPoint = gestureRecognizer.locationInView(self.myMap)
    var newCoordinate = myMap.convertPoint(touchPoint, toCoordinateFromView: self.myMap)

    var annotation = MKPointAnnotation()
    annotation.coordinate = newCoordinate
    //annotation.title = "New Place"
    myMap.addAnnotation(annotation)

    var loc = CLLocation(latitude: newCoordinate.latitude, longitude: newCoordinate.longitude)

}

您必須在手勢開始時檢查手勢識別器的state

func action(gestureRecognizer:UIGestureRecognizer) {
    if gestureRecognizer.state == UIGestureRecognizerState.Began {
        // ...
    }
}

長按手勢是連續的。 當指定時間段(minimumPressDuration)按下允許的手指數(numberOfTouchesRequired)並且觸摸不超出允許的移動范圍(allowableMovement)時,手勢開始(UIGestureRecognizerStateBegan)。 每當手指移動時,手勢識別器轉換到改變狀態,並且當任何手指被抬起時,手勢識別器結束(UIGestureRecognizerStateEnded)。

嘗試這樣的事情:

let longGesture = UILongPressGestureRecognizer(target : self,
 action : #selector(someFunc(gestureRecognizer:)))


func someFunc(gestureRecognizer: UILongPressGestureRecognizer){
if gestureRecognizer.state == .began {
//do something
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM