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

标签 ios swift animation

我在 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/

相关文章:

ios - 如何将外接相机连接到 iPhone 并拍摄/下载照片?

ios - 让条件在 Firebase Observer 中工作

android - 使用 Rebound 库向 ImageView 添加与 Facebook Messenger 聊天头像相同的自然拖动效果

javascript - 使用angularjs将一个盒子移动到一个随机位置

ios - 调用中的额外参数 "animated"- 错误

objective-c - UIScrollView 不更新

ios - 从 NSDictionary 获取值

ios - 如何显示位于 TableView 之外且可滚动的父 View ?

ios - Telnet 得到空输出并且没有登录名/密码

css - 网站上的动画