QuantumHoneybees QuantumHoneybees - 1 year ago 48
iOS Question

UIView animateWithDuration doesn't animate, it just loads

I'm loading image search results from the web but they don't seem to be animating correctly. To do so, I just change the imageView's image from nil to the poster inside a

call. But instead of animating, it just pops into place instantly.

Here's the code:

//Set the poster first as blank
cell.poster.image = nil
//If I'm provided with a poster path, load an image.
if let imagePath = matchingItems[indexPath.row]["poster_path"] as? String {
//Sessions and Stuff for request
let url = NSURL(string: "http://i.imgur.com/b3DKTX2.png")
let urlRequest = NSURLRequest(URL: url!)
let session = NSURLSession.sharedSession()

//Asynchronous Request:
let dataTask = session.dataTaskWithRequest(urlRequest, completionHandler: { (data, response, error) -> Void in
if let poster = UIImage(data: data!) {
dispatch_async(dispatch_get_main_queue()) {
//Animate the poster in
UIView.animateWithDuration(1.0) {
cell.poster.image = poster

} else {
//Just use placeholder if no image exists
cell.poster.image = UIImage(named: "Placeholder.jpg")

Answer Source

You can't animate the setting of the image property of a UIImageView. Since you want the image to fade in, animate its alpha property.

Something like this:

cell.poster.alpha = 0.0 // hide it
cell.poster.image = poster // set it
UIView.animateWithDuration(1.0) {
    cell.poster.alpha = 1.0 // fade it in