5

I have a UIImageView as one of my tabbarcontroller views, and I've added a UISwipeGestureRecognizer (right and left) to navigate through a series of photos in an NSArray.

However, when swiping it jumps from photo to photo with no smooth transition. I'm not after anything fancy, but even a slide animation would be okay, like as one photo slides in, the other is sliding out the other side. Is this possible with the UISwipeGestureRecgonizer and an NSArray?

idmean
  • 14,540
  • 9
  • 54
  • 83
JamesBent
  • 65
  • 1
  • 4

2 Answers2

13

The standard solution is to use a UIScrollView with pagingEnabled = YES and multiple image views inside the scroll view. If you want to work with just one UIImageView, add a transition animation to the image view's layer when you recognize a swipe:

CATransition *animation = [CATransition animation];
animation.duration = 0.5;
animation.type = kCATransitionMoveIn;
animation.subtype = kCATransitionFromRight;
[myImageView.layer addAnimation:animation forKey:@"imageTransition"];
myImageView.image = newImage;
Ole Begemann
  • 135,006
  • 31
  • 278
  • 256
  • Thanks - I think the UIScrollview works okay, except I have over 60 images, so may look at the transition animation instead. One concern tho - does this way use up more memory instead of having lots of different UIImageViews in a UIScrollView, or does the ImageView release in my -(void)dealloc release each photo from the memory as it's swiped through? – JamesBent Feb 20 '11 at 15:10
  • You can have at most two images loaded at a time if you do the UIScrollView-based transition the right way. Configure the contentsView of the UIScrollView so that it can hold two images, then load each image when it is about to be shown and release the one that has gone away. There are plenty of examples of how to do this on the net. And you can check out the scrollviews session from WWDC'2k10 (available somewhere in Itunes U) – kervich Apr 03 '11 at 18:39
0

Why do it this way? Why not just use a UIScrollView with pagingEnabled set to YES? Place your image views inside the scrollview, make sure your contentSize's height is the height of the view you're placing the scroll view in, and your width is the width of all your image views side by side, so you can scroll.

jer
  • 20,094
  • 5
  • 45
  • 69
  • If you have 100 images, it will kill many devices because of lack of memory. If you have 10000 images, it will kill all...... – Jonny Jul 13 '11 at 16:46
  • 2
    No it won't. You create independent subviews on the scrollview which act as pages, in much the same way rows do in a tableview. You maintain two sets, a visible set and a recycled set where you add / remove these pages. Your contentSize still must be rather large, but that takes up no memory. The contentSize is not the size of the view, it's the marker for the system to know when scrolling should stop. – jer Jul 13 '11 at 19:35