【问题标题】:Uiview animation doesn't workuiview动画不起作用
【发布时间】:2011-05-09 17:54:07
【问题描述】:

这是我的代码:

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

标题更新标签:

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

每当用户向 tableview 添加新记录时,我都会调用 updateTotalLabel。 我对动画有疑问,因为动画只有在第一次调用 updateLabel 后才能工作。

编辑

好的,所以我录制了一部电影:YT

在输出中,您可以看到每个动画的触发时间。

【问题讨论】:

    标签: iphone animation uiview


    【解决方案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];
                                              }];
                         }];
    
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 2011-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-01
      相关资源
      最近更新 更多