Xcode UIPageViewController - 如何控制过渡期间的行为?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8231040/
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
Xcode UIPageViewController - how to control behavior during transition?
提问by olavo.mayer
I've been using the UIPageControllertemplate in Xcode to create an interactive book.
我一直在 Xcode 中使用UIPageController模板来创建一本交互式书。
In this app, every page plays a different audio file as soon as the page loads.
在这个应用程序中,每个页面在页面加载时都会播放不同的音频文件。
It works great when I tap one of the corners of the page (the audio from the first page stops and the audio from the second page starts, no observable overlap).
当我点击页面的一个角时效果很好(第一页的音频停止,第二页的音频开始,没有可观察到的重叠)。
The problem occurs when I swipe my finger, dragging the page. Just like on iBooks, I can hold the page in the middle of the transition, seeing both pages, but that causes the audio from both pages to overlap.
当我滑动手指,拖动页面时会出现问题。就像在 iBooks 上一样,我可以将页面保持在过渡的中间,看到两个页面,但这会导致两个页面的音频重叠。
Optimally, I would like for the audio from the first page to stop as soon as the transition begins and the audio from the second page to start as soon as the transition finishes (page curls reaches the end of the screen), but either one of these alone would give me a good result.
最佳情况下,我希望第一页的音频在转换开始后立即停止,第二页的音频在转换完成后立即开始(页面卷曲到达屏幕的末尾),但是仅这些就可以给我一个好的结果。
The thing is I can't find the place to add the [audioFromPage stop] nor the [audioFromPage play] so that there's no overlap.
问题是我找不到添加 [audioFromPage stop] 或 [audioFromPage play] 的地方,所以没有重叠。
I've tried to add these lines to setViewControllers:direction:animated:completion:
method as well as to the viewControllerAfterViewControllerand viewControllerBeforeViewControllermethods, but none of them have worked.
我试图将这些行添加到setViewControllers:direction:animated:completion:
方法以及viewControllerAfterViewController和viewControllerBeforeViewController方法中,但它们都没有工作。
Do you guys have any idea on how to solve this problem?
你们对如何解决这个问题有什么想法吗?
回答by mmc
I have had a similar situation in one of my apps.
我在我的一个应用程序中遇到了类似的情况。
To start the audio only after a successful page turn (either direction) there is an optional method that can be added to whichever class is your the UIPageViewControllerDelegate:
要仅在成功翻页(任一方向)后启动音频,有一个可选方法可以添加到您的 UIPageViewControllerDelegate 类中:
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed;
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed;
Anytime this method is called, you could send the current [pageViewController.viewControllers objectAtIndex:0]
a message to start playing it's sound (it very much appears to me that you are presenting a single page at a time, so the NSArray viewControllers
will always have a single element)
任何时候调用此方法时,您都可以向当前发送[pageViewController.viewControllers objectAtIndex:0]
一条消息以开始播放它的声音(在我看来,您一次呈现一个页面,因此 NSArrayviewControllers
将始终具有单个元素)
Likewise, you could also send a similar message to the [previousViewcontrollers objectAtIndex:0]
a message to stop playing its sound.
同样,您也可以向 a 消息发送类似[previousViewcontrollers objectAtIndex:0]
的消息以停止播放其声音。
This will switch the sound from one page to the next at the completion of a page turn, rather than the beginning, as you requested, but there is no analogous method for pageViewcontroller didBeginAnimating:
这将在完成翻页时将声音从一页切换到下一页,而不是按照您的要求从开头切换,但没有类似的方法 pageViewcontroller didBeginAnimating:
the safest way to do this would be to define a protocol that includes your audioFromPage stop and start methods, and then make every viewController (the "pages" of your book) implement this protocol. That way the compiler will prevent you from accidentally creating a page that does not implement those method, and avoid a runtime crash.
最安全的方法是定义一个包含 audioFromPage stop 和 start 方法的协议,然后让每个 viewController(你的书的“页面”)实现这个协议。这样,编译器将防止您意外创建未实现这些方法的页面,并避免运行时崩溃。
回答by Basem Saadawy
There are two methods for beginning and finishing pages transitions:
开始和结束页面过渡有两种方法:
-(void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray *)pendingViewControllers
-(void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray *)pendingViewControllers
-(void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed
-(void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed