如何在使用单步旋转时实现平滑动画/如何在旋转开始时获得新的帧大小? [英] How to achieve smooth animation when using single-step rotation / How do I get the new frame size at the start of rotation?

查看:100
本文介绍了如何在使用单步旋转时实现平滑动画/如何在旋转开始时获得新的帧大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从两阶段旋转切换到一阶段旋转(以避免控制台警告,因为Apple推荐这样做,因为一阶段更快)。

I'm trying to switch from two-stage rotation to one-stage rotation (to avoid the console warning, and because Apple recommend doing so because one-stage is faster).

然而,我无法弄清楚如何获得我的视图的新大小(考虑导航栏,状态栏等),足以在动画期间执行我的UI更新(而不是简单正如许多应用程序似乎所做的那样,将项目捕捉到最后的新位置,这会在动画结束时产生一个大的混蛋。

However I can't figure out how to get the new size of my view (taking into account the navigation bar, status bar, etc) early enough to perform the update of my UI during the animation (rather than simply snapping the items to their new positions at the end as many applications seem to do, which results in a big "jerk" right at the end of the animation).

我在中获得的尺寸willAnimateRotationToInterfaceOrientation:duration:方法(可能很明显)是旧尺寸。

The sizes I get in the willAnimateRotationToInterfaceOrientation:duration: method are (perhaps obviously) the old sizes.

我可以看到我应该能够手工计算它,计算当前的条形高度,然后通过从旋转的屏幕尺寸中扣除那些来推断新的视图框架尺寸? (这并不难做到,虽然可能很脆弱,因为它假设导航栏,状态栏等在两个方向都是相同的高度,你必须手动考虑工具栏的高度不同肖像与风景 - 我只是想确保我没有错过更直接或更常见的方式。)

I can see I should be able calculate it by hand, working out the current bar heights, then inferring the new view frame size by deducted those from the rotated screen dimensions? (which isn't that difficult to do, though is perhaps fragile as it assumes the navigation bar, status bar, etc will be the same height in both orientations, and you'd have to manually take account of the toolbar being different heights in portrait vs landscape - I just want to make sure I've not missed a more straightforward or common way.)

任何关于其他人采取的方法的反馈都会很棒!

Any feedback on approaches other people have taken would be great!

谢谢

Joseph

推荐答案

使用我的视图的layoutSubviews方法进行自动旋转,我获得了最大的成功。在方向更改期间调用layoutSubviews时,视图的边界已设置为旋转结束时的范围。您也可以在此时查询状态栏,导航栏和工具栏的大小,并获得正确的后旋转值(尽管可以交换状态栏的宽度和高度 - 我只需将较小的值作为高度,效果很好)。使用该信息,您可以布置子视图,然后它们将作为轮换的一部分进行动画处理。

I've had the most success using my view's layoutSubviews method for autorotations. When layoutSubviews gets called during an orientation change, the view's bounds are already set to what they will be at the conclusion of the rotation. You can also at this time query the status bar, navigation bar, and toolbar for their sizes and get the correct post-rotation values (although the status bar width and height may be swapped--I just take the smaller value as the height, works great). Using that information you can lay out subviews and they will then be animated as part of the rotation.

为您想要的每种情况创建UIView子类可能会很烦人为此,我创建了一个名为DelegatingView的UIView的特殊子类,如下所示:

It can be annoying to create a UIView subclass for every situation where you want to do this, so I created a special subclass of UIView called DelegatingView, which looks like this:

@protocol DelegatingViewDelegate

- (void)layoutSubviewsForView:(UIView*)view;

@end

@interface DelegatingView : UIView {
    id<DelegatingViewDelegate> _delegate;
}

@property (nonatomic,assign) id<DelegatingViewDelegate> delegate;

@end

@implementation DelegatingView

@synthesize delegate = _delegate;

- (void)layoutSubviews {
    [super layoutSubviews];
    [_delegate layoutSubviewsForView:self];
}

@end

我用它作为我的基础查看,向其添加子视图,并将我的UIViewController设置为委托。然后我可以从我的视图控制器布局子视图,可以访问当前方向等等。

I use this as my base view, add subviews to it, and set my UIViewController as the delegate. Then I can layout subviews from my view controller, having access to the current orientation and so on.

这篇关于如何在使用单步旋转时实现平滑动画/如何在旋转开始时获得新的帧大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆