Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

alaahershy

macrumors member
Original poster
Aug 19, 2013
38
0
I create a view controller and i put a UIImageView in it, I made an NSArray that contains the images and I passed this NSarray in the UIImageView, and I added a UISwipeGestureRecognizer for left an right swiping.

This code works but the images come when swiping like boom without effects or transition, is there a way to add a simple transition between them?

Here is the code:
Code:
NSArray *images = [[NSArray alloc] initWithObjects:
                 @"1-r.png",
                 @"2-r.png",
                 @"3-r.png",
                 @"4-r.png",
                 @"5-r.png",
                 nil];

UISwipeGestureRecognizerDirection direction = [(UISwipeGestureRecognizer *)sender direction];

switch (direction) {
    case UISwipeGestureRecognizerDirectionLeft:
        imageIndex++;
        break;
    case UISwipeGestureRecognizerDirectionRight:
        imageIndex--;
        break;

    default:
        break;
}

imageIndex = (imageIndex < 0) ? ([images count] - 1) : imageIndex % [images count];
_imageView.image = [UIImage imageNamed:[images objectAtIndex:imageIndex]];

Thanks in Advance
 
Last edited by a moderator:
I create a view controller and i put a UIImageView in it, I made an NSArray that contains the images and I passed this NSarray in the UIImageView, and I added a UISwipeGestureRecognizer for left an right swiping.

This code works but the images come when swiping like boom without effects or transition, is there a way to add a simple transition between them?

Here is the code:
Code:
NSArray *images = [[NSArray alloc] initWithObjects:
                 @"1-r.png",
                 @"2-r.png",
                 @"3-r.png",
                 @"4-r.png",
                 @"5-r.png",
                 nil];

UISwipeGestureRecognizerDirection direction = [(UISwipeGestureRecognizer *)sender direction];

switch (direction) {
    case UISwipeGestureRecognizerDirectionLeft:
        imageIndex++;
        break;
    case UISwipeGestureRecognizerDirectionRight:
        imageIndex--;
        break;

    default:
        break;
}

imageIndex = (imageIndex < 0) ? ([images count] - 1) : imageIndex % [images count];
_imageView.image = [UIImage imageNamed:[images objectAtIndex:imageIndex]];

Thanks in Advance

You could try using CATransition from the QuartzCore libraries? I don't know the exact implementation off the top of my head but I know there are some Apple example projects which probably have the code you need.
 
An image viewer is usually implemented with a scrollview. No transitions are required.

Apple has sample code for this. Look at PhotoScroller, StreetScroller, Scrolling etc.

If you know that you'll have a small number of images you could just build a scrollview with appropriate number of imageviews and add the images.

The most efficient way is to have a scrollview with three image views and you populate the three image views with the previous, current and next image to be displayed.
 
You could set the transparency with the touches moved on the y axis,
or from images count


Code:
int oldcount
int imagecount
float alpha = 0.0;



// somewhere in a loop
if (oldcount != imagecount) {alpha = -0.1;}
if (alpha < 1.0) {alpha = alpha + 0.1;}
oldcount = imagecount;
 
just come across this a few days back.
implemented as a UIImageView category.

.h file

Code:
@interface UIImageView (LPPAdditions)

- (void)lpp_animatedSetImage:(UIImage *)image;

@end

.m file

Code:
@implementation UIImageView (LPPAdditions)

- (void)lpp_animatedSetImage:(UIImage *)image {

    [UIView transitionWithView:self
                      duration:.2
                       options:UIViewAnimationOptionTransitionCrossDissolve
                    animations:^ { self.image = image; }
                    completion:nil];

}

@end
 
The cross-dissolve is one animation that one might consider for this but I don't think it works well with a swipe.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.