美文网首页
iOS之屏幕旋转

iOS之屏幕旋转

作者: 虫yu | 来源:发表于2016-05-07 17:04 被阅读255次
    屏幕方向.png

    检测屏幕旋转:
    视图控制器本身能检测到屏幕的旋转,如果要处理屏幕旋转,需要重写几个方法:

    supportedInterfaceOrientations(设置设备支持旋转的方向,如果不添加,视图控制器将无法检测屏幕的旋转)。
    willRotateToInterfaceOrientation:duration:(暂停音乐、关闭视图交互等)。
    willAnimateRotationToInterfaceOrientation:duration:(添加自定义动画等)。
    didRotateFromInterfaceOrientation:(播放音乐、打开视图交互等)。
    

    视图控制器中的方法:

    //旋转的时候暂停⾳乐、关闭视图交互等
    //此方法已废弃
    //- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    //    [self.getbackView.textField resignFirstResponder];
    //}
    
    //Notifies the container that the size of its view is about to change
    //视图控制器里的视图的大小发生改变的时候,视图控制器会收到这个方法的通知(即这个方法会在此时自动执行)
    - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator{
        [self.getbackView.textField resignFirstResponder];
    }
    
    //Returns all of the interface orientations that the view controller supports.
    //返回所有视图控制器支持的方向(注意home键在上的情况默认不处理)
    //设置屏幕旋转时支持的方法
    - (NSUInteger)supportedInterfaceOrientations{
        return UIInterfaceOrientationMaskAll;
        //在这个方法的API中可找到以下枚举值,这里不详细介绍
        //UIInterfaceOrientationMaskAll 横屏和竖屏
        //UIInterfaceOrientationMaskLandscape 横屏
    }
    

    视图的处理:

    ****注意视图控制器会自动调整view的大小以适应屏幕旋转,bounds被修改, 触发view的layoutSubviews方法。
    view重写layoutSubviews方法,根据设备方向,重新布局。
    [UIApplication shareApplication].statusBarOrientation提供设备当前方向(获取应用程序的状态栏的方向)。
    

    视图中的方法:

    //只要视图本身的bounds发生变化,此方法就会执行
    - (void)layoutSubviews {
        //获取屏幕方向 UIInterfaceOrientation(枚举类型)
        UIInterfaceOrientation orientaion = [UIApplication sharedApplication].statusBarOrientation;
        //判断是否横向
        if (orientaion == UIInterfaceOrientationLandscapeLeft || orientaion ==UIInterfaceOrientationLandscapeRight) {
            self.getbackButton.frame = CGRectMake(300, 150, 100, 40);
        }else {
            self.getbackButton.frame = CGRectMake(150, 150, 100, 40);
        }
    }
    

    相关文章

      网友评论

          本文标题:iOS之屏幕旋转

          本文链接:https://www.haomeiwen.com/subject/tefsrttx.html