浏览导航使用滑动手势

如何使用滑动手势垂直切换视图?

我find了我的答案。 我张贴代码供您参考。 谢谢 :-)

viewDidLoad

  UISwipeGestureRecognizer *swipeGesture = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipedScreendown:)] autorelease]; swipeGesture.numberOfTouchesRequired = 1; swipeGesture.direction = UISwipeGestureRecognizerDirectionDown; [m_pImageView addGestureRecognizer:swipeGesture]; 

现在

 - (void)swipedScreendown:(UISwipeGestureRecognizer*) swipeGesture { m_pViewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; CATransition *transition = [CATransition animation]; transition.duration = 0.75; transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; transition.type = kCATransitionPush; transition.subtype = kCATransitionFromBottom; transition.delegate = self; [self.view.layer addAnimation:transition forKey:nil]; [self.view addSubview:PadViewController.view]; } 

如果你需要更多的澄清请在这里发表。

实现这个(didload)

 //........towards right Gesture recogniser for swiping.....// UISwipeGestureRecognizer *rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipeHandle:)]; rightRecognizer.direction = UISwipeGestureRecognizerDirectionRight; [rightRecognizer setNumberOfTouchesRequired:1]; [urView addGestureRecognizer:rightRecognizer]; [rightRecognizer release]; //........towards left Gesture recogniser for swiping.....// UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipeHandle:)]; leftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft; [leftRecognizer setNumberOfTouchesRequired:1]; [urView addGestureRecognizer:leftRecognizer]; [leftRecognizer release]; 

那么这个:

 - (void)rightSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer { //Do moving } - (void)leftSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer { // do moving } 

当然! 只需将你的viewController设置为UIGestureRecognizerDelegate并声明UISwipeGestureRecognizer *swipeLeftRecognizer; (也保留和综合)。 然后,在实现中,设置识别器

  UIGestureRecognizer *recognizer; // RIGHT SWIPE recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)]; [self.view addGestureRecognizer:recognizer]; [recognizer release]; // LEFT SWIPE recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)]; self.swipeLeftRecognizer = (UISwipeGestureRecognizer *)recognizer; swipeLeftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft; [self.view addGestureRecognizer:swipeLeftRecognizer]; self.swipeLeftRecognizer = (UISwipeGestureRecognizer *)recognizer; [recognizer release]; 

然后用这个方法触发你想要的动作

 - (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer { if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft) { // load a different viewController } else { // load an even different viewController } } 

你在这里做的是特定于你的应用程序。 您可以切换tabBar选项,通过navigationController跳转,以模态方式呈现不同的视图,或者只是在转换过程中做一个简单的幻灯片。

快速添加手势

 func addSwipes() { // Left Swipe let swipeLeft = UISwipeGestureRecognizer(target: self, action: "swipeLeft:") swipeLeft.direction = .Left self.view.addGestureRecognizer(swipeLeft) // Right Swipe let swipeRight = UISwipeGestureRecognizer(target: self, action: "swipeRight:") swipeRight.direction = .Right self.view.addGestureRecognizer(swipeRight) } func swipeLeft(gestureRecognizer: UISwipeGestureRecognizer) { } func swipeRight(gestureRecognizer: UISwipeGestureRecognizer) { } 

参考PengOne,这是你在上面评论中提到的代码。 我把它保存在我的Mac上,因为我认为它可能有用一天…:D

 // Manual navigation push animation UIImage* image = [UIImage imageNamed:@"CurrentView"]; UIImageView* imageView = [[UIImageView alloc] initWithImage:image]; imageView.frame = self.view.bounds; [self.view addSubview:imageView]; [imageView release]; CATransition *transition = [CATransition animation]; transition.type = kCATransitionPush; transition.subtype = kCATransitionFromRight; [self.view.layer addAnimation:transition forKey:@"push-transition"];