簡體   English   中英

在iOS7中使用動畫處理界面方向

[英]Handling Interface Orientation with Animation in iOS7

從iOS8開始,非常方便的viewWillTransitionToSize方法可以處理界面方向的更改。 我在應用程序中使用以下方式(為簡單起見,我僅使用單個UIViewelementView ,我想在示例中調整大小):

- (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 7iOS8和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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM