屏幕旋转变换

  • 注册通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_handleDeviceOrientationChange)
                                                 name:UIDeviceOrientationDidChangeNotification object:nil];
  • 旋转动画
- (void)rotate:(SJOrientation)orientation animated:(BOOL)animated completion:(void (^__nullable)(SJOrentationObserver * _Nonnull))block {
    if ( !_view || !_targetSuperview ) return;
    
    if ( self.isTransitioning ) return;
    
    _transitioning = YES;
    
    CGAffineTransform transform = CGAffineTransformIdentity;
    UIInterfaceOrientation ori = UIInterfaceOrientationUnknown;
    
    switch ( orientation ) {
        case SJOrientation_LandscapeRight: {
            ori = UIInterfaceOrientationLandscapeLeft;
            transform = CGAffineTransformMakeRotation(-M_PI_2);
        }
            break;
        case SJOrientation_LandscapeLeft: {
            ori = UIInterfaceOrientationLandscapeRight;
            transform = CGAffineTransformMakeRotation(M_PI_2);
        }
            break;
        case SJOrientation_Portrait: {
            ori = UIInterfaceOrientationPortrait;
            transform = CGAffineTransformIdentity;
            [_blackView removeFromSuperview];
        }
            break;
    }
    
    SJOrientation oldOri = _orientation;
    SJOrientation newOri = orientation;
    
    if ( oldOri == SJOrientation_Portrait ) {
        CGRect frame = [__window convertRect:_view.frame fromView:_targetSuperview];
        _view.frame = frame;
        [__window addSubview:_view];
        _portrait = frame;
    }
    
    
    // update
    _orientation = orientation;
    
    [UIApplication sharedApplication].statusBarOrientation = ori;
    
    [UIView beginAnimations:@"rotation" context:NULL];
    if ( animated ) [UIView setAnimationDuration:_duration];
    else [UIView setAnimationDuration:0];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(_animationDidStop)];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    if ( newOri == SJOrientation_Portrait ) {
        _view.bounds = CGRectMake(0, 0, _portrait.size.width, _portrait.size.height);
        _view.center = CGPointMake(_portrait.origin.x + _portrait.size.width * 0.5, _portrait.origin.y + _portrait.size.height * 0.5);
    }
    else {
        CGFloat width = [UIScreen mainScreen].bounds.size.width;
        CGFloat height = [UIScreen mainScreen].bounds.size.height;
        CGFloat max = MAX(width, height);
        CGFloat min = MIN(width, height);
        _view.bounds = CGRectMake(0, 0, max, min);
        _view.center = CGPointMake(min * 0.5, max * 0.5);
    }
    [_view setTransform:transform];
    [UIView commitAnimations];
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,035评论 3 119
  • 安静是一种哲学 入门容易 学好却很难 那边 一浅眸 忽闪忽闪 这边 一心颤 扑通扑通
    爱笑的小跑阅读 747评论 0 0
  • 12月20日,星期三,晴 今天女儿感冒有所好转,她坚持要去学校,我就送她去了。 下午继续打针,我发现她今天的精神状...
    月儿贝贝阅读 1,261评论 0 1
  • 邻居的门口有一棵老槐树,树很大,一根树枝垂到我家的窗前。阳光将其斑驳的影子投在早已模糊不清的玻璃上,又在掉了漆的书...
    王黄鸡阅读 3,269评论 0 0
  • 今天是已经看完《阿里传》的第五天,终于有一点时间来整理一下思绪,写一篇读后感了。踏入职场以来,我一直在从事电...
    林德豹阅读 6,844评论 0 0