UILongPressGestureRecognizer 发送动作两次

UILongPressGestureRecognizer sending action twice

我的长按手势识别器导致其动作事件被执行两次?

我想弄清楚 Warning: Attempt to present VC2 on VC1 whose view is not in the window hierarchy!

通过使用一些 println() 测试,我发现我的 VC2 被显示了两次。

我的VC2演示方法:

P1long:UILongPressGestureRecognizer 位于 VC1 的 MainView

当长按 VC1 的 P1 完成时

@IBAction func PresentPlayerInfo(sender: UIGestureRecognizer){
    var loc = sender.locationInView(self.view)
    var segueSwitch = 0

    if (CGRectContainsPoint(self.P1.frame, lock)) 
        { tappedView = self.P1; segueSwitch = 1 }
    else if (CGRectContainsPoint(self.ReDeal.frame, lock)) 
        { tappedView = self.ReDeal; segueSwitch = 2 }

    if segueSwitch == 1
        { performSegueWithIdentifier("PlayersTable", sender: self)
        println("PlayersTable") }

    else if segueSwitch == 2 
        { self.viewDidLoad() }
}

控制台输出:

PlayersTable PlayersTable Warning: Attempt to present <iPro_Poker_HH_swift.VC2: 0x14555470> on <iPro_Poker_HH_swift.VC1: 0x153a2600> whose view is not in the window hierarchy!

为什么我的 LongPress 动作了两次

您应该处理长按手势识别器的状态。 UILongPressGestureRecognizer 的操作调用它的状态更改。所以你第一次收到它是在 state == UIGestureRecognizerStateBegan 时,第二次是在它的 UIGestureRecognizerStateEnded 时。

你需要这样的东西:

if (recognizer.state == UIGestureRecognizerStateEnded)
{
    //your action
}