【问题标题】:UILongPressGestureRecognizer sending action twiceUILongPressGestureRecognizer 发送动作两次
【发布时间】:2015-04-27 21:09:14
【问题描述】:

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

我试图找出一个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 会出现两次。

【问题讨论】:

  • 基本上是this等的复制品。

标签: ios swift uigesturerecognizer presentviewcontroller long-press


【解决方案1】:

您应该处理长按手势识别器的状态。 UILongPressGestureRecognizer 的动作调用它的状态变化。因此,当 state == UIGestureRecognizerStateBegan 和第二次 UIGestureRecognizerStateEnded 时,您会收到它。

你需要这样的东西:

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

【讨论】:

  • 所有的GestureRecognizer都是这样的吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-20
  • 2017-09-13
  • 1970-01-01
  • 2020-05-25
  • 2015-05-17
  • 2011-07-05
  • 1970-01-01
相关资源
最近更新 更多