我在 animateWithDuration:animations:completion: 方法中使用 block 时出错
下面是我的代码:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var cell=tableView.cellForRowAtIndexPath(indexPath)
var backGrndView:UIView?=cell?.contentView.viewWithTag(2) as UIView?
UIView.animateWithDuration(0.2,
animations: {
backGrndView?.backgroundColor=UIColor.redColor()
},
completion: { finished in
backGrndView?.backgroundColor=UIColor.redColor()
})
}
我在 link 尝试了解决方案.
但是我的问题没有解决。
下面是截图:
请帮帮我。
提前致谢
最佳答案
问题似乎在于,如果闭包中只有一个语句,swift auto 会返回。您可以通过添加显式返回来解决此问题:
UIView.animate(withDuration: 0.2,
animations: {
backGrndView?.backgroundColor = UIColor.red
return
},
completion: { finished in
backGrndView?.backgroundColor = UIColor.red
return
})
这个声明:
backGrndView?.backgroundColor = UIColor.red
返回与Void?
相同的()?
,但闭包返回类型为Void
。
关于ios - swift 中持续时间的动画问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26062122/