如何在viewDidAppear中给UIView添加动画?

发布于 2024-08-19 17:09:45 字数 279 浏览 9 评论 0原文

我尝试向 viewDidLoad 和 viewDidAppear 添加动画,但它不起作用:

- (void)viewDidAppear:(BOOL)animated{
 [UIView beginAnimations:@"transition" context:NULL];
 [UIView setAnimationTransition:110 forView:self.view cache:YES];
 [UIView commitAnimations];
}

为什么?

I tried to add a animation to viewDidLoad and viewDidAppear, but it doesn't work:

- (void)viewDidAppear:(BOOL)animated{
 [UIView beginAnimations:@"transition" context:NULL];
 [UIView setAnimationTransition:110 forView:self.view cache:YES];
 [UIView commitAnimations];
}

Why?

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

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

发布评论

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

评论(3

娇女薄笑 2024-08-26 17:09:45

我遇到了同样的问题,我想我在这个 SO 问题上找到了解决方案

当 viewDidAppear 被调用时,您仍然在屏幕上看不到任何内容(尽管有名称),但您即将看到。然后,您可以使用 PerformSelector:withDelay 或 NSTimer 来启动动画。延迟可以仅为 0.1,并且动画将在屏幕出现时播放。

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    NSLog(@"View did appear!");

    [self performSelector:@selector(animationCode) withObject:nil afterDelay:0.1f];
}

- (void)animationCode {
    // you animation code
}

I had the same problem and I think I found the solution on this SO question.

When viewDidAppear gets called you still don't see anything on the screen (despite the name), but you are about to. You can then use a performSelector:withDelay or an NSTimer to launch your animation. The delay can just be 0.1 and your animation will play just when the screen appears.

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    NSLog(@"View did appear!");

    [self performSelector:@selector(animationCode) withObject:nil afterDelay:0.1f];
}

- (void)animationCode {
    // you animation code
}
往事随风而去 2024-08-26 17:09:45

您没有告诉视图它应该动画到哪个状态,因此它不会执行任何操作。您需要在 beginAnimations:context:commitAnimations 之间放置代码来更改视图的外观(例如,通过删除一个子视图并添加另一个子视图)。

You are not telling the view which state it should animate to so it won't do anything. You need to place code between beginAnimations:context: and commitAnimations that changes the appearance of the view (e.g. by removing one subview and adding another).

心欲静而疯不止 2024-08-26 17:09:45
  1. 您没有正确使用 beginAnimations:commitAnimations。你应该在它们之间放置一些通常不会动画的东西:例如使用 self.view.alpha = 0.5 你会得到一个淡入淡出的效果。 它们对它们之间以外的任何内容都没有影响。

  2. viewDidAppear: 被调用时,你的视图,嗯...已经出现了。现在对任何东西进行动画处理都为时已晚。你真正想做的是这样的:

    - (void)showMyViewWithAnimation {
        [UIView beginAnimations:nil 上下文:nil];
        [UIView setAnimationTransition:110 forView:childView 缓存:YES];
        [parentView addSubview:childView];
        [UIView提交动画];
    }
    

    在上面的示例中,childView 在您的示例中被称为 self.view

  3. 请写出转换的名称;没有人一看就知道110是什么。这是糟糕的风格。

  1. You're not using beginAnimations: and commitAnimations correctly. You're supposed to put something in between them that normally wouldn't be animated: e.g. with self.view.alpha = 0.5 you get a fading effect. They have no effect on anything that isn't between them.

  2. By the time viewDidAppear: is called, your view, well... has appeared. It's too late to animate anything. What you actually want to do is something like this:

    - (void)showMyViewWithAnimation {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationTransition:110 forView:childView cache:YES];
        [parentView addSubview:childView];
        [UIView commitAnimations];
    }
    

    In the example above, childView is what in your example is called self.view.

  3. Please write out the name of the transition; no one knows what 110 is by looking at it. It's bad style. </pedantry>

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