Ios 在swift中使用持续时间设置动画的问题
在animateWithDuration:animations:completion:method中使用块时出错 下面是我的代码:Ios 在swift中使用持续时间设置动画的问题,ios,swift,animation,Ios,Swift,Animation,在animateWithDuration:animations:completion:method中使用块时出错 下面是我的代码: func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { var cell=tableView.cellForRowAtIndexPath(indexPath) var backGrndView:UIView?=cell?.cont
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()
})
}
我尝试了一下解决方案
但是我的问题没有解决
以下是截图:
请帮帮我
提前感谢问题似乎在于,如果闭包中只有一条语句,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
,似乎问题在于,如果闭包中只有一条语句,swift会自动返回。您可以通过添加显式返回来解决此问题:
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
,似乎问题在于,如果闭包中只有一条语句,swift会自动返回。您可以通过添加显式返回来解决此问题:
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
,似乎问题在于,如果闭包中只有一条语句,swift会自动返回。您可以通过添加显式返回来解决此问题:
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
你在使用swift 3吗?@Cruz,我想我是。你在使用swift 3吗?@Cruz,我想我是。你在使用swift 3吗?@Cruz,我想我是。