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

时间:2022-11-06 00:15:44

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

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

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

Why?

3 个解决方案

#1


24  

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.

当调用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
}

#2


0  

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).

你没有告诉它应该查看它应该设置的状态,所以它不会做任何事情。您需要在beginAnimations:context:和commitAnimations之间放置代码,以改变视图的外观(例如,删除一个子视图并添加另一个子视图)。

#3


0  

  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.

    您没有正确使用beginAnimations:和commitAnimations。你应该把它们放在它们之间通常不会被动画化的东西:例如使用self.view.alpha = 0.5可以获得淡化效果。它们对任何不在它们之间的东西都没有影响。

  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:

    当viewDidAppear:被调用时,你的视图已经出现了。任何东西动画都为时已晚。你真正想要做的是这样的事情:

    - (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.

    在上面的示例中,childView在您的示例中称为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>

    请写出过渡的名称;通过观察,没有人知道110是什么。这是糟糕的风格。

#1


24  

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.

当调用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
}

#2


0  

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).

你没有告诉它应该查看它应该设置的状态,所以它不会做任何事情。您需要在beginAnimations:context:和commitAnimations之间放置代码,以改变视图的外观(例如,删除一个子视图并添加另一个子视图)。

#3


0  

  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.

    您没有正确使用beginAnimations:和commitAnimations。你应该把它们放在它们之间通常不会被动画化的东西:例如使用self.view.alpha = 0.5可以获得淡化效果。它们对任何不在它们之间的东西都没有影响。

  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:

    当viewDidAppear:被调用时,你的视图已经出现了。任何东西动画都为时已晚。你真正想要做的是这样的事情:

    - (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.

    在上面的示例中,childView在您的示例中称为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>

    请写出过渡的名称;通过观察,没有人知道110是什么。这是糟糕的风格。