UIView 翻转动画没有动画
Posted
技术标签:
【中文标题】UIView 翻转动画没有动画【英文标题】:UIView flip animation is not animating 【发布时间】:2015-06-26 01:28:26 【问题描述】:我正在尝试在前视图和后视图之间翻转,就像书页一样。我在视图控制器中运行了以下测试代码,但没有发生动画。我只看到后视图(红色方块)静态出现了 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];
];
【问题讨论】:
【参考方案1】:如果您声明您的frontView
、backView
和containerView
as 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
中调用上述方法,您应该可以看到动画运行。
【讨论】:
我找到了问题的原因。您只能在绘制视图后为其设置动画。因此,您不能在同一个运行循环中创建视图并为它们设置动画。您的解决方案是将动画延迟到以后的运行循环的一种方法。也可以使用 performSelector 延迟动画。以上是关于UIView 翻转动画没有动画的主要内容,如果未能解决你的问题,请参考以下文章