UIImageView Transition

Discussion in 'iOS Programming' started by alaahershy, Sep 8, 2013.

  1. alaahershy, Sep 8, 2013
    Last edited by a moderator: Sep 8, 2013

    alaahershy macrumors member

    Joined:
    Aug 19, 2013
    #1
    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
     
  2. Twimfy macrumors 6502a

    Twimfy

    Joined:
    Sep 11, 2011
    Location:
    UK
    #2
    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.
     
  3. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #3
    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.
     
  4. xArtx macrumors 6502a

    Joined:
    Mar 30, 2012
    #4
    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;
    
    
    
     
  5. Punicasoft macrumors member

    Joined:
    Jul 27, 2013
    #5
    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
     
  6. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #6
    The cross-dissolve is one animation that one might consider for this but I don't think it works well with a swipe.
     

Share This Page