[UIViewtransitionWithView...] 和 [UIViewtransitionFromView...] 之间的区别

发布于 2024-12-17 06:44:48 字数 941 浏览 2 评论 0原文

我当前有一个包含在视图中的视图。主视图控制器(父视图)有一个属性,该属性在界面构建器中连接到子视图。子视图包含一个按钮。我希望按钮翻转子视图,同时保持主视图静止。目前,我的 IBAction 中有以下用于该按钮的代码:

[UIView transitionWithView: self.subViewFront
                  duration: 1.0
                   options: UIViewAnimationOptionTransitionFlipFromTop
                animations: nil
                completion: nil];
[UIView commitAnimations];
[[self view] addSubview: self.subViewBack;

该代码效果很好,但是在查看 Apple 的 API 参考时我注意到以下内容:

在 iOS 4 及更高版本中不鼓励使用本节中的方法。 请改用基于块的动画方法。

按照这个建议,我尝试使用以下代码:

[UIView transitionFromView: self.subViewFront
                    toView: self.subViewBack
                  duration: 1.0
                   options: UIViewAnimationOptionTransitionFlipFromTop
                completion: nil];

它看起来应该与我最初编码的内容相同,但是,当我使用此代码运行我的应用程序时,它会翻转我的整个视图(父视图和子视图),而不仅仅是子视图。是否应该使用此方法来替代我使用的原始方法,或者我缺少什么?谢谢。

I currently have a view that is contained on a view. The main view controller (parent view) has a property that is hooked up in interface builder to the subview. The subview contains a button. I want the button to flip the subview while keeping the main view stationary. I currently have the following code in my IBAction for the button:

[UIView transitionWithView: self.subViewFront
                  duration: 1.0
                   options: UIViewAnimationOptionTransitionFlipFromTop
                animations: nil
                completion: nil];
[UIView commitAnimations];
[[self view] addSubview: self.subViewBack;

This code works great, however I noticed the following when looking at the API reference from Apple:

Use of the methods in this section is discouraged in iOS 4 and later.
Use the block-based animation methods instead.

Following this advice I attempted to use the following code:

[UIView transitionFromView: self.subViewFront
                    toView: self.subViewBack
                  duration: 1.0
                   options: UIViewAnimationOptionTransitionFlipFromTop
                completion: nil];

It looks like it should do the same as what I originally coded, however, when I run my app with this code, it flips my entire view (parent and child view) instead of just the subview. Should this method be used as a substitution from the original method I have used, or is there something I'm missing? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮华 2024-12-24 06:44:48

我遇到了同样的问题(父级变得动画),但通过将两个视图(subViewFrontsubViewBack)添加到容器视图来解决它。

因此,将两个视图添加到容器 UIView 中,然后使用 -transitionFromView:... 就像您的示例所示:

UIView *container = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)] autorelease];
[self.view addSubview:container];

...

[container addSubview:self.subViewFront];
[container addSubview:self.subViewBack];

I had the same issue (with the parent getting animated instead), but resolved it by adding the two views (subViewFront and subViewBack) to a container view instead.

So add your two views to a container UIView instead, then use the -transitionFromView:... just like your example says:

UIView *container = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)] autorelease];
[self.view addSubview:container];

...

[container addSubview:self.subViewFront];
[container addSubview:self.subViewBack];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文