在 iOS7 中使用动画处理界面方向
Handling Interface Orientation with Animation in iOS7
从iOS8开始,就有了非常方便的viewWillTransitionToSize方法来处理界面方向变化。我在我的应用程序中使用以下方式(为了简单起见,我只使用单个 UIView
、elementView,我想在示例中调整大小):
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
// Define the new screen size we are about to transition to
windowSize = size;
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context){
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
// Animate UI element to new dimensions
[self setUpUIElements];
} completion:^(id<UIViewControllerTransitionCoordinatorContext> context){
// Housekeeping after animation (just so there is something here)
randomBool = YES;
}];
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
}
-(void) setUpUIElements {
elementView.frame = CGRectMake(0.0f,
0.0f,
windowSize.size.width,
windowSize.size.height);
}
此方法可以很好地处理方向变化,在实际旋转的同时对调整大小方法进行动画处理;再简单不过了。但是,我想支持 iOS 7 以及 iOS8 和 iOS9,这个方法在那里不可用。所以,我的问题是:
如何使用旧版 iOS7 代码获得相同的平滑动画大小调整结果?
我正在试验 willRotateToInterfaceOrientation 之类的方法,但无法确定新界面的确切尺寸以及如何在设备旋转的同时为我的调整大小方法设置动画。我需要更熟悉遗留代码的人的帮助。
由于没有答案,我被迫翻阅旧文档并花了几天时间尝试各种解决方案。长话短说:下面的方法完成刚刚完成的工作(我希望它能帮助那些想要支持旧设备的人):
- (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
// Typecast the interface orientation to which the app is transitioning
UIDeviceOrientation orientation = (UIDeviceOrientation)toInterfaceOrientation;
// Call method that handles the resizing
[self handleInterfaceOrientation:orientation withDuration:duration];
} // <- For leagcy iOS7 support
- (void)handleInterfaceOrientation:(UIDeviceOrientation)toInterfaceOrientation withDuration:(NSTimeInterval)duration {
// Calculate screen size the app is transitioning to (ignore iOS8-only devices, such as iPhone 6 and newer)
if ( (toInterfaceOrientation == UIInterfaceOrientationPortrait) || (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) ) {
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
if ([[UIScreen mainScreen] bounds].size.height > 480.0f) {
windowSize = CGSizeMake(320.0f, 568.0f);
} else {
windowSize = CGSizeMake(320.0f, 480.0f);
}
} else {
windowSize = CGSizeMake(768.0f, 1024.0f);
}
} else {
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
if ([[UIScreen mainScreen] bounds].size.height > 480.0f) {
windowSize = CGSizeMake(568.0f, 320.0f);
} else {
windowSize = CGSizeMake(480.0f, 320.0f);
}
} else {
windowSize = CGSizeMake(1024.0f, 768.0f);
}
}
// Animate UI elements to new dimension
[UIView animateWithDuration:duration
delay:0.0f
options: UIViewAnimationOptionCurveEaseInOut
animations:^{
[self populateGalleryScrollView];
} completion:^(BOOL finished) {
// Once rotation is done, disable the rotation view
rotationView.alpha = 0.0f;
}];
} // <- For legacy iOS7 support
从iOS8开始,就有了非常方便的viewWillTransitionToSize方法来处理界面方向变化。我在我的应用程序中使用以下方式(为了简单起见,我只使用单个 UIView
、elementView,我想在示例中调整大小):
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
// Define the new screen size we are about to transition to
windowSize = size;
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context){
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
// Animate UI element to new dimensions
[self setUpUIElements];
} completion:^(id<UIViewControllerTransitionCoordinatorContext> context){
// Housekeeping after animation (just so there is something here)
randomBool = YES;
}];
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
}
-(void) setUpUIElements {
elementView.frame = CGRectMake(0.0f,
0.0f,
windowSize.size.width,
windowSize.size.height);
}
此方法可以很好地处理方向变化,在实际旋转的同时对调整大小方法进行动画处理;再简单不过了。但是,我想支持 iOS 7 以及 iOS8 和 iOS9,这个方法在那里不可用。所以,我的问题是:
如何使用旧版 iOS7 代码获得相同的平滑动画大小调整结果?
我正在试验 willRotateToInterfaceOrientation 之类的方法,但无法确定新界面的确切尺寸以及如何在设备旋转的同时为我的调整大小方法设置动画。我需要更熟悉遗留代码的人的帮助。
由于没有答案,我被迫翻阅旧文档并花了几天时间尝试各种解决方案。长话短说:下面的方法完成刚刚完成的工作(我希望它能帮助那些想要支持旧设备的人):
- (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
// Typecast the interface orientation to which the app is transitioning
UIDeviceOrientation orientation = (UIDeviceOrientation)toInterfaceOrientation;
// Call method that handles the resizing
[self handleInterfaceOrientation:orientation withDuration:duration];
} // <- For leagcy iOS7 support
- (void)handleInterfaceOrientation:(UIDeviceOrientation)toInterfaceOrientation withDuration:(NSTimeInterval)duration {
// Calculate screen size the app is transitioning to (ignore iOS8-only devices, such as iPhone 6 and newer)
if ( (toInterfaceOrientation == UIInterfaceOrientationPortrait) || (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) ) {
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
if ([[UIScreen mainScreen] bounds].size.height > 480.0f) {
windowSize = CGSizeMake(320.0f, 568.0f);
} else {
windowSize = CGSizeMake(320.0f, 480.0f);
}
} else {
windowSize = CGSizeMake(768.0f, 1024.0f);
}
} else {
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
if ([[UIScreen mainScreen] bounds].size.height > 480.0f) {
windowSize = CGSizeMake(568.0f, 320.0f);
} else {
windowSize = CGSizeMake(480.0f, 320.0f);
}
} else {
windowSize = CGSizeMake(1024.0f, 768.0f);
}
}
// Animate UI elements to new dimension
[UIView animateWithDuration:duration
delay:0.0f
options: UIViewAnimationOptionCurveEaseInOut
animations:^{
[self populateGalleryScrollView];
} completion:^(BOOL finished) {
// Once rotation is done, disable the rotation view
rotationView.alpha = 0.0f;
}];
} // <- For legacy iOS7 support