ios 使用滑动手势进行视图导航

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6326816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:19:45  来源:igfitidea点击:

Views Navigation Using Swipe Gesture

iphoneiosxcodeipad

提问by Akshay

How can I switch views vertically using swipe gestures ?

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

回答by Akshay

I found my answer. I am posting the code for your reference. Thanks :-)

我找到了我的答案。我发布代码供您参考。谢谢 :-)

in viewDidLoad

viewDidLoad

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

Now

现在

- (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];
}

If you need some more clarification please post here.

如果您需要更多说明,请在此处发布。

回答by rptwsthi

Implement this (didload)

实现这个(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];   

Then This:

那么这个:

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

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

回答by PengOne

Certainly! Just set your viewController to be the UIGestureRecognizerDelegateand declare UISwipeGestureRecognizer *swipeLeftRecognizer;(also retain and synthesize). Then, in the implementation, set up the recognizers with

当然!只需将您的 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];

Then trigger the actions you want with the method

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

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

What you do here is specific to your app. You can switch the tabBar selection, jump through a navigationController, present a different view modally, or just do a simple slide in transition.

您在此处执行的操作特定于您的应用程序。您可以切换 tabBar 选择,跳过导航控制器,以模态方式呈现不同的视图,或者只是在转换中做一个简单的幻灯片。

回答by RiceAndBytes

Adding gestures in swift

快速添加手势

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) {

}

回答by Chris

For reference PengOne, here is the code you referred to in your comment above. I saved it on my Mac because I thought it might be useful one day... :D

作为参考 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"];