这是测试项目中主视图 Controller 的 viewDidLoad:
- (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];
}
黄色框刚刚出现。无论我尝试哪个 UIViewAnimationOption,都没有动画。为什么???
编辑:我还尝试使用 performSelector withDelay 将动画移出 viewDidLoad 并移入另一种方法。相同的结果 - 没有动画。
也试过这个: [UIView transitionFromView:redView toView:yellowView duration:3 options:UIViewAnimationOptionTransitionFlipFromLeft completion:NULL];
不过,黄色 View 只是出现了。没有动画。
最佳答案
经过一些测试后,您似乎无法在同一个运行循环中创建容器 View 和设置动画并让一切正常运行。为了使您的代码正常工作,我首先在 viewDidLoad
方法中创建了 containerView
和 redView
。然后我将你的动画放入 viewDidAppear
方法中。为了能够从 viewDidAppear
方法中引用 containerView
和 redView
,我将它们设为属性。以下是将执行您想要的动画的 ST_ViewController.m
文件的代码。
@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
关于ios - UIView transitionWithView 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7616655/