UIView.transitionWithView没有动画

时间:2015-04-22 17:29:01

标签: ios swift uiview

我正在尝试使用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)
    }

有什么想法吗?谢谢:))

3 个答案:

答案 0 :(得分:1)

将您的功能签名更改为此选项,您可以同时获取要更改的图像和UIImageView本身。     func animateChangeWithImage(image: UIImage, inImageView: UIImageView)

答案 1 :(得分:1)

问题在于index。我正在使用index,因为用户正在滚动滚动视图,这会不断更新index变量并且没有给它足够的时间来实际制作动画。

changeBackgroundAtIndex放在scrollViewDidEndDecelerating内而不是scrollViewDidScroll修复它。

答案 2 :(得分:0)

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

这似乎可以做你想做的事:How to animate the change of image in an UIImageView?

相关问题