2011-05-09 67 views
0

所以这是我的代码:的UIView动画不起作用

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    if (section == 0) { 
     if (!header) { 
      header = [[CustomBackground alloc] initWithFrame:CGRectMake(0, 0, 320, 30)]; 
      float number = [self.total floatValue]; 
      [self updateTotalLabel: &number]; 
     } 
     return header; 
    } 
    return nil; 
} 

-(void)updateTotalLabel:(float *)amount 
{ 
    float currentValue = [self.total floatValue]; 
    self.total = [NSNumber numberWithFloat:(currentValue + *amount)]; 
    [header updateLabel:[NSString stringWithFormat:TOTAL, [total floatValue]]]; 
} 

头updateLabel:

-(void)updateLabel:(NSString *)string 
{ 
    CGRect frame = self.frame; 
    frame.origin.y = -frame.size.height; 
    [UIView animateWithDuration:0.5 
          delay:0.1 
         options: UIViewAnimationCurveEaseOut 
        animations:^{ 
         self.frame = frame; 
        } 
        completion:^(BOOL finished){ 
         NSLog(@"Done!"); 
        }]; 
    self.frame = frame; 
    frame.origin.y = 0; 
    self.label.text = string; 
    [UIView animateWithDuration:0.5 
          delay:1.0 
         options: UIViewAnimationTransitionCurlDown 
        animations:^{ 
         self.frame = frame; 
        } 
        completion:^(BOOL finished){ 
         NSLog(@"Done!"); 
        }]; 

    [label setNeedsDisplay]; 
} 

我打电话updateTotalLabel,每次当用户添加新记录的tableview时间。 我有一个动画问题,因为动画只能在第一次调用updateLabel后才起作用。

编辑

好了,我建议电影:YT

在输出,你可以看到当每个动画触发。

回答

1

不确定你的问题是什么,因为实际上没有关于正在发生什么和未发生什么的描述。尽管如此,我确实看到了动画代码的问题。您正试图使用​​延迟来允许多个连续动画。它可能工作,我真的不知道。但是,更好的方法是使用完成块继续进行所需的动画。试试这个:

-(void)updateLabel:(NSString *)string 
{ 
    CGRect frame = self.frame; 
    frame.origin.y = -frame.size.height; 
    [UIView animateWithDuration:0.5 
          delay:0.1 
         options: UIViewAnimationCurveEaseOut 
        animations:^{ 
         self.frame = frame; 
        } 
        completion:^(BOOL finished){ 
         NSLog(@"Done 1!"); 
         frame.origin.y = 0; 
         self.label.text = string; 
         [UIView animateWithDuration:0.5 
               delay:0.0 
              options: UIViewAnimationTransitionCurlDown 
              animations:^{ 
               self.frame = frame; 
              } 
              completion:^(BOOL finished){ 
               NSLog(@"Done 2!"); 
               [label setNeedsDisplay]; 
              }]; 
        }]; 



} 
+0

好的,我更新了;) – juantorena 2011-05-09 18:37:36