这是一个iPad项目,我有一个带有几个子视图的UIView,我试图使用[UIView transitionFromView:toView:duration:options:completion]为其中一个UIViews制作动画,但当我运行此方法时,整个父项视图被翻转! 这是我正在使用的代码:
UIView *secondView = [[UIView alloc] initWithFrame:CGRectMake(200, 200, 300, 300)];
[newView setBackgroundColor:[UIColor redColor]];
[UIView transitionFromView:self.firstView.view toView:secondView duration:1.0 options:UIViewAnimationOptionTransitionFlipFromLeft completion:nil];
如何在不动画整个父视图的情况下在这些视图之间切换? 谢谢!
答案 0 :(得分:70)
此代码可以帮助您:
将要翻转的两个视图放在具有相同大小的未命名视图中
并将IBOutlet UIView *newView,*oldView;
与视图相关联,并将新视图置于顶部
bool a = NO;
@implementation ViewController
- (IBAction)flip:(id)sender
{
if (a == NO) {
[UIView transitionFromView:oldView toView:newView
duration:1.0
options:UIViewAnimationOptionTransitionFlipFromLeft
completion:NULL];
a = YES; // a = !a;
}
else {
[UIView transitionFromView:newView toView:oldView
duration:1.0
options:UIViewAnimationOptionTransitionFlipFromLeft
completion:NULL];
a = NO; // a = !a;
}
}
答案 1 :(得分:37)
我也遇到了问题。我使用了Floris编写的代码,但是虽然它适用于第一个“翻转”,但下一个翻转开始变得奇怪,前端UI视图上的按钮和标签丢失了位置。
我将代码放在适当的位置,并且工作正常。
需要注意的事项:
(在我的.h)
@property BOOL displayingFront;
在我的.m
@synthesize displayingFront;
在我的viewDidLoad方法
中self.displayingFront = YES;
这是.m中的代码,如果正面和背面的UIViews,我已经装配了两个按钮......
- (IBAction)flip:(id)sender
{
[UIView transitionWithView:self.panelView
duration:1.0
options:(displayingFront ? UIViewAnimationOptionTransitionFlipFromRight :
UIViewAnimationOptionTransitionFlipFromLeft)
animations: ^{
if(displayingFront)
{
self.frontView.hidden = true;
self.backView.hidden = false;
}
else
{
self.frontView.hidden = false;
self.backView.hidden = true;
}
}
completion:^(BOOL finished) {
if (finished) {
displayingFront = !displayingFront;
}
}];
}
答案 2 :(得分:2)
使用容器视图来保存您的子视图,并使容器视图的大小与您尝试设置动画的子视图相同。
因此,您可以像这样设置您的观点。
self.view-> “容器视图” - >子视图
答案 3 :(得分:2)
**//flipping view continuously**
bool a = NO;
-(void)viewDidLoad
{
// THIS is the canvass holding the two views this view is flipping
UIView *v=[[UIView alloc]initWithFrame:CGRectMake(40, 40, 150, 150)];
v.backgroundColor=[UIColor clearColor];
[self.view addSubview:v];
[v addSubview:yourfirstview];
[v addSubview:yoursecondview];
// calling the method
[NSTimer scheduledTimerWithTimeInterval:2.0
target:self
selector:@selector(flip)
userInfo:nil
repeats:YES];
}
//flipping view randomly
-(void)flip
{
if (a == NO)
{
[UIView transitionFromView:yourfirstview toView:yoursecondview duration:1.0 options:UIViewAnimationOptionTransitionFlipFromLeft completion:NULL];
a = YES;
}
else if(a==YES)
{
[UIView transitionFromView:yoursecondview toView:yourfirstview duration:1.0 options:UIViewAnimationOptionTransitionFlipFromLeft completion:NULL];
a = NO;
}
}
答案 4 :(得分:2)
我通过使用旧式动画解决了这个问题:
[UIView beginAnimations:@"Flip" context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:firstView cache:YES];
[self addSubview:secondView];
[UIView commitAnimations];
此外,您可以删除firstView:
[firstView removeFromSuperview];
答案 5 :(得分:1)
我想在SWIFT 4中更新史蒂夫·帕克的答案:-
var displayBlankView:Bool = true
UIView.transition(with:flipView, duration:1.0, options:displayBlankView ? .transitionFlipFromLeft:.transitionFlipFromRight, animations:{
if(self.displayBlankView){
self.view1.isHidden=true
self.view2.isHidden=false
}else{
self.view1.isHidden=false
self.view2.isHidden=true
}
}, completion:{
(finished: Bool) in
self.displayBlankView = !self.displayBlankView;
})