【问题标题】:iOS 7 uinavigationcontroller how to detect swipe?iOS 7 uinavigationcontroller 如何检测滑动?
【发布时间】:2013-12-17 20:52:34
【问题描述】:

在新的 iOS 7 UINavigationController 中,有一个滑动手势可以在视图之间切换。 有没有办法检测或拦截手势?

【问题讨论】:

  • I won't disable it。你让它听起来像你在“淘气”
  • 这只是因为我阅读了有关“如何在 uinavigationcontroller 中禁用滑动手势”的类似主题。所以我想清楚^^

标签: ios iphone ios7 uinavigationcontroller uigesturerecognizer


【解决方案1】:

交互式弹出手势识别器通过UINavigationControllerinteractivePopGestureRecognizer 属性公开。您可以添加自己的控制器作为手势识别器的目标并做出适当的响应:

@implementation MyViewController

...

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.navigationController.interactivePopGestureRecognizer addTarget:self 
                                                                  action:@selector(handlePopGesture:)];
}


- (void)handlePopGesture:(UIGestureRecognizer *)gesture
{
    if (gesture.state == UIGestureRecognizerStateBegan)
    {
        // respond to beginning of pop gesture
    }
    // handle other gesture states, if desired
}

...

@end

【讨论】:

    【解决方案2】:

    这里是Austin's answer,在 Swift 中。使用this post 构造选择器,我发现以下工作。

    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationController?.interactivePopGestureRecognizer?.addTarget(self, action:#selector(self.handlePopGesture))
    }
    
    func handlePopGesture(gesture: UIGestureRecognizer) -> Void {
        if gesture.state == UIGestureRecognizerState.Began {
            // respond to beginning of pop gesture
        }
    }
    

    【讨论】:

    • 这对我没有反应......我只是想在手势识别上打印“你好”
    • 我必须在func handlePopGesture 前面添加@objc 以使其成为有效代码。
    猜你喜欢
    • 2020-01-17
    • 2013-06-17
    • 2021-07-01
    • 2011-05-15
    • 2014-06-12
    • 2023-03-14
    • 1970-01-01
    • 2014-04-11
    • 1970-01-01
    相关资源
    最近更新 更多