当它们加载时我将如何淡入细胞?

Jam*_*hen 0 scroll fade tableview ios swift

目前我正在使用这种方法的类.

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
    }
}
}
Run Code Online (Sandbox Code Playgroud)

并在我的主viewcontroller中调用它,如下所示

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell,
       forRowAtIndexPath indexPath: NSIndexPath) {
            TipInCellAnimator.animate(cell)
    }
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是,当重新加载项目时,单元格会闪烁,因为它正在淡入,我无法点击屏幕来停止滚动.

有没有人有关于如何解决这些问题或其他方法达到相同效果的一些提示?

小智 5

通过查看代码,"闪烁"单元格的最明显原因是因为动画的持续时间.尝试将持续时间从0.1增加到0.5

对于第二个问题,iOS默认情况下会在其视图为动画时忽略用户交互.要启用此功能,请将UIViewAnimationOptions中的选项设置为"AllowUserInteraction".

请参阅以下代码以获取Swift 1.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)
}
Run Code Online (Sandbox Code Playgroud)

或者在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)
}
Run Code Online (Sandbox Code Playgroud)

  • 另外,请不要忘记取消任何现有的动画,因为这些单元格可以重复使用.或者使用.BeginFromCurrentState动画选项. (3认同)