我如何在单元格加载时淡入淡出?
How would I fade in cells as they load?
目前我正在使用 class 这种方法。
class TipInCellAnimator {
// placeholder for things to come -- only fades in for now
class func animate(cell:UITableViewCell) {
let view = cell.contentView
view.layer.opacity = 0.1
UIView.animateWithDuration(0.1) {
view.layer.opacity = 1
}
}
}
并在我的主 viewcontroller 中调用它,如下所示
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell,
forRowAtIndexPath indexPath: NSIndexPath) {
TipInCellAnimator.animate(cell)
}
我遇到的问题是,当项目重新加载时,单元格会闪烁,因为它正在淡入,我无法点击屏幕来停止滚动。
有没有人对如何解决这些问题或达到相同效果的其他方法有一些提示?
通过查看您的代码,"flashing" 个单元格的最明显原因是动画的持续时间。尝试将持续时间从 0.1 增加到 0.5
对于第二个问题,iOS 默认情况下会在其视图动画时忽略用户交互。要启用此功能,请将 UIViewAnimationOptions 中的选项设置为 "AllowUserInteraction".
Swift1.2
请参考以下代码
class func animate(cell:UITableViewCell) {
let view = cell.contentView
view.layer.opacity = 0.1
UIView.animateWithDuration(0.5, delay: 0, options: .AllowUserInteraction | .CurveEaseInOut, animations: { () -> Void in
view.layer.opacity = 1
}, completion: nil)
}
或 Swift 2.0
class func animate(cell:UITableViewCell) {
let view = cell.contentView
view.layer.opacity = 0.1
UIView.animateWithDuration(0.5, delay: 0, options: [.AllowUserInteraction, .CurveEaseInOut], animations: { () -> Void in
view.layer.opacity = 1
}, completion: nil)
}
目前我正在使用 class 这种方法。
class TipInCellAnimator {
// placeholder for things to come -- only fades in for now
class func animate(cell:UITableViewCell) {
let view = cell.contentView
view.layer.opacity = 0.1
UIView.animateWithDuration(0.1) {
view.layer.opacity = 1
}
}
}
并在我的主 viewcontroller 中调用它,如下所示
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell,
forRowAtIndexPath indexPath: NSIndexPath) {
TipInCellAnimator.animate(cell)
}
我遇到的问题是,当项目重新加载时,单元格会闪烁,因为它正在淡入,我无法点击屏幕来停止滚动。
有没有人对如何解决这些问题或达到相同效果的其他方法有一些提示?
通过查看您的代码,"flashing" 个单元格的最明显原因是动画的持续时间。尝试将持续时间从 0.1 增加到 0.5
对于第二个问题,iOS 默认情况下会在其视图动画时忽略用户交互。要启用此功能,请将 UIViewAnimationOptions 中的选项设置为 "AllowUserInteraction".
Swift1.2
请参考以下代码class func animate(cell:UITableViewCell) {
let view = cell.contentView
view.layer.opacity = 0.1
UIView.animateWithDuration(0.5, delay: 0, options: .AllowUserInteraction | .CurveEaseInOut, animations: { () -> Void in
view.layer.opacity = 1
}, completion: nil)
}
或 Swift 2.0
class func animate(cell:UITableViewCell) {
let view = cell.contentView
view.layer.opacity = 0.1
UIView.animateWithDuration(0.5, delay: 0, options: [.AllowUserInteraction, .CurveEaseInOut], animations: { () -> Void in
view.layer.opacity = 1
}, completion: nil)
}