Search code examples
iosanimationuipageviewcontrollerphoto-gallery

UIPageViewController shows an animation in the wrong direction when deleting the last photo


I'm using UIPageViewController with transition style swipe, to implement a photo gallery. As the user swipes through photos, she can delete an individual photo, in which case the gallery goes to the next photo, along with an animation. This works as expected.

If the last photo is deleted, we go to the photo before the deleted one. This also works as expected, but the animation is wrong — it looks as if we're going to the next photo.

My code is:

let photo = (viewControllers!.first as! SinglePhotoController).photo
var nextPhoto = Photo.after(photo: photo)
if nextPhoto == nil {
  nextPhoto = Photo.before(photo: photo)
  print("You deleted the last photo, so showing previous photo")
}
photo.delete {
  let nextController = self.makeController(photo: nextPhoto!)
  self.setViewControllers([nextController], direction: .forward, animated: true)
}

photo.delete() is a method that deletes the photo and invokes the callback on the main thread.

How do I invoke setViewControllers() with a different animation?

This is on iOS 10.


Solution

  • Just use direction: .reverse when deleting the last photo.

    The effect of this is transient — once the animation triggered by setViewControllers() completes, it no longer has an effect. In other words, the direction of navigation remains unchanged: if you have A, B, and C, and you delete C, you'll end up with A and B as intended, not B and A.