简体   繁体   中英

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:

-(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. I have a problem with animation because, animation works only after first call updateLabel.

EDIT

Ok, so I rec a movie: 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];
                                          }];
                     }];



}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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