使用transitionFromView和transitionWithView的转换行为
我正在尝试在两个子视图(view1 和 view2)之间创建过渡。当按下按钮时,我希望 view1(前)翻转并显示 view2(后)。我尝试过transitionFromView 和transitionWithView。每个都有效 - 但每个都有一个问题。
transitionFromView - 翻转超级视图(整个窗口视图翻转,而不是子视图)。当发生这种翻转时 - 一个子视图在翻转之前位于超级视图的前面,而另一个子视图位于翻转的背面 - 正如它应该的那样。但我不希望超级视图翻转,只希望子视图翻转。
transitionWithView - 仅翻转子视图 - 但“目标”视图会在转换发生之前显示。
有人有建议吗?
-(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];
}
}
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.
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.
transitionWithView - flips only the subviews - but the 'to' view gets displayed before the transition happens.
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];
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您需要在动画块中删除和添加子视图。另外,我认为transitionWithView应该以超级视图作为参数。我认为要实现这一点,您需要做的是使用与要翻转的视图大小相同的容器视图。
这是从文档中复制的:
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.
This is copied from the documentation:
我也遇到了同样的问题,我同意之前的答案。看来您必须有一个容器视图来动画从一个子视图到另一个子视图的转换。
我正在使用transitionFromView 方法。要在动画后面有背景,您还需要一个带有容器视图背景的超级视图。
我的代码如下所示:
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.
My code looks like:
这是我的工作解决方案存根,一个简单的事情花了 2 个小时,该死的:
我们有 1 个按钮来翻转东西,一个称为面板的 UIView,它包含我想用翻转动画交换的 2 个视图。
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.遇到同样的问题后,我同意 Eric 和 Sam 的观点:
只要您创建一个您希望翻转的适当大小的容器视图,transitionWithView或transitionFromView都将按照上面给出的方式执行您想要的操作。否则整个窗口视图将会翻转。
因此,如果 view1 是您开始的子视图并且您想翻转到 view2 然后添加
然后最简单的动画方法可能是:
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.
So if view1 is the subview you begin with and you want to flip to view2 then add
And then the simplest way to animate is probably:
我有同样的要求,并且看到了很多方法——从使用层(如果你只想处理 UIView,最终会变得非常复杂)到建议我需要一个“容器”,然后在子视图之间进行转换。但如果你只是想从前到后切换某些东西,比如翻转扑克牌或游戏图块,我做了一行更改:(
注意:我有一组 UIViews (tiles[x][y])我的所有图像文件名都在数据库表/数组中,它们动态加载到 UIImageViews 的 UIImage 中,名为“tileBackPicImageNM”的卡片或图块的“背面”图像
。我的代码只是将正面图像替换为背面图像:
效果很好,
但是现在,为了显示翻转动作,它是:
我尝试了不同的“持续时间”参数 - 随后将其设置为浮动。我在 viewDidLoad 例程中设置的速度快于 0.3 几乎不可见,并且值 1 或 2 非常慢...
这不涉及两个子视图,这是我的情况更有用,因为我没有'不想拥有一个包含视图层次结构等的容器数组...并且在新的级别等上我总是重新加载数组...
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:
(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".
so my code that simply swapped the front image out for an image of the back was:
which works fine.
but and now, to show a flipping action, it is:
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...
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...
请记住,容器视图需要纯色背景色。这意味着除了清晰的颜色之外的任何其他颜色。这是我的错误,花了很长时间才弄清楚。
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.
使用transitionFromView,前视图和后视图都应该位于另一个视图(而不是超级视图)内。这样就不会翻转整个屏幕。
UIView.transitionFromView(frontView, toView: backView,uration: 0.5, options: .TransitionFlipFromLeft | .ShowHideTransitionViews) { finish in
}
你可能想让(另一个视图)等于 frontView 的大小
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.
UIView.transitionFromView(frontView, toView: backView, duration: 0.5, options: .TransitionFlipFromLeft | .ShowHideTransitionViews) { finished in
}
You might want to make the (another view) equal to the size of the frontView