我试图在正面视图和后视图之间切换,就像书页一样。我在视图控制器中运行以下测试代码,没有动画发生。我只看到后视图(红色方块)静态显示2秒钟。怎么了?
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 100.0)];
containerView.backgroundColor = [UIColor greenColor];
[self.view addSubview:containerView];
UIView *backView = [[UIView alloc] initWithFrame:containerView.bounds];
backView.backgroundColor = [UIColor redColor];
[containerView addSubview:backView];
UIView *frontView = [[UIView alloc] initWithFrame:containerView.bounds];
frontView.backgroundColor = [UIColor blueColor];
[containerView addSubview:frontView];
[UIView transitionFromView:frontView
toView:backView
duration:2.0
options:UIViewAnimationOptionTransitionFlipFromRight
| UIViewAnimationOptionShowHideTransitionViews
completion:^(BOOL finished) {
[containerView removeFromSuperview];
}];
答案 0 :(得分:0)
如果您将frontView
,backView
和containerView
声明为ivars:
@interface yourViewController ()
{
UIView *backView;
UIView *frontView;
UIView *containerView;
}
然后将视图初始化代码放在viewDidLoad
:
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 100.0)];
containerView.backgroundColor = [UIColor greenColor];
[self.view addSubview:containerView];
UIView *backView = [[UIView alloc] initWithFrame:containerView.bounds];
backView.backgroundColor = [UIColor redColor];
[containerView addSubview:backView];
UIView *frontView = [[UIView alloc] initWithFrame:containerView.bounds];
frontView.backgroundColor = [UIColor blueColor];
[containerView addSubview:frontView];
最后将动画代码放在方法中:
- (void)flippingViewTransitionAnimation
{
[UIView transitionFromView:frontView
toView:backView
duration:2.0
options:UIViewAnimationOptionTransitionFlipFromRight | UIViewAnimationOptionShowHideTransitionViews
completion:^(BOOL finished) {
[containerView removeFromSuperview];
}];
}
随后在viewDidAppear
内调用上述方法,您应该可以看到动画运行。