UILongPressGestureRecognizer 被触发两次 [英] UILongPressGestureRecognizer getting fired twice

查看:46
本文介绍了UILongPressGestureRecognizer 被触发两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户长按地图超过 2-4 秒时,UILongPressGestureRecognizer 会被触发两次.我如何确保它只会被触发一次?

UILongPressGestureRecognizer is getting fired twice when user long presses on a map for over 2-4 seconds. How can I ensure it will only be fired once?

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 以获取手势的开始:

You have to check the gesture recognizer´s state for the begin of the gesture:

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

这篇关于UILongPressGestureRecognizer 被触发两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆