代码实例:
//
// ViewController.m
// IOS150702_UI层的说明
//
// Created by PengJunlong on 15/7/2.
// Copyright (c) 2015年 Peng Junlong. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 70, 70)];
topView.backgroundColor = [UIColor greenColor];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem ];
btn.frame = CGRectMake(30, 30, 100, 100);
btn.backgroundColor = [UIColor blueColor];
[btn setTitle:@"按钮" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(changeViewColor) forControlEvents:UIControlEventTouchUpInside];
[topView addSubview:btn];
[self.view addSubview:topView];
}
- (void)changeViewColor
{
self.view.backgroundColor = [UIColor redColor];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
结果:
蓝色的button是放在绿色的UIView上的,且Button的大小已经超出UIView的范围,点击绿色范围之外的部分,按钮没有任何反应,因为该部分已经超出范围.点击绿色和蓝色交叉的部分时会相应按钮的事件,修改View的背景色为红色
UIView的MVC模式练习:
点击下一条会随机改变条形的长度,使用MVC模式实现
//
// View.h
// IOS150701_UI_条形图MVC练习
//
// Created by PengJunlong on 15/7/2.
// Copyright (c) 2015年 Peng Junlong. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface View : UIView
- (id)initWithFrame:(CGRect)frame addTarget:(id)target action:(SEL)action;
- (void)UpdateViewByArray:(NSArray *)arr;
@end
//
// View.m
// IOS150701_UI_条形图MVC练习
//
// Created by PengJunlong on 15/7/2.
// Copyright (c) 2015年 Peng Junlong. All rights reserved.
//
#import "View.h"
@implementation View
- (id)initWithFrame:(CGRect)frame addTarget:(id)target action:(SEL)action
{
if (self = [super initWithFrame:frame]) //不能使用init,使用init初始化时,得到的self大小是{(0.0),(0,0)};
//添加的子视图超出父视图,点击超出范围的部分则没有任何反应,点击父视图范围内的则有反应
{
UIView *bgview = [[UIView alloc] initWithFrame:frame];
bgview.backgroundColor = [UIColor cyanColor];
bgview.tag = 100;
CGFloat size = (frame.size.height-80)/12;
for (int i=0; i<12; i++)
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, i*size+20, 50, size-10)];
label.text = [NSString stringWithFormat:@"%d",i+1];
label.backgroundColor = [UIColor grayColor];
label.alpha = 0.7;
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor redColor];
[bgview addSubview:label];
UIView *stickView = [[UIView alloc] initWithFrame:CGRectMake(50, i*size+20, 0, size-10)];
stickView.backgroundColor = [UIColor blueColor];
stickView.tag = i+1;
[bgview addSubview:stickView];
}
UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeSystem];
nextButton.frame = CGRectMake(50, frame.size.height-50, frame.size.width-100, 30);
[nextButton setTitle:@"下一条" forState:UIControlStateNormal];
nextButton.backgroundColor = [UIColor grayColor];
[nextButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
[bgview addSubview:nextButton];
[self addSubview:bgview];
}
return self;
}
- (void)UpdateViewByArray:(NSArray *)arr
{
UIView *bgView = (UIView *)[self viewWithTag:100];
for ( int i=0; i<12; i++)
{
UIView *view = [bgView viewWithTag:i+1];
CGRect frame = view.frame;
frame.size.width = [[arr objectAtIndex:i] intValue];
view.frame = frame;
}
}
@end
MVC中的Model:
//
// DataModel.h
// IOS150701_UI_条形图MVC练习
//
// Created by PengJunlong on 15/7/2.
// Copyright (c) 2015年 Peng Junlong. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface DataModel : NSObject
{
NSMutableArray *_dataArray;
}
- (id)init;
- (NSArray *)updateModel;
@end
//
// DataModel.m
// IOS150701_UI_条形图MVC练习
//
// Created by PengJunlong on 15/7/2.
// Copyright (c) 2015年 Peng Junlong. All rights reserved.
//
#import "DataModel.h"
@implementation DataModel
- (id)init
{
if (self = [super init])
{
_dataArray = [NSMutableArray array];
for (int i=0; i<12; i++)
{
[_dataArray addObject:[NSNumber numberWithInt:0]];
}
}
return self;
}
- (NSArray *)updateModel
{
for (int i=0; i<12; i++)
{
NSNumber *number = [NSNumber numberWithInt:arc4random()%300];
[_dataArray replaceObjectAtIndex:i withObject:number];
}
return _dataArray;
}
@end
MVC中的Controller:
//
// ViewController.h
// IOS150701_UI_条形图MVC练习
//
// Created by PengJunlong on 15/7/2.
// Copyright (c) 2015年 Peng Junlong. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
//
// ViewController.m
// IOS150701_UI_条形图MVC练习
//
// Created by PengJunlong on 15/7/2.
// Copyright (c) 2015年 Peng Junlong. All rights reserved.
//
#import "ViewController.h"
#import "View.h"
#import "DataModel.h"
@interface ViewController ()
{
View *_view;
DataModel *_datamodel;
}
@end
//MVC设计模式
//Model/View/Controller
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self createModel];
[self createUI];
[self btnRefrehsView];
}
- (void)createModel
{
_datamodel = [[DataModel alloc] init];
}
- (void)createUI
{
CGRect frame = self.view.frame;
frame.size.height = self.view.frame.size.height-80;
_view = [[View alloc] initWithFrame:frame addTarget:self action:@selector(btnRefrehsView)];
[self.view addSubview:_view];
}
- (void)btnRefrehsView
{
NSLog(@"点击");
[_view UpdateViewByArray:[_datamodel updateModel]];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end