逐个动画UITableViewCells

时间:2013-04-12 10:45:32

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

我正在尝试使用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;

}

3 个答案:

答案 0 :(得分:3)

您需要为每个动画使用不同的延迟值。不使用.55的常量延迟,而是将单元格编号传递给animate方法,并使用类似的内容:

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

答案 1 :(得分: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()
}

答案 2 :(得分: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]];


}