屏幕旋转

屏幕旋转

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

1.supportedinterfaceOrientations(设置设备支持的旋转方向)

2. willRotateTointerfaceOrientation:duration(暂停音乐, 关闭视图交互等)

3.willAnimateRotationTointerfaceOrientation:duration:(添加自定义动画等)

4.didRotationFrominterfaceOrientation:(播放音乐, 打开视图交互等)

设备默认的视图有三种:竖屏(home键在下面, 左屏(home键在右)和右屏(home键在左应用程序的默认方向是竖直向下的, 如果要改变默认方向, 在工程中选中工程, General -> Deployment Info -> Fevice Orientation选择支持的手势有哪儿些, 第一个点击到得会是程序的默认手势, 在info.plist的Supported interface orientations中可以查看到所添加的方向, item0的是默认方向

-(NSUInteger)supportedInterfaceOrientations{

returnUIInterfaceOrientation + enum;

}

通过上面的方法可以设置当前视图控制器所支持的屏幕方向, 这里注意如果要使用向下方法的话要先在工程中勾选, 因为默认支持的是三种.

视图控制器会自动调整view的大小以适应屏幕旋转, bounds被修改, 触发view的layoutSubview方法.

下面是一个小demo 的代码

-(instancetype)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];

    if (self) {

        self.backgroundColor = [UIColoryellowColor];

        self.textField.placeholder = @"请输入文字";

    }

    return self;

}

 

View重写layoutSubviews方法 根据设备方向, 重新布局

-(void)layoutSubviews{

    [super layoutSubviews];

   NSLog(@"%@",NSStringFromCGRect(self.bounds));

   

    //获取当前屏幕的方向, 根据屏幕的方向来设定子视图的位置

    UIInterfaceOrientation orientation =[UIApplication sharedApplication].statusBarOrientation;

   

    switch (orientation) {

        case UIInterfaceOrientationPortrait:

        caseUIInterfaceOrientationPortraitUpsideDown:

        {

            self.textField.frame =CGRectMake(20, 60, CGRectGetWidth(self.bounds) - 40,40);

        }

            break;

        caseUIInterfaceOrientationLandscapeLeft:

        caseUIInterfaceOrientationLandscapeRight:

        {

            CGRect textFieldFrame = self.textField.frame;

            textFieldFrame.origin.x =(CGRectGetWidth(self.bounds) - CGRectGetWidth(textFieldFrame)) / 2;

            self.textField.frame =textFieldFrame;

        }

            break;

           

        default:

            break;

    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值