olavo.mayer
olavo.mayer

Reputation: 13

Xcode UIPageViewController - how to control behavior during transition?

I've been using the UIPageController template in Xcode to create an interactive book.

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.

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.

I've tried to add these lines to setViewControllers:direction:animated:completion: method as well as to the viewControllerAfterViewController and viewControllerBeforeViewController methods, but none of them have worked.

Do you guys have any idea on how to solve this problem?

Upvotes: 1

Views: 6726

Answers (2)

Basem Saadawy
Basem Saadawy

Reputation: 1818

There are two methods for beginning and finishing pages transitions:

-(void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray *)pendingViewControllers

-(void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed

Upvotes: 1

mmc
mmc

Reputation: 17414

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:

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

Likewise, you could also send a similar message to the [previousViewcontrollers objectAtIndex:0] a message to stop playing its sound.

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:

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.

Upvotes: 6

Related Questions