UIView animateWithDuration没有动画,它只是加载

时间:2016-05-23 23:38:38

标签: ios animation uiview

我正在从网络上加载图片搜索结果,但它们似乎没有正确动画。为此,我只需将imageView的图像从nil更改为UIView.animateWithDuration(1.0){}调用中的海报。但它不是动画,而是立即弹出。

以下是代码:

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

2 个答案:

答案 0 :(得分:1)

您无法为image的{​​{1}}属性设置设置动画。由于您希望图像淡入,请为其UIImageView属性设置动画。

这样的事情:

alpha

答案 1 :(得分:1)

许多属性都可以设置动画,但不能image的{​​{1}}属性。 有关动画属性的更多信息:schema xsd