简体   繁体   English

UIView.transitionWithView不动画

[英]UIView.transitionWithView not animating

I'm trying to animate the change of an image in a UIImageView using transitionWithVIew . 我正在尝试使用transitionWithVIew为UIImageView中的图像变化设置动画。 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 :) 谢谢 :)

Change your function signature to this, where you take both the image you want to change and the UIImageView itself. 将函数签名更改为此,同时获取要更改的图像和UIImageView本身。 func animateChangeWithImage(image: UIImage, inImageView: UIImageView)

The problem was with index . 问题出在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. 我正在获得index因为用法是在滚动视图中滚动,这将不断更新index变量,而没有给它足够的时间进行动画。

Putting changeBackgroundAtIndex inside scrollViewDidEndDecelerating instead of scrollViewDidScroll fixed it. changeBackgroundAtIndex放置在scrollViewDidEndDecelerating而不是用scrollViewDidScroll修复。

It could be that the actual image itself is not animated when changed given that it is not a UIView. 鉴于实际的图像本身不是UIView,因此更改时可能没有对动画本身进行动画处理。

This would seem to do what you want: How to animate the change of image in an UIImageView? 这似乎可以满足您的要求: 如何为UIImageView中的图像变化设置动画?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM