0

从 iOS8 开始,就有了非常方便的viewWillTransitionToSize方法来处理界面方向的变化。我在我的应用程序中使用以下方式(为了简单起见,我只使用一个elementViewUIView,我想在示例中调整大小):

- (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);
}

这个方法可以很好地处理方向变化,在实际旋转的同时为 resize 方法设置动画;再简单不过了。但是,我想同时支持iOS 7iOS8和 iOS9,而这种方法在那儿不可用。所以,我的问题是:

如何使用旧版 iOS7 代码获得相同的平滑动画调整大小结果?

我正在尝试使用willRotateToInterfaceOrientation 之类的方法,但无法确定新界面的确切尺寸以及如何在设备旋转的同时为我的调整大小方法设置动画。我需要更熟悉遗留代码的人的帮助。

4

1 回答 1

0

由于没有答案,我被迫翻阅旧文档并花几天时间尝试各种解决方案。长话短说:下面的方法完成了刚刚完成的工作(我希望它能帮助想要支持旧设备的人):

- (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
于 2015-10-31T14:57:24.703 回答