UIView.animateWithDuration 滚动 uitableviewcell 后停止

UIView.animateWithDuration stops after scrolling uitableviewcell

我在 uiTableViewCell 中添加了一个无限动画,它只是在 table 视图单元格内闪烁一个 UILabel。

我的问题是,当我滚动 table 视图时,它只是停止闪烁

我的密码是

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("TripListCell", forIndexPath: indexPath) as! TripListCell
    
    let trip = tripList[indexPath.section]
    cell.lblTripDirection.textColor = UIColor(red: 51/255, green: 210/255, blue: 123/255, alpha: 1.0)
    
    UIView.animateWithDuration(0.5, delay: 0.0, options: [.CurveEaseInOut, .Repeat, .Autoreverse, .AllowUserInteraction], animations: {
        
        cell.lblTripDirection.alpha = 0.0
        }, completion: {
            bool in
        cell.lblTripDirection.alpha = 1.0
        cell.lblTripDirection.textColor = UIColor.blackColor()
    })
    return cell
}

更新:

UIView.commitAnimations() 在返回对我有用的单元格之前。 谢谢大家:)

您可以在自定义单元格中重写 UITableViewCell 的 prepareForReuse 方法 TripListCell

每次调用 dequeueReusableCellWithIdentifier 时都会调用

prepareForReuse

您应该将您的代码放在这个 UITableViewDelegate 方法中:

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath);

这是因为cellForRowAtIndexPath重复使用同一个单元格来显示其他数据。所以你不应该在 cellForRowAtIndexPath.

中编写你的动画

你应该尝试用custom cell classawakeFromNib写或者你应该用willDisplayCell的方法写动画。

希望这会有所帮助:)

TableView 重复使用它们的单元格,因此您的标签不可避免地停止了它们的动画。

您可以在自定义 table 视图单元格的 prepareForReuse:tableView:willDisplayCell:forRowAtIndexPath: delegate method.

中将重复使用的单元格恢复到默认状态

本例使用UIView动画,只需要改变动画的值属性取消动画,return恢复默认状态即可。

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {

    //declaring the cell variable again

    var cell: TripListCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! TripListCell
    UIView.performWithoutAnimation {
        cell.lblTripDirection.layoutIfNeeded()
    }
    // do your animation..
    ...
}

UIView.commitAnimations() 在返回单元格之前为我工作。