ios 如何翻转单个 UIView(不翻转父视图)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9524048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to flip an individual UIView (without flipping the parent view)
提问by DanMunoz
This is an iPad project where I have a UIView with several subViews, and I am trying to animate one of this UIViews using [UIView transitionFromView:toView:duration:options:completion], but when I run this method the whole parent view gets flipped! This is the code I'm using:
这是一个 iPad 项目,其中我有一个带有多个子视图的 UIView,我正在尝试使用 [UIView transitionFromView:toView:duration:options:completion] 为其中一个 UIView 设置动画,但是当我运行此方法时,整个父视图都会翻转!这是我正在使用的代码:
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];
Any idea on how can I flip between this views without animating the whole parent view? Thanks!
关于如何在不为整个父视图设置动画的情况下在这些视图之间切换的任何想法?谢谢!
回答by Floris497
this code may helps you:
此代码可以帮助您:
put the two views you want to flip inside an unnamed view with the same size
and link the IBOutlet UIView *newView,*oldView;
to the views and put the new view on top
将要翻转的两个视图放在一个大小相同的未命名视图中,并将其链接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;
}
}
回答by Steve Parker
I had problems getting this to work also. I used the code written by Floris, but although it worked for the first "flip" the next flip started to go weird where the buttons and labels on the frontside UI View lost there positions.
我也有问题让这个工作。我使用了 Floris 编写的代码,但尽管它适用于第一次“翻转”,但下一次翻转开始变得奇怪,前端 UI 视图上的按钮和标签丢失了那里的位置。
I put the code below into place and it is working fine.
我把下面的代码放到位,它工作正常。
Couple of things to note:
需要注意的几点:
- panelView is a UIView control on the ViewController that has two other UIViews nested inside it (subviews). The first one is called frontView, second one is called backView. (see picture below)
- panelView 是 ViewController 上的一个 UIView 控件,其中嵌套了两个其他 UIView(子视图)。第一个叫做frontView,第二个叫做backView。(见下图)
- I have a bool property called displayingFront on the class
- 我在班级上有一个名为 displayFront 的 bool 属性
(in my .h)
(在我的 .h 中)
@property BOOL displayingFront;
in my .m
在我的.m
@synthesize displayingFront;
in my viewDidLoad method
在我的 viewDidLoad 方法中
self.displayingFront = YES;
This is the code in the .m that i have rigged up to the two buttons if front and back UIViews...
这是 .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;
}
}];
}
回答by vipinagg
Use a container view to hold your subviews, and make the container view the same size as your subview that you're trying to animate.
使用容器视图来保存您的子视图,并使容器视图与您尝试设置动画的子视图大小相同。
So, you can setup your views like this.
因此,您可以像这样设置视图。
self.view-> "a container view" -> subviews
self.view->“容器视图”-> 子视图
回答by shankar
**//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;
}
}
回答by Calin Drule
I fixed this problem by using old-school animations:
我通过使用老式动画解决了这个问题:
[UIView beginAnimations:@"Flip" context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:firstView cache:YES];
[self addSubview:secondView];
[UIView commitAnimations];
Additionally you can remove the firstView:
此外,您可以删除 firstView:
[firstView removeFromSuperview];
回答by khusboo suhasini
I'd like to make an update of Steve Parker's answer in SWIFT 4:-
我想更新史蒂夫帕克在 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;
})