屏幕适配






//
//  RootViewController.m
//  屏幕适配
//
//  Created by zm on 14-11-25.
//  Copyright (c) 2014年 practice. All rights reserved.
//

#import "RootViewController.h"

@interface RootViewController ()
{
    UIView *_view;
    CGSize frameSize;
    NSMutableArray *_viewsArray;
}
@end

@implementation RootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
         /**
            *   frameSize = [[UIScreen mainScreen] bounds].size  无论何时获取都是一样的
            *   当屏幕旋转时,要手动设置frameSize,将宽高数值对调
            */
        frameSize = [[UIScreen mainScreen] bounds].size;
        _viewsArray = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // 屏幕初次显示,判断屏幕朝向,如果不是垂直,而是是水平方向的,修改 frameSize
    if (self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft||
        self.interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
        frameSize = CGSizeMake(frameSize.height, frameSize.width);
    }
    
    //   4 列  3行
    [self createViewsWithNumX:4 andNumY:3];
    //[self createFourView];
}

//   上下左右四个视图
-(void)createFourView
{
    float offsetX = 30;
    float offsetY = 60;
    float x = offsetX;
    float y = offsetY;
    float width = 100;
    float height = 30;
    
    [self createView:CGRectMake(x, y, width, height) withColor:[UIColor lightGrayColor]];
    
    x = frameSize.width - width - x;
    [self createView:CGRectMake(x, y, width, height) withColor:[UIColor lightGrayColor]];
    
    y = frameSize.height - height - y;
    x = offsetX;
    [self createView:CGRectMake(x, y, width, height) withColor:[UIColor lightGrayColor]];
    
    x = frameSize.width - width - x;
    [self createView:CGRectMake(x, y, width, height) withColor:[UIColor lightGrayColor]];
}

/**
 *  根据 numX 每行个数,   :    列数
 *      numY 每列有几个   :    行数
 */
-(void)createViewsWithNumX:(int)numX andNumY:(int)numY
{
    float widht = 100;
    float height = 50;
//    float numX = 3;
//    float numY = 3;
    
    float offsetX = (frameSize.width - numX*widht)/(numX+1);
    float offsetY = (frameSize.height - numY*height)/(numY+1);
    float x = offsetX;
    float y = offsetY;
    
    for (int i = 0 ; i < numY; i++) {
        
        for (int j = 0; j < numX; j++) {
            
            [self createView:CGRectMake(x, y, widht, height) withColor:[UIColor grayColor]];
            x += widht+offsetX;
        }
        
        x = offsetX;
        y += height+offsetY;
    }
}

-(void)createView:(CGRect) frame withColor:(UIColor *)color
{
    UIView *view = [[UIView alloc] initWithFrame:frame];
    view.backgroundColor = color;
    [self.view addSubview:view];
    
    [_viewsArray addObject:view];
    //return view;
}


/**
 * 控制屏幕旋转
 * 返回YES表示可以旋转,否则不可以旋转
 */
-(BOOL)shouldAutorotate
{
    return YES;
}


//屏幕将要旋转到toInterfaceOrientation
/**
 * toInterfaceOrientation将要旋转到的方向
 */

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    //移除所有子视图
    for (UIView *view in self.view.subviews) {
        [view removeFromSuperview];
    }
    
    frameSize = [[UIScreen mainScreen] bounds].size;
    
    //设备水平放置
    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight||
        toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
        
        frameSize = CGSizeMake(frameSize.height, frameSize.width);
    }
    
    //根据新的frameSize,重画视图  旋转屏幕,宽高数值对调
    //[self createFourView];
    [self createViewsWithNumX:4 andNumY:3];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end


 






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值