ios 使用 transitionFromView 和 transitionWithView 的过渡行为
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3602434/
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
Transition behavior using transitionFromView and transitionWithView
提问by David
I am attempting to create a transition between two subviews (view1 and view2). When a button is pressed I want view1 (front) to flip and show view2 (back). I have tried both transitionFromView and transitionWithView. Each works - but each has a problem.
我正在尝试在两个子视图(view1 和 view2)之间创建过渡。当按下按钮时,我希望 view1(正面)翻转并显示 view2(背面)。我已经尝试过 transitionFromView 和 transitionWithView。每个都有效 - 但每个都有问题。
transitionFromView- flips the superview (the whole window view flips, not the subviews). When this flip happens - one subview is on the front of the superview before the flip, and the other subview is on the back of the flip - as it should be. But I don't want the superview to flip, just the subviews.
transitionFromView- 翻转超级视图(整个窗口视图翻转,而不是子视图)。当这种翻转发生时 - 一个子视图在翻转之前位于超视图的前面,另一个子视图在翻转的后面 - 应该是这样。但我不希望超级视图翻转,只是子视图翻转。
transitionWithView- flips only the subviews - but the 'to' view gets displayed before the transition happens.
transitionWithView- 仅翻转子视图 - 但在转换发生之前显示“to”视图。
Anyone have a suggestion?
有人有建议吗?
-(IBAction) button1action:(id) sender {
if ([sender tag] == 0) {
[UIView transitionFromView:view2 toView:view1 duration:2.0
options:UIViewAnimationOptionTransitionFlipFromLeft
completion:nil];
}
else {
[view1 removeFromSuperview];
[self.view addSubview:view2];
[UIView transitionWithView:view2
duration:2.0
options:UIViewAnimationOptionTransitionFlipFromRight +
UIViewAnimationOptionShowHideTransitionViews
animations:^{}
completion:nil];
}
}
回答by Erik B
You need to remove and add the subviews in the animation block. Also, I think that transitionWithView is supposed to take the super view as argument. I think what you need to do to get this right is to use a container view that is the same size as the views you want to flip.
您需要在动画块中删除和添加子视图。另外,我认为 transitionWithView 应该将超级视图作为参数。我认为要做到这一点,您需要做的是使用与要翻转的视图大小相同的容器视图。
This is copied from the documentation:
这是从文档中复制的:
[UIView transitionWithView:containerView
duration:0.2
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{ [fromView removeFromSuperview]; [containerView addSubview:toView]; }
completion:NULL];
回答by Sam
I have run into this exact same problem, and I agree with the earlier answer. It seems you must have a container view for animating a transition from one subview to another subview.
我遇到了这个完全相同的问题,我同意之前的答案。似乎您必须有一个容器视图来动画从一个子视图到另一个子视图的过渡。
I'm using the transitionFromView approach. To have a background behind the animation, you will also need a superview with the background for the container view.
我正在使用 transitionFromView 方法。为了在动画后面有一个背景,你还需要一个带有容器视图背景的超级视图。
My code looks like:
我的代码看起来像:
[UIView transitionFromView:view1
toView:view2
duration:0.5
options:UIViewAnimationOptionTransitionFlipFromRight |
UIViewAnimationOptionAllowUserInteraction |
UIViewAnimationOptionBeginFromCurrentState
completion:nil];
回答by Rui Nunes
Here's my working solution stub, a simple thing that took 2 hours, damn:
we got 1 button to flip things, a UIView
called panel that holds the 2 views I want to swap with a flip animation.
这是我的工作解决方案存根,一个简单的事情花了 2 个小时,该死的:我们有 1 个按钮来翻转东西,一个UIView
被调用的面板,其中包含我想用翻转动画交换的 2 个视图。
-(void)viewDidLoad{
[super viewDidLoad];
UIButton *btnFlip = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnFlip.frame = CGRectMake(10, 10, 50, 30);
[btnFlip setTitle:@"flip" forState:UIControlStateNormal];
[btnFlip addTarget:self action:@selector(flip) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnFlip];
panel = [[UIView alloc] initWithFrame:CGRectMake(10, 40, 300,300)];
panel.backgroundColor = [UIColor darkGrayColor];
[self.view addSubview:panel];
panelBack = [[UIView alloc] initWithFrame:CGRectMake(10, 40, 280, 200)];
panelBack.tag = 1;
panelBack.backgroundColor = [UIColor brownColor];
[panel addSubview:panelBack];
panelFront = [[UIView alloc] initWithFrame:CGRectMake(10, 40, 280, 200)];
panelFront.tag = 2;
panelFront.backgroundColor = [UIColor whiteColor];
[panel addSubview:panelFront];
displayingFront = TRUE;
}
-(void)flip{
[UIView transitionWithView:panel
duration:0.5
options:(displayingFront ? UIViewAnimationOptionTransitionFlipFromRight : UIViewAnimationOptionTransitionFlipFromLeft)
animations:^{
if (displayingFront) {
//panelFront.hidden=TRUE;
//panelBack.hidden = FALSE;
[panelFront removeFromSuperview];
[panel addSubview:panelBack];
}else{
//panelFront.hidden=FALSE;
//panelBack.hidden = TRUE;
[panelBack removeFromSuperview];
[panel addSubview:panelFront];
}
}
completion:^(BOOL finished){
displayingFront = !displayingFront;
}];
}
回答by Hal
Having run into the same problem I agree with Eric and Sam: either transitionWithView or transitionFromView will both do what you want as given above as long as you create a container view of the appropriate size that you wish to flip. Otherwise the whole window view will flip.
遇到了同样的问题,我同意 Eric 和 Sam 的观点:transitionWithView 或 transitionFromView 都可以按照上面给出的方式执行您想要的操作,只要您创建了一个您希望翻转的适当大小的容器视图。否则整个窗口视图将翻转。
So if view1 is the subview you begin with and you want to flip to view2 then add
因此,如果 view1 是您开始的子视图并且您想翻转到 view2 然后添加
UIView *containerView = [[UIView alloc] initWithFrame:appropriateSize];
[containerView addSubview:view1];
And then the simplest way to animate is probably:
然后制作动画的最简单方法可能是:
[UIView transitionFromView:view1
toView:view2
duration:2.0
options:UIViewAnimationOptionTransitionFlipFromLeft
completion:nil];
回答by harry
I had the same requirement, and saw many approaches -- from using layers (which ends up very complicated if you just want to deal with a UIView) to suggestions that i needed a "container" to then transition between to sub-views. But if you just want to swithc something front to back, like flipping over a playing card or a game tile, i made a one line change:
我有同样的要求,并且看到了很多方法——从使用层(如果你只想处理 UIView 最终会非常复杂)到我需要一个“容器”然后在子视图之间转换的建议。但是,如果您只想前后切换某些内容,例如翻转扑克牌或游戏图块,我进行了一行更改:
(note: i have an array of UIViews (tiles[x][y]) in a grid (for a game). i have all my imagefilenames in database tables / arrays, which dynamically are loaded into the UIImages of the UIImageViews. The image for the "back" of a card or tile in a UIImage named "tileBackPicImageNM".
(注意:我在网格中有一个 UIViews (tiles[x][y]) 数组(用于游戏)。我在数据库表/数组中有我所有的图像文件名,它们动态加载到 UIImageViews 的 UIImages 中。名为“tileBackPicImageNM”的 UIImage 中卡片或图块的“背面”图像。
so my code that simply swapped the front image out for an image of the back was:
所以我的代码只是将正面图像换成背面图像:
[tiles[indexX][indexY] setImage:[UIImage imageNamed:tileBackPicImageNM]];
which works fine.
这工作正常。
but and now, to show a flipping action, it is:
但是现在,要显示翻转动作,它是:
[UIView transitionWithView:tiles[indexX][indexY] duration:0.3 options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{
[[indexX][indexY] setImage:[UIImage imageNamed:tileBackPicImageNM]];
}
completion:NULL];
I experimented with different "duration" parameters -- subsequently made it a float that I set in the viewDidLoad routine. Faster than 0.3 is almost not visible, and a value of 1 or 2 is very slow...
我尝试了不同的“持续时间”参数——随后使它成为我在 viewDidLoad 例程中设置的浮点数。快于 0.3 几乎看不到,值 1 或 2 很慢...
This does not involve two sub-views, which is my case is a lot more useful since i didn't want to have an array of containers containing a hierarchy of views, etc... and on new levels etc i always reload the arrays anyway...
这不涉及两个子视图,这在我的情况下更有用,因为我不想拥有包含视图层次结构等的容器数组……而在新级别等上,我总是重新加载数组反正...
回答by Ilker Baltaci
Keep in mind that the container view requires a solid background color.It means anything else than clear color.This was my mistake and spent quite long time to figure it out.
请记住,容器视图需要纯背景色。这意味着除了清晰的颜色之外的任何其他内容。这是我的错误,花了很长时间才弄明白。
回答by Ahmed Onawale
using transitionFromView, both front and back view should be inside another view(not the super view). this way it will not flip the whole screen.
使用transitionFromView,前视图和后视图都应该在另一个视图内(不是超级视图)。这样它就不会翻转整个屏幕。
> superview
> another view
> backview
> frontview
UIView.transitionFromView(frontView, toView: backView, duration: 0.5, options: .TransitionFlipFromLeft | .ShowHideTransitionViews) { finished in
UIView.transitionFromView(frontView, toView: backView, duration: 0.5, options: .TransitionFlipFromLeft | .ShowHideTransitionViews) { 完成
}
}
You might want to make the (another view) equal to the size of the frontView
您可能想让(另一个视图)等于 frontView 的大小