UITapGesture没有开火

时间:2016-06-30 09:55:55

标签: ios swift notifications uitapgesturerecognizer

我正在将FrontViewController视图加载到我当前的视图控制器MyVideosViewController

我在MyVideosViewController viewdidload()

中添加了2个通知观察者
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(MyVideosViewController.playLatestvideoClick), name: latestKey, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(MyVideosViewController.playMostWatchedvideoClick), name: mostKey, object: nil)

FrontViewController

 //MARK: -Gesture Play
func playLatestvideoClick(sender: UITapGestureRecognizer)
{
   print("----- PLAY GESTURE TAPPED ------")
    let imgVw = sender.view as! UIImageView
    api.selectedVideoID = String(imgVw.tag)
    let homeviewcontroller = self.storyboard?.instantiateViewControllerWithIdentifier("MYVIDEO") as? MyVideosViewController

    NSNotificationCenter.defaultCenter().postNotificationName(self.latestKey, object: nil)
}

func playMostWatchedvideoClick(sender: UITapGestureRecognizer)
{
    print("----- PLAY GESTURE TAPPED ------")
    let imgVw = sender.view as! UIImageView
    api.selectedVideoID = String(imgVw.tag)
    let homeviewcontroller = self.storyboard?.instantiateViewControllerWithIdentifier("MYVIDEO") as? MyVideosViewController

    NSNotificationCenter.defaultCenter().postNotificationName(self.mostKey, object: nil)
}

这是我在FrontViewController

中设置动画操作的方法
let tapPlay = UITapGestureRecognizer.init(target: self, action: (#selector(FrontViewController.playLatestvideoClick(_:))))
        self.imgVideo.addGestureRecognizer(tapPlay)
        self.imgVideo.userInteractionEnabled=true

但是当我点击手势时,这些方法中的任何一个都没有触发。这是什么原因? 请帮帮我。

更新

就像这样。如果我重新加载这个MyVideoViewController它第一次工作。然后它无法正常工作

1 个答案:

答案 0 :(得分:0)

请尝试以下代码:

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(MyVideosViewController.playLatestvideoClick(_:)), name: latestKey, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(MyVideosViewController.playMostWatchedvideoClick(_:)), name: mostKey, object: nil)

您需要传递此(_:)之类的发件人,然后才会调用此方法。

希望这会对你有所帮助。