UIView transitionWithView不起作用 [英] UIView transitionWithView doesn't work
问题描述
以下是测试项目中主视图控制器的viewDidLoad:
Here is viewDidLoad from the main view controller in the test project:
- (void)viewDidLoad
{
[super viewDidLoad];
{ [super viewDidLoad];
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 300, 300)];
[self.view addSubview:containerView];
UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
[redView setBackgroundColor:[UIColor redColor]];
[containerView addSubview:redView];
UIView *yellowView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
[yellowView setBackgroundColor:[UIColor yellowColor]];
[UIView transitionWithView:containerView duration:3
options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
[redView removeFromSuperview];
[containerView addSubview:yellowView];
}
completion:NULL];
}
出现黄色框。没有动画,无论我尝试哪种UIViewAnimationOption。为什么???
The yellow box just appears. There is no animation, no matter which UIViewAnimationOption I try. Why???
编辑:我也尝试使用performSelector withDelay将动画移出viewDidLoad并转移到另一个方法中。相同的结果 - 没有动画。
I've also tried using performSelector withDelay to move the animation out of viewDidLoad and into another method. Same result - no animation.
也试过这个:
[UIView transitionFromView:redView toView:yellowView持续时间:3个选项:UIViewAnimationOptionTransitionFlipFromLeft完成:NULL];
Also tried this: [UIView transitionFromView:redView toView:yellowView duration:3 options:UIViewAnimationOptionTransitionFlipFromLeft completion:NULL];
然而,黄色视图才会出现。没有动画。
Still, the yellow view just appears. No animation.
推荐答案
经过一些测试后,您似乎无法创建容器视图并在同一个runloop中设置动画让事情奏效。为了使你的代码有效,我首先在中创建
方法。然后我将你的动画放入 containerView
和 redView
viewDidLoad viewDidAppear
方法。这样我就可以从 viewDidAppear $ c $中引用
containerView
和 redView
c>方法,我把它们作为属性。以下是 ST_ViewController.m
文件的代码,该文件将执行您想要的动画。
After some testing it appears that you cannot create the container view and set up the animation within the same runloop and have things work. In order to make your code work, I first created the containerView
and the redView
in the viewDidLoad
method. Then I put your animation into the viewDidAppear
method. So that I could reference the containerView
and the redView
from the viewDidAppear
method, I made them properties. Here is the code for an ST_ViewController.m
file that will perform the animation you wanted.
@interface ST_ViewController ()
@property (nonatomic, strong) UIView *containerView;
@property (nonatomic, strong) UIView *redView;
@end
@implementation ST_ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self setContainerView:[[UIView alloc] initWithFrame:CGRectMake(10, 10, 300, 300)]];
[[self view] addSubview:[self containerView]];
[self setRedView:[[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)]];
[[self redView] setBackgroundColor:[UIColor redColor]];
[[self containerView] addSubview:[self redView]];
}
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
UIView *yellowView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
[yellowView setBackgroundColor:[UIColor yellowColor]];
[UIView transitionWithView:[self containerView]
duration:3
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^(void){
[[self redView] removeFromSuperview];
[[self containerView] addSubview:yellowView];
}
completion:nil];
}
@end
这篇关于UIView transitionWithView不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!