Md1079
Md1079

Reputation: 1360

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     

enter image description here

the yellow part of the image/animation should be solid

Upvotes: 0

Views: 394

Answers (3)

Md1079
Md1079

Reputation: 1360

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.

Upvotes: 1

Bhavuk Jain
Bhavuk Jain

Reputation: 2187

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
}

Upvotes: 0

Sonny Saluja
Sonny Saluja

Reputation: 7287

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

Upvotes: 1

Related Questions