滚动uitableviewcell后,UIView.animateWithDuration停止

时间:2016-05-19 07:12:23

标签: ios swift tableview tableviewcell uianimation

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

我的问题是,当我滚动桌面视图时,它会停止闪烁

我的代码是

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()为我工作。 谢谢大家:)

5 个答案:

答案 0 :(得分:1)

您可以在自定义单元格cout<<"enter 'data' for list: "; // 'd' is an Integer variable cin>>d; node *tmp = new node(d, 0, head); head = tmp; 中覆盖UITableViewCell的prepareForReuse方法。

每次调用TripListCell时都会调用{p> prepareForReuse

答案 1 :(得分:1)

这是因为cellForRowAtIndexPath重新使用相同的单元格来显示其他数据。所以你不能在cellForRowAtIndexPath中写下你的动画。

您应该尝试写awakeFromNib custom cell class或者使用willDisplayCell方法来编写动画。

希望这会有所帮助:)

答案 2 :(得分:0)

您应该将代码放在此UITableViewDelegate方法中:

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

答案 3 :(得分:0)

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

您可以在自定义表格视图单元格的prepareForReuse:tableView:willDisplayCell:forRowAtIndexPath: delegate method中将重复使用的单元格恢复为默认状态。

在这种情况下,您使用UIView动画,您只需要更改动画属性的值以取消动画并返回默认状态。

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..
    ...
}

答案 4 :(得分:0)

UIView.commitAnimations()为我工作。