iOS 7 uinavigationcontroller如何检测滑动?

Ste*_*ven 11 iphone uinavigationcontroller uigesturerecognizer ios ios7

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

Aus*_*tin 31

交互式流行手势识别是通过暴露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
Run Code Online (Sandbox Code Playgroud)


小智 12

这是奥斯汀在斯威夫特的回答.使用这篇文章构建选择器,我发现以下工作.

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
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 我必须在 `func handlePopGesture` 前面添加 `@objc` 以使其有效代码。 (2认同)