【问题标题】:Animating UITableViewCells one by one一个一个地为 UITableViewCells 设置动画
【发布时间】:2013-04-12 10:45:32
【问题描述】:

我正在尝试动画,其中 UITableView 已经到位,但是每个 UITableViewCells 的子视图(它本身包含单元格的内容视图)都在窗口边界之外。它在屏幕之外。当视图加载时,单元格从左到右,一个一个地动画,直到框架的最右边的 x 值接触到 UITableView 的最右边的 x 值。

动画是这样的,一旦第一个单元起飞,第二个单元在第一个单元完成动画之前起飞。同样,第三个单元格在第二个单元格完成动画之前起飞。但是,我无法做到这一点,并且我所有的行都动画在一起。

-(void)animateStatusViewCells:(SupportTableViewCell *)cell
{
__block CGPoint center;
[UIView animateWithDuration:0.55 delay:0.55 options:UIViewAnimationOptionCurveEaseIn animations:^{
    center = cell.statusView.center;
    center.x += (cell.frame.size.width - 20);
    cell.statusView.center = center;
}completion:^(BOOL success){
    NSLog(@"Translation successful!!!");
    double delayInSeconds = 2.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        //[self animateStatusViewCells:cell];
    });
}];
}


 -(void)animateSupportTableView:(UITableView *)myTableView
 {
   NSMutableArray *cells = [[NSMutableArray alloc] init];
   NSInteger count = [myTableView numberOfRowsInSection:0];

 for (int i = 0; i < count; i++) {
    [cells addObject:[myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]]];
}

for (SupportTableViewCell *cell in cells) {

    [self animateStatusViewCells:cell];
   }

}

-(void)viewDidAppear:(BOOL)animated{
   [super viewDidAppear:animated];
   [self animateSupportTableView:_statusTableView];
 }

这是自定义 UITableViewCell 的 initWithStyle 方法:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
 {
 self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
 if (self) {
    // Initialization code
    _statusView = [[UIView alloc] initWithFrame:self.frame];
    self.contentView.backgroundColor = [UIColor greenColor];
    self.contentView.alpha = 0.7;
    CGPoint cellCenter = self.center;
    cellCenter.x = self.center.x -  (self.frame.size.width - 20);
    _statusView.center = cellCenter;

    [self addSubview:_statusView];
    [_statusView addSubview:self.contentView];
 }
 return self;

}

【问题讨论】:

  • center 应该在第一个块内声明,你不需要__block

标签: objective-c ios5 core-animation objective-c-blocks uiviewanimation


【解决方案1】:

您需要为每个动画使用不同的延迟值。不要使用 0.55 的恒定延迟,而是将单元格编号传递给您的 animate 方法,并使用以下内容:

CGFloat delay = .55 + cellNumber * cellDelay;
[UIView animateWithDuration: 0.55 delay: delay options:UIViewAnimationOptionCurveEaseIn animations:
^{
  //animation code
}
];

【讨论】:

    【解决方案2】:

    不管怎样,这是一个干净的解决方案:

    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell,     forRowAtIndexPath indexPath: NSIndexPath) {
        let trans = CATransform3DMakeTranslation(-cell.frame.size.width, 0.0, 0.0)
        cell.layer.transform = trans
    
        UIView.beginAnimations("Move", context: nil)
        UIView.setAnimationDuration(0.3)
        UIView.setAnimationDelay(0.1 * Double(indexPath.row))
        cell.layer.transform = CATransform3DIdentity
        UIView.commitAnimations()
    }
    

    【讨论】:

      【解决方案3】:

      试试这个

      在某处声明。

      int c=0;
      
      
      -(void)animateStatusViewCells:(SupportTableViewCell *)cell
      {
      __block CGPoint center;
      [UIView animateWithDuration:0.55 delay:0.55 options:UIViewAnimationOptionCurveEaseIn animations:^{
          center = cell.statusView.center;
          center.x += (cell.frame.size.width - 20);
          cell.statusView.center = center;
      }completion:^(BOOL success){
      c++;
      if(c<[cells count]){
      [self animateStatusViewCells:[cells objectAtIndex:c]];
      }
      }];
      }
      
      
      
      -(void)animateSupportTableView:(UITableView *)myTableView
       {
         NSMutableArray *cells = [[NSMutableArray alloc] init];
         NSInteger count = [myTableView numberOfRowsInSection:0];
      
       for (int i = 0; i < count; i++) {
          [cells addObject:[myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]]];
      }
      
         [self animateStatusViewCells:[cells objectAtIndex:c]];
      
      
      }
      

      【讨论】:

        猜你喜欢
        • 2015-03-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-26
        • 2016-12-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多