UIView的transitionWithView不起作用
这是测试项目中主视图控制器的 viewDidLoad:
- (void)viewDidLoad
{ [超级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 ,都没有动画。为什么???
编辑:我还尝试使用 PerformSelector withDelay 将动画从 viewDidLoad 移出并移入另一个方法。相同的结果 - 没有动画。
还尝试过这个: [UIView transitionFromView:redView toView:yellowView 持续时间:3 个选项:UIViewAnimationOptionTransitionFlipFromLeft 完成:NULL];
尽管如此,黄色视图还是出现了。没有动画。
Here is viewDidLoad from the main view controller in the test project:
- (void)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];
}
The yellow box just appears. There is no animation, no matter which UIViewAnimationOption I try. Why???
EDIT: I've also tried using performSelector withDelay to move the animation out of viewDidLoad and into another method. Same result - no animation.
Also tried this:
[UIView transitionFromView:redView toView:yellowView duration:3 options:UIViewAnimationOptionTransitionFlipFromLeft completion:NULL];
Still, the yellow view just appears. No animation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
经过一些测试后,您似乎无法在同一运行循环中创建容器视图并设置动画并让一切正常工作。为了使您的代码正常工作,我首先在
viewDidLoad
方法中创建了containerView
和redView
。然后我将动画放入viewDidAppear
方法中。为了可以从viewDidAppear
方法引用containerView
和redView
,我将它们设置为属性。以下是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 theredView
in theviewDidLoad
method. Then I put your animation into theviewDidAppear
method. So that I could reference thecontainerView
and theredView
from theviewDidAppear
method, I made them properties. Here is the code for anST_ViewController.m
file that will perform the animation you wanted.不要将其放入
viewDidLoad
中。当视图完全加载到内存中但尚未显示在屏幕上时,将调用viewDidLoad
。尝试将上述代码放入
viewDidAppear:
中,当视图出现在屏幕上时调用该代码。编辑:附注,如果 PerformSelector:withDelay: 修复了某些内容,则意味着您试图做错那件事。你需要以某种方式重构。 PerformSelector:withDelay: 在 99.999% 的情况下都是错误的解决问题的方法。一旦你将此代码放在不同速度的设备(新 iPhone、旧 iPhone)上,它就会变得混乱。
Don't put this in
viewDidLoad
.viewDidLoad
is called when the view is fully loaded into memory, but not yet displayed on screen.Try putting the above code in
viewDidAppear:
which is called when the view has appeared on screen.Edit: a side note, if performSelector:withDelay: fixes something, that means you're trying to do that thing wrong. You need to refactor somehow. performSelector:withDelay: is the wrong way to solve problems 99.999% of the time. As soon as you place this code on a different speed device (new iPhone, old iPhone) it will mess up.