-1

I'm trying to animate the change of an image in a UIImageView using transitionWithVIew. The image changes, but it doesn't seem to be animating. Not sure why this is happening.

Here's my code:

func changeBackgroundAtIndex(index : Int) {
        switch index {
        case 0:
            animateChangeWithImage(MLStyleKit.imageOfAboutMe)
        case 1:
            animateChangeWithImage(MLStyleKit.imageOfProjects)
        case 2:
            animateChangeWithImage(MLStyleKit.imageOfSkills)
        case 3:
            animateChangeWithImage(MLStyleKit.imageOfEducation)
        case 4:
            animateChangeWithImage(MLStyleKit.imageOfTheFuture)
        default:
            animateChangeWithImage(MLStyleKit.imageOfAboutMe)
        }
    }

    func animateChangeWithImage(image : UIImage) {
        UIView.transitionWithView(backgroundImageView, duration: 0.4, options: UIViewAnimationOptions.TransitionCrossDissolve, animations: { () -> Void in
            self.backgroundImageView.image = image
        }, completion: nil)
    }

Any ideas? Thanks :)

mlevi
  • 1,433
  • 3
  • 18
  • 30

3 Answers3

1

Change your function signature to this, where you take both the image you want to change and the UIImageView itself. func animateChangeWithImage(image: UIImage, inImageView: UIImageView)

Kelvin Lau
  • 6,373
  • 6
  • 34
  • 57
1

The problem was with index. I was getting the index as the uses was scrolling through a scroll view, which would constantly keep updating the index variable and not give it enough time to actually animate.

Putting changeBackgroundAtIndex inside scrollViewDidEndDecelerating instead of scrollViewDidScroll fixed it.

mlevi
  • 1,433
  • 3
  • 18
  • 30
0

It could be that the actual image itself is not animated when changed given that it is not a UIView.

This would seem to do what you want: How to animate the change of image in an UIImageView?

Community
  • 1
  • 1
Rory McKinnel
  • 7,936
  • 2
  • 17
  • 28