同步UITableViewCells的闪烁动画

时间:2015-04-15 09:11:58

标签: ios objective-c uitableview core-animation calayer

我希望特殊UITableViewCell s闪烁其边框。问题是我无法同步边框闪烁,我的细胞“随机”闪烁。 这是我尝试过的。我有self.borderAnimationStartDate来设置初始时间点。我将在显示单元格时添加闪烁动画,并尝试为单元格图层设置“时间偏移”(beginDate)。

- (void)tableView:(UITableView*)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // ...
    if (myCondition)
    {
         Z9EmptySlotCell* emptySlotCell = (Z9EmptySlotCell*)cell;
         emptySlotCell.isNew = YES;

         [emptySlotCell.backgroundCellView.layer addAnimation:self.borderAnimation forKey:@"color"];
         emptySlotCell.backgroundCellView.layer.beginTime = -[[NSDate date] timeIntervalSinceDate:self.borderAnimationStartDate];
        }
    }
}

- (CABasicAnimation*)borderAnimation
{
    if (!_borderAnimation)
    {
        _borderAnimation = [CABasicAnimation animationWithKeyPath:@"borderColor"];
        _borderAnimation.fromValue = (id)[UIColor colorWithRed:255.0/255.0 green:231.0/255.0 blue:153.0/255.0 alpha:1].CGColor;
        _borderAnimation.toValue   = (id)[UIColor clearColor].CGColor;
        _borderAnimation.duration = 2;
        _borderAnimation.repeatCount =  HUGE_VALF;
    }
    return _borderAnimation;
}

1 个答案:

答案 0 :(得分:0)

您还可以使用NSTimer实现此功能。

在viewController中,将NSTimer声明为属性或实例变量。我们称之为blinkTimer

blinkTimer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(blinkCells) userInfo:nil repeats:YES];
[blinkTimer fire];

blinkCells方法可以为每个可见单元设置动画,如下所示:

-(void)blinkCells
{
    NSArray *cells = [self.tableView visibleCells];

    for (UITableViewCell *cell in cells)
    {
        [cell.layer addAnimation:[self borderAnimation] forKey:@"color"];
    }
}

请务必从repeatCount方法中删除[self borderAnimation]行。

编辑:为了将动画应用到所有单元格,您需要像这样收集它们:

NSMutableArray *cells = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i < [self.tableView numberOfSections]; ++i)
{
    for (NSInteger j = 0; j < [self.tableView numberOfRowsInSection:i]; ++j)
    {
        [cells addObject:[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:j inSection:i]]];
    }
}

您最初可以收集它们并将单元格存储在属性或实例变量中。

相关问题