简体   繁体   English

Uiview动画不起作用

[英]Uiview animation doesn't work

So it is my code: 所以这是我的代码:

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

header updateLabel: 标头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];
}

I am calling updateTotalLabel, every time when user adds new record to tableview. 每当用户将新记录添加到tableview时,我都会调用updateTotalLabel。 I have a problem with animation because, animation works only after first call updateLabel. 我对动画有问题,因为动画只能在首次调用updateLabel之后才能工作。

EDIT 编辑

Ok, so I rec a movie: YT 好吧,所以我看电影: YT

In output you can see when each animation is trigger. 在输出中,您可以看到何时触发每个动画。

Not really sure what your problem is, as there is really no description of what is happening and what is not happening. 不太确定您的问题是什么,因为实际上没有描述正在发生的事情和未发生的事情。 I do see a problem with your animation code though. 我确实看到了您的动画代码有问题。 You're attempting to use delays to allow for multiple continuous animations. 您正在尝试使用延迟来允许多个连续动画。 It might work, I really don't know. 这可能有用,我真的不知道。 However, a better method is just to use the completion block to continue with what ever animation you want. 但是,更好的方法是仅使用完成块来继续您想要的动画。 Try this: 尝试这个:

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



}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM