简体   繁体   中英

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 UIView.animateWithDuration(1.0){} call. But instead of animating, it just pops into place instantly.

Here's the code:

//Poster
//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
                }
            }
        }
    })

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

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
}

许多属性可以设置动画,但不能设置UIImageViewimage属性。动画属性的更多信息: https : //developer.apple.com/library/ios/documentation/Cocoa/Conceptual/CoreAnimation_guide/AnimatableProperties/AnimatableProperties.html

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