//
// 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