Uiview动画不起作用

发布于 2024-11-06 04:19:46 字数 1889 浏览 1 评论 0原文

所以这是我的代码:

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

每次用户向表视图添加新记录时,我都会调用 updateTotalLabel 。 我对动画有疑问,因为动画仅在第一次调用 updateLabel 后才起作用。

编辑

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

在输出中,您可以看到每个动画何时播放扳机。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

舞袖。长 2024-11-13 04:19:46

不太确定你的问题是什么,因为实际上没有描述正在发生的事情和没有发生的事情。不过,我确实发现您的动画代码有问题。您正在尝试使用延迟来允许多个连续动画。它可能会起作用,我真的不知道。然而,更好的方法是使用完成块来继续您想要的动画。试试这个:

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



}

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



}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文