简体   繁体   中英

stop Alpha blending on UIImageView animation

I have a series of PNG files loaded as an array of UIImages and want to animate them. The idea is the PNG's have some opacity and I want to animate them showing the background through the transparent channels. 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. 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.

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. Instead of alpha I used:

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

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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