iOS學習筆記: 屏幕旋轉,強制橫屏 (對view做動畫)


 

1.基於導航的試圖控制器翻轉,需要展示導航欄,因為不支持狀態欄旋轉,所以屏蔽了狀態欄

  以下代碼寫在需要單獨翻轉的VC中。

 

- (void)viewDidLoad {

    [super viewDidLoad];

 //以下代碼用於隱藏狀態欄

    if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) {

        [self prefersStatusBarHidden];

        [self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)]

    }

}

- (BOOL)prefersStatusBarHidden {

    return YES;//隱藏為YES,顯示為NO

}

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:YES];

 //視圖將要顯示時 翻轉

    CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;//時間

    [UIView beginAnimations:nil context:nil];

    [UIView setAnimationDuration:duration];

    self.navigationController.view.transform = CGAffineTransformIdentity;

    self.navigationController.view.transform = CGAffineTransformMakeRotation(M_PI*1.5);//翻轉角度

    self.navigationController.view.bounds = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width);

    [UIView commitAnimations];

}

- (void)viewWillDisappear:(BOOL)animated {

    [super viewWillDisappear:YES];

 //視圖將要消失的時候 再把視圖翻轉回來 ,不會影響其他VC的展示

    CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;

    [UIView beginAnimations:nil context:nil];

    [UIView setAnimationDuration:duration];

    self.navigationController.view.transform = CGAffineTransformIdentity;

    self.navigationController.view.transform = CGAffineTransformMakeRotation(M_PI*2);

    self.navigationController.view.bounds = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height);

    [UIView commitAnimations];

}

         

 

 

2. 不需要展示導航欄的,或者因為需求想要自定義導航欄的 ,只需要在 viewWillAppear 方法里面加上 隱藏導航欄的代碼

self.navigationController.navigationBar.hidden = YES;

在 viewWillAppear 方法的翻轉動畫的代碼中 改成對view做翻轉就行,如下: 

    self.view.bounds = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height);

    self.view.transform = CGAffineTransformMakeRotation(M_PI*1.5);

然后在視圖將要消失的時候將導航欄的hidden再改為NO,注意 因為只是對當前vc的view做翻轉,所以不會影響導航控制器,也就是當前頁面的翻轉不會影響到其他頁面,

所以在viewWillDisappear里面 不需要再對view做旋轉了。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM