【问题标题】:Views Navigation Using Swipe Gesture使用滑动手势查看导航
【发布时间】:2011-09-13 16:26:09
【问题描述】:

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

【问题讨论】:

标签: iphone ios xcode ipad


【解决方案1】:

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

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

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

【讨论】:

  • "如何实现上下交换?"
【解决方案2】:

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

【讨论】:

  • 感谢 rptwsthi。你能告诉我“搬家”我该怎么做吗?只需加载 viewController 就可以了?
  • 抱歉迟到了,是的,您可以加载、控制器或添加子视图,但我不知道您将如何制作动画......:我
  • @rptwsthi 这段代码绝对完美!非常感谢!
  • @cardmagik 为您服务。 :)
  • 这项工作,但导航非常快。无论如何要放慢速度或像Apple一样工作(在设置中)
【解决方案3】:

当然!只需将您的 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 选择、跳转导航控制器、以模态方式呈现不同的视图,或者只是在过渡中做一个简单的幻灯片。

【讨论】:

  • 感谢 PengOne 的快速回复。还有一件事-我希望该视图像滚动屏幕一样加载。我该怎么做?
  • 动画...您可以使用简单的UIViewAnimationTransition 或制作更精美的自定义动画。谷歌搜索一些很好的例子。
  • 感谢 PengOne。我会试试的。
  • 我知道我不久前在 SO 上看到过一篇文章,其中给出了获得类似于默认导航控制器的漂亮滑动过渡的说明,但我现在找不到它。我相信你会用几个选择搜索词找到它。祝你好运!
【解决方案4】:

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

}

【讨论】:

    【解决方案5】:

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

    【讨论】: