Rafique
Rafique

Reputation: 5

UIImageView animation in Swift with time delay

I'm stuck with this issue where I want a UIImage to glow within two seconds and then to go back to it's normal state.

Given this issue here's what I have at the moment:

I have the image views referenced, from 0 to 9.

@IBOutlet weak var imageOne: UIImageView!
@IBOutlet weak var imageTwo: UIImageView!

etc.

Then I added them to SubViews in the viewDiDLoad() function:

override func viewDidLoad() {
        super.viewDidLoad()
        self.view.addSubview(imageOne)
        self.view.addSubview(imageTwo)
        etc.
}

Here is my implementation of the colorisation function:

func colorize(imageView: UIImageView, color: CGColorRef) {
        imageView.layer.shadowColor = color
        imageView.layer.shadowRadius = 7.0
        imageView.layer.shadowOpacity = 0.9
        imageView.layer.shadowOffset = CGSizeZero
        imageView.layer.masksToBounds = false
}

Now, I'm trying to animate two image views which are called through this. a and b are just randoms acquired from the previous view. In this example, they will be 1 and 3.:

UIView.animateWithDuration(2.0, delay: 0, options: nil, animations: { () -> Void in
        self.colorize(labelArray[a.toInt()!], color:self.green)
}, completion: nil)

UIView.animateWithDuration(2.0, delay: 2.0, options: nil, animations: { () -> Void in
        self.colorize(labelArray[b.toInt()!], color:self.cyan)
}, completion: nil)

Now, this is what the view looks like beforehand -

enter image description here

And this is what it's like after, but both animations occur at the same time. There is no transition. It just automatically applies the glow -

enter image description here

Thanks for your help!

Upvotes: 0

Views: 2683

Answers (1)

Lyndsey Scott
Lyndsey Scott

Reputation: 37290

Since you're trying to animation your UIImageView layers, try using Core Animation instead of UIView animation blocks since Core Animation must be used to animate the shadow layer of a view. If you simply want to fade the shadow in, try this to animate the shadow opacity:

override func performGlowAnimations {

    self.colorize(labelArray[a.toInt()!], color:self.green)

    // Delay the second call by 2 seconds
    let delay = 2.0 * Double(NSEC_PER_SEC)
    var time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
    dispatch_after(time, dispatch_get_main_queue(), {
        self.colorize(labelArray[b.toInt()!], color:self.cyan)
    })
}

func colorize(imageView: UIImageView, color: CGColorRef) {

    // Set the image's shadowColor, radius, offset, and
    // set masks to bounds to false
    imageView.layer.shadowColor = color
    imageView.layer.shadowRadius = 7.0
    imageView.layer.shadowOffset = CGSizeZero
    imageView.layer.masksToBounds = false

    // Animate the shadow opacity to "fade it in"
    let shadowAnim = CABasicAnimation()
    shadowAnim.keyPath = "shadowOpacity"
    shadowAnim.fromValue = NSNumber(float: 0.0)
    shadowAnim.toValue = NSNumber(float: 0.9)
    shadowAnim.duration = 2.0

    imageView.layer.addAnimation(shadowAnim, forKey: "shadowOpacity")
    imageView.layer.shadowOpacity = 0.9
}

Upvotes: 4

Related Questions