在UIImageView动画上停止Alpha混合

时间:2016-06-06 17:43:45

标签: ios animation uiimageview uiimage

我将一系列PNG文件作为UIImages数组加载,并希望为它们设置动画。这个想法是PNG有一些不透明度,我想通过透明通道动画显示背景。当我将UIImageView alpha设置为0(以显示背景)时,默认行为是UIImageView将其背景的alpha与UIImage相乘,所以现在我将动画的实体部分视为具有一定的透明度,当然有一个转变这种阿尔法混合行为的方法,如果不是它对苹果部分的壮观疏忽。当然,动画的基础是将动态图像分层叠加在背景之上?

var images : [UIImage] = [UIImage]()
    var i = 0
    for i = 0; i < 32; i += 1 {
        let image = UIImage(named: "boost_\(i)")
        images.append(image!)
    }
    imageView.alpha = 0
    imageView.animationImages = images
    imageView.animationDuration = 1
    imageView.animationRepeatCount = 1
    imageView.startAnimating     

enter image description here

图像/动画的黄色部分应该是实心的

3 个答案:

答案 0 :(得分:1)

您无需在UIImageView上将alpha设置为0.只需使图像视图的背景透明。

imageView.backgroundColor = UIColor. clearColor()
imageView.opaque = NO

答案 1 :(得分:1)

所以问题根本不是imageView,而是我在imageView(在viewController之上)下面有一个具有alpha值的UIView,而这正以某种方式传递给它的超级视图。而不是我使用的alpha:

UIView.backgroundColor = UIColor(white: 0, alpha 0.85) 

它似乎解决了这个问题。

答案 2 :(得分:0)

您可以使用以下内容:

            let transition = CATransition()

            transition.duration = 0.5
            transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)
            transition.type = kCATransitionFade

            self.imageView.layer.addAnimation(transition, forKey: nil)

更新(多张图片动画):

此代码完美无缺:

let timer  = NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: #selector(changeImages), userInfo: nil, repeats: true)
        timer.fire()


 func changeImages() { 

        if currentIndex == 4 {
            currentIndex = 0
        }
        let image = arrayImages[currentIndex]

        imageView.image = UIImage(named: image)
        let transition = CATransition()

        transition.duration = 1
        transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)
        transition.type = kCATransitionFade

        imageView.layer.addAnimation(transition, forKey: nil)

        currentIndex += 1
}