gpt4 book ai didi

ios - swift 中持续时间的动画问题

转载 作者:行者123 更新时间:2023-11-28 11:22:42 24 4
gpt4 key购买 nike

我在 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 尝试了解决方案.

但是我的问题没有解决。

下面是截图:

enter image description here

请帮帮我。

提前致谢

最佳答案

问题似乎在于,如果闭包中只有一个语句,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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com