UIView.transitionWithView 没有动画

UIView.transitionWithView not animating

我正在尝试使用 transitionWithVIew 为 UIImageView 中的图像变化设置动画。图像发生变化,但它似乎没有动画效果。不确定为什么会这样。

这是我的代码:

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)
    }

有什么想法吗?谢谢:)

可能是因为实际图像本身不是 UIView,所以在更改时它本身没有动画。

这似乎可以满足您的要求:How to animate the change of image in an UIImageView?

将您的函数签名更改为此,您可以在其中获取要更改的图像和 UIImageView 本身。 func animateChangeWithImage(image: UIImage, inImageView: UIImageView)

问题出在 index。我得到 index 是因为用户在滚动视图中滚动,这会不断更新 index 变量并且没有给它足够的时间来实际动画。

changeBackgroundAtIndex 放入 scrollViewDidEndDecelerating 而不是 scrollViewDidScroll 修复了它。