xcode 在视图之间滑动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7830466/
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-09-14 22:10:52  来源:igfitidea点击:

Swiping Between Views

objective-ciosxcodeios4xcode4

提问by user964627

Maybe you guys can help me. I have three different views (Current right now in storyboard). I would like the user to swipe between the three views instead of clicking a button. Is there a easy way to enable the swiping and to allow left to right and right to left swiping?

也许你们可以帮助我。我有三种不同的观点(目前在故事板中)。我希望用户在三个视图之间滑动而不是单击按钮。有没有一种简单的方法来启用滑动并允许从左到右和从右到左滑动?

I have been doing some research on Apples Dev. website and found some helpful stuff nothing that really explains switching between views.

我一直在对 Apples Dev 进行一些研究。网站并找到了一些有用的东西,没有真正解释视图之间的切换。

回答by Srikar Appalaraju

Some of the ways to switch between views in iOS -

在 iOS 中切换视图的一些方法 -

  1. Best is to use a NavigationViewControllermuch tighter memory management.
  2. One can use ModalViewControllersalso.
  3. Do a custom presentation.
  1. 最好是使用NavigationViewController更严格的内存管理。
  2. 一个也可以用ModalViewControllers
  3. 进行自定义演示。

Of all the three, the first one although is more mem eff. but to animate the transition between views is difficult. So is the second one. The third gives you the maximum flexibility to provide custom animations between view transitions but you need to really take care of memory management yourself.

在这三个中,第一个虽然更高效。但是动画视图之间的过渡是困难的。第二个也是如此。第三个为您提供了最大的灵活性,可以在视图转换之间提供自定义动画,但您需要自己真正处理内存管理。

Now coming back to your particular problem; it seems pretty straightforward. You can take a couple of approaches -

现在回到你的特定问题;这似乎很简单。你可以采取几种方法 -

  1. Have a huge UIScrollView& enable only horizontal scroll & paging. Now add your views in this scrollView. upon swipe you get to see your new views.
  2. If scroll view is not what you want & want to have some more customization as to how the swipe happens then you can try to do the following -
  1. 有一个巨大的UIScrollView并仅启用水平滚动和分页。现在在此滚动视图中添加您的视图。滑动后,您可以看到您的新视图。
  2. 如果滚动视图不是您想要的并且想要对滑动发生的方式进行更多自定义,那么您可以尝试执行以下操作 -
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleViewsSwipe:)];
[swipe setDirection:UISwipeGestureRecognizerDirectionLeft];
[yourView addGestureRecognizer:swipe];
[swipe release];
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleViewsSwipe:)];
[swipe setDirection:UISwipeGestureRecognizerDirectionLeft];
[yourView addGestureRecognizer:swipe];
[swipe release];

With this the swipe gesture is defined on yourViewif some one swipes iOS will hit the selector handleViewsSwipe. You handle what happens when a user swipes here.

有了这个,滑动手势被定义为yourView是否有人滑动 iOS 会点击选择器handleViewsSwipe。您可以处理用户在此处滑动时发生的情况。

define the selector as

将选择器定义为

- (void)handleViewsSwipe:(UISwipeGestureRecognizer *)gesture

Hope this helps..

希望这可以帮助..

回答by Alan Moore

Yes, you just use the UISwipeGestureRecognizer to recognize left and right swipes:

是的,您只需使用 UISwipeGestureRecognizer 来识别左右滑动:

https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UISwipeGestureRecognizer_Class/Reference/Reference.html

https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UISwipeGestureRecognizer_Class/Reference/Reference.html

You will need to do something different for pre-iOS 3.2 if that matters to you.

如果这对您很重要,您将需要为 iOS 3.2 之前的版本做一些不同的事情。

Switching between views is easy enough, you can just build up three different subviews and only make the one you need visible. There are other ways to do it also, depending on the type of material you are using -- I would need more info to give a more specific answer than that.

在视图之间切换很容易,您只需构建三个不同的子视图,然后只显示您需要的一个。还有其他方法可以做到这一点,具体取决于您使用的材料类型——我需要更多信息来给出比这更具体的答案。

回答by Eliz

You can consider a scroll view as well. It is good especially if you may need pagination in the end, or if you are using them for images and you may need to zoom in and out.

您也可以考虑滚动视图。这很好,特别是如果您最终可能需要分页,或者如果您将它们用于图像并且您可能需要放大和缩小。

http://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/UIScrollView_pg/CreatingBasicScrollViews/CreatingBasicScrollViews.html

http://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/UIScrollView_pg/CreatingBasicScrollViews/CreatingBasicScrollViews.html

回答by Jay Imerman

What about a UIPageControl? You can use that to detect/control requests to switch between pages, then use the delegate UIControlEventValueChangedevent to switch content in the view.

UIPageControl 怎么样?您可以使用它来检测/控制在页面之间切换的请求,然后使用委托UIControlEventValueChanged事件来切换视图中的内容。