• 屏幕旋转


    视图控制器,本身可以检测到屏幕是否有在旋转,如果有屏幕旋转这个事件,处理这个旋转,需要重写一下几个方法即可。

    - (NSUInteger)supportedInterfaceOrientations

    这个方法是用来设置设备支持旋转的方向(这里说的旋转方式不是手机的旋转方向,而是屏幕里屏幕的旋转方向,因为屏幕内容与手机旋转方向是相反的)

    // 设备支持旋转方向

    - (NSUInteger)supportedInterfaceOrientations{

        return UIInterfaceOrientationMaskAll;

    }

    return回去的,是屏幕内容的旋转方向。

    willRotateToInterfaceOrientation:duration:

    将要旋转前要作的事,目前苹果已经弃用

    didRotateFromInterfaceOrientation:

    完成旋转后要做的事,目前苹果已经弃用。

    当检测到旋转后,应重启另一套布局,用来适应屏幕的宽高等。重要标志是bounds的值是否改变。如果改变,则会调用一个方法layoutSubviews。

    在这个方法里,可以对布局进行修改

    RootViewController.m

    视图控制器,检测到屏幕是否有旋转,如果旋转,则进行对应的处理。

    这里的旋转可以旋转几个方向,可以设置某些方向的旋转无效。return响应的方向即可

    #import "RootViewController.h"

    #import "MyView.h"

    @interface RootViewController ()

     

    @property(nonatomic,retain)MyView *mv;

     

    @end

     

    @implementation RootViewController

     

    - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{

        NSLog(@"将要旋转..");

    }

     

    - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{

        NSLog(@"旋转完成..");

    }

     

    // 设备支持旋转方向

    - (NSUInteger)supportedInterfaceOrientations{

        return UIInterfaceOrientationMaskAll;

    }

    - (void)loadView{

        self.mv = [[[MyView alloc]initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

        self.view = _mv;

    }

    - (void)dealloc

    {

        [_mv release];

        [super dealloc];

    }

    - (void)viewDidLoad {

        [super viewDidLoad];

    }

    // 接收到内存警告

    - (void)didReceiveMemoryWarning {

        [super didReceiveMemoryWarning];

        // 根视图已经加载过并且根视图没有显示在屏幕上

        if([self isViewLoaded]== YES && self.view.window == nil){

            // 将根视图销毁

            self.view = nil;

        }

    }

    @end

    MyView.m

    #import "MyView.h"

     

    @interface MyView ()

     

    @property(nonatomic,retain)UIView *v;

     

    @end

    @implementation MyView

    - (void)dealloc

    {

        [_v release];

        [super dealloc];

    }

     

    - (instancetype)initWithFrame:(CGRect)frame

    {

        self = [super initWithFrame:frame];

        if (self) {

            [self p_setupView];

        }

        return self;

    }

    - (void)p_setupView{

       

        self.backgroundColor = [UIColor greenColor];

        self.v = [[UIView alloc]initWithFrame:CGRectMake(50, 50, 230, 300)];

        self.v.backgroundColor = [UIColor grayColor];

        [self addSubview:_v];

       

    }

    // 当bounds发生改变的时候,自动调用此方法

    -(void)layoutSubviews{

        NSLog(@"bounds 发生改变");

        // 获取当前设备方向

        if (([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown) || ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait)) {

            self.v.frame = CGRectMake(50, 50, 230, 300);

        }else{

            self.v.frame = CGRectMake(100, 50, 300, 230);

        }

    }

    @end

    旋转后对布局的选择,适应屏幕

  • 相关阅读:
    第三章 学习ICE 3.0Slice语言
    腾讯
    Websvn的安装
    fedora下装eclipse
    linux快捷键
    windows下SVN解决方案
    用ICE实现一个简单的聊天室
    Tortoise SVN 客户端使用方法
    GCC安装
    在VC++6.0 IDE中配置ICE工程[ ICE FOR VC++6.0 ]
  • 原文地址:https://www.cnblogs.com/Coder-GT/p/4866417.html
Copyright © 2020-2023  润新知