【问题标题】:Synchronize blink animation for UITableViewCells为 UITableViewCells 同步闪烁动画
【发布时间】:2015-04-15 09:11:58
【问题描述】:

我想让特殊的UITableViewCells 闪烁它的边框。问题是我无法同步边框闪烁并且我的单元格“随机”闪烁。 这是我尝试过的。我有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;
}

【问题讨论】:

  • 如何设置self.borderAnimationStartDate?
  • ZeMoon,现在我在viewDidLoad 中将其设置为[NSDate date]。将来我计划在添加一些视图后设置它的值
  • 你必须大量使用通知来实现这一点。这是唯一的方法。

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


【解决方案1】:

您也可以通过使用 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"];
    }
}

确保从[self borderAnimation] 方法中删除repeatCount 行。

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

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]]];
    }
}

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

【讨论】:

  • 抱歉,您的解决方案不起作用。我认为,您的解决方案使用与我相同的逻辑,但您使用 CFTimeInterval 而不是 NSTimeInterval。两者都是double,所以在滚动 tableView 后看到异步闪烁:(
  • 不同之处在于传递的值也是如此。您可以记录值以查看它。很遗憾听到它不起作用。还有另一种方法。您可以使用 NSNotificationCenter 和 NSTimer 使每个单元格闪烁。我将使用此功能编辑我的答案。
  • 我还标记了我所有单元格的 beginTime 都是相同的。但闪烁是异步的
  • 现在只有 visibleCells 看起来不错。但是在滚动下一组单元格后等待 2 秒完成
  • 嗯,那么动画需要应用到所有的单元格上。
猜你喜欢
  • 2011-12-02
  • 2012-03-12
  • 2011-03-30
  • 2011-08-23
  • 1970-01-01
  • 1970-01-01
  • 2020-09-19
相关资源
最近更新 更多