简体   繁体   English

停止在UIImageView动画上进行Alpha混合

[英]stop Alpha blending on UIImageView animation

I have a series of PNG files loaded as an array of UIImages and want to animate them. 我有一系列作为UIImages数组加载的PNG文件,并希望对其进行动画处理。 The idea is the PNG's have some opacity and I want to animate them showing the background through the transparent channels. 这个想法是PNG有点不透明,我想通过透明通道为它们设置背景动画。 When I set the UIImageView alpha to 0 (to show the background through) the default behaviour is the UIImageView multiplies the alpha of it's background with the UIImage, so now Im getting the solid part of the animation as having some transparency, surely there is a way to turn of this alpha blending behaviour, if not its a spectacular oversight on apples part. 当我将UIImageView alpha设置为0(以显示背景)时,默认行为是UIImageView将其背景的alpha与UIImage相乘,因此现在我获得了具有一定透明度的动画的坚实部分,肯定有一个改变这种alpha混合行为的方法,即使不是苹果方面的一项重大监督。 Surely the basis of animation is layering moving images on top of backgrounds? 当然,动画的基础是在背景上叠加运动图像吗?

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     

在此处输入图片说明

the yellow part of the image/animation should be solid 图像/动画的黄色部分应稳定

You don't need to set alpha on UIImageView to 0. Just make the background of the image view transparent. 您无需将UIImageView上的alpha设置为0。只需使图像视图的背景透明即可。

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

So the problem wasn't the imageView at all, it was the that I had a UIView underneath the imageView (on top of the viewController) that had an alpha value, and this was being somehow passed on to its superviews. 所以问题根本就不是imageView,而是我在imageView下方(在viewController顶部)有一个具有alpha值的UIView,并且以某种方式将其传递给其superviews。 Instead of alpha I used: 我使用的不是alpha,而是:

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

and it seemed to solve the problem. 它似乎解决了问题。

You can use something like this: 您可以使用如下形式:

            let transition = CATransition()

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

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

UPDATE(Multiple images Animate): UPDATE(多张图片动画):

This code works perfectly: 这段代码可以完美地工作:

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
}

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

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