UIImage slide on/off inside UIImageView iOS

731 views Asked by At

I have a UIImageView in the center of the screen (Position Set with the help of the constraints through the storyboard) In the code's viewDidLoad method I set the image for that imageView by:

self.imageView.image = [UIImage imageNamed:@"food"];

Then I wanted to slide the old image off from left of the UIImageView and slide the new image on from right of the UIImageView for which i have used the following code(The code below is triggered after the swipe gesture is recognized, so the code below is inside the selector method for the swipeGestureRecognizer):

self.imageView.image = [UIImage imageNamed:@"picnic"];
CATransition *animation = [CATransition animation];
[animation setDuration:1];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromRight];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[[self.imageView layer] addAnimation:animation forKey:nil];

But when I do this the new image appears on the screen from the right edge of the screen and not the right edge of the UIImageView and same with the old image which is pushed out all the way to the left edge of the screen and not the left edge of the UIImageView. Is there a way I can fix it? Any help would be appreciated. Thanks

2

There are 2 answers

0
DimasSup On BEST ANSWER

You can try put image view into view which too have clip to bounds YES. I think this should help.

0
Display Name On

Swift version of the self-answering question (I was only looking for the animation):

    self.image = newImage
    let animation = CATransition()
    animation.duration = 0.3
    animation.type = .push
    animation.subtype = .fromRight
    animation.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
    layer.add(animation, forKey: nil)