0

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

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

-(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; 

}

+0

'中心'应该在第一个块内部声明,并且你不需要'__block' – newacct

回答

3

您需要为每个动画使用不同的延迟值。而不是使用.55恒定的延迟,传递的细胞数的动画的方法,并使用类似:

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

非常感谢你! – jerry

0

试试这个

声明这个地方。

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


} 
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() 
}