Lizza
Lizza

Reputation: 2819

UIPageViewController transitions without the animation sometimes. Why?

I have a book app that uses the UIPageViewController to navigate it's content. For some reason, there are times when I turn the page (with the swipe gesture), and the transitions takes place, but without the animation. There is no page turn animation, but the new content is just there.

Why is that? Could it be because I have got some db calls going on in the background that could be messing with the UI animations?

Thanks

EDIT: Here is the code that handles the transitions

#pragma mark - UIPageViewControllerDataSource Methods

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController 
  viewControllerBeforeViewController:(UIViewController *)viewController
{
currentIndex = [self.modelArray indexOfObject:[(ContentViewController *)viewController labelContents]];
if(currentIndex == 0)
{
    return nil;
}
ContentViewController *contentViewController = [[ContentViewController alloc] init];
contentViewController.labelContents = [self.modelArray objectAtIndex:currentIndex - 1];

[data setCurrentPage:currentIndex-1];
return contentViewController;
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController
   viewControllerAfterViewController:(UIViewController *)viewController
{
currentIndex = [self.modelArray indexOfObject:[(ContentViewController *)viewController labelContents]];
if(currentIndex == self.modelArray.count-1)
{
    return nil;
}
ContentViewController *contentViewController = [[ContentViewController alloc] init];
contentViewController.labelContents = [self.modelArray objectAtIndex:currentIndex + 1];

[data setCurrentPage:currentIndex+1];


return contentViewController;
}

#pragma mark - UIPageViewControllerDelegate Methods

- (UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController *)pageViewController
               spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation
{
if(UIInterfaceOrientationIsPortrait(orientation))
{
    //Set the array with only 1 view controller
    UIViewController *currentViewController = [self.pageViewController.viewControllers objectAtIndex:0];
    NSArray *viewControllers = [NSArray arrayWithObject:currentViewController];
    [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:NULL];

    //Important- Set the doubleSided property to NO.
    self.pageViewController.doubleSided = NO;
    //Return the spine location
    return UIPageViewControllerSpineLocationMin;
}
else
{
    NSArray *viewControllers = nil;
    ContentViewController *currentViewController = [self.pageViewController.viewControllers objectAtIndex:0];

     currentIndex = [self.modelArray indexOfObject:[(ContentViewController *)currentViewController labelContents]];
    if(currentIndex == 0 || currentIndex %2 == 0)
    {
        UIViewController *nextViewController = [self pageViewController:self.pageViewController viewControllerAfterViewController:currentViewController];
        viewControllers = [NSArray arrayWithObjects:currentViewController, nextViewController, nil];
    }
    else
    {
        UIViewController *previousViewController = [self pageViewController:self.pageViewController viewControllerBeforeViewController:currentViewController];
        viewControllers = [NSArray arrayWithObjects:previousViewController, currentViewController, nil];
    }
    //Now, set the viewControllers property of UIPageViewController
    [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:NULL];

    return UIPageViewControllerSpineLocationMid;
}
}

Upvotes: 2

Views: 2278

Answers (1)

Tung Dao
Tung Dao

Reputation: 11

I think it's because of your line:

[data setCurrentPage:currentIndex-1]; 

in your viewControllerBeforeViewController: and viewControllerAfterViewController: function.

These function only need to return an appropriate viewController. The job of change the current controller and playing animation is not yours but the UIPageViewControllerDataSource delegate. if you set it yourself, there will be no animation

Upvotes: 1

Related Questions