+ (UIColor *)randomColor
{
return [selfcolorWithRed:arc4random() %256 /255.0green:arc4random() %256 /255.0blue:arc4random() %256 /255.0alpha:1];
}
//第一创建三个UIView对象
// MHTViewController.m
// LessonTragetAction
// Copyright (c) 2014年 Summer. All rights reserved.
#import"MHTViewController.h"
#import"CustomView.h"
#import"UIColor+RandomColor.h"
#define kCyanView_Frame CGRectMake(100,130, 120, 120)
#define kGreenView_Frame CGRectMake(15,260, 120, 120)
#define kGrayView_Frame CGRectMake(185,260, 120, 120)
@interfaceMHTViewController ()
@end
@implementation MHTViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNilbundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[superviewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColoryellowColor];
//view1
CustomView *greenView = [[CustomViewalloc]initWithFrame:kGreenView_Frame];
greenView.backgroundColor = [UIColorgreenColor];
greenView.layer.cornerRadius =60;
[greenViewaddTarget:selfaction:@selector(changeSuperviewColor:)forControlEvents:UIControlEventTouchUpInside];
greenView.layer.masksToBounds =YES;
[self.viewaddSubview:greenView];
[greenViewrelease];
//view2
CustomView *cyanView = [[CustomViewalloc]initWithFrame:kCyanView_Frame];
cyanView.backgroundColor = [UIColorcyanColor];
[self.viewaddSubview:cyanView];
cyanView.layer.cornerRadius =60;
[cyanViewaddTarget:selfaction:@selector(changeSelfColor:)forControlEvents:UIControlEventTouchDown];
cyanView.layer.masksToBounds =YES;
[cyanViewrelease];
//view3
CustomView*grayView = [[CustomViewalloc]initWithFrame:kGrayView_Frame];
grayView.backgroundColor = [UIColorgrayColor];
[self.viewaddSubview:grayView];
[grayViewaddTarget:selfaction:@selector(changeSelfLocation:)forControlEvents:UIControlEventTouchUpInside];
grayView.layer.cornerRadius =60;
grayView.layer.masksToBounds =YES;
[grayViewrelease];
NSArray *titles =@[@"更改父视图的颜色" ,@"更改自身的颜色" ,@"更改自身的位置"];
for (int i = 0; i < 3; i++) {
UILabel *lable = [[UILabelalloc]initWithFrame:CGRectMake(0,0,120, 120)];
lable.layer.cornerRadius =60;
lable.layer.masksToBounds =YES;
lable.text = titles[i];
lable.font = [UIFontfontWithName:@"EuphemiaUCAS"size:15];
lable.textAlignment =NSTextAlignmentCenter;
lable.backgroundColor = [UIColorclearColor];
if (0 == i) {
[greenViewaddSubview:lable];
}elseif (1 == i){
[cyanViewaddSubview:lable];
}elseif (2 == i){
[grayViewaddSubview:lable];
}
[lablerelease];
//设置分割线
UILabel *hintLine = [[UILabelalloc]initWithFrame:CGRectMake(50,80,220, 2)];
hintLine.backgroundColor = [UIColorgrayColor];
hintLine.layer.cornerRadius =5;
hintLine.layer.masksToBounds =YES;
[self.viewaddSubview:hintLine];
[hintLinerelease];
//设置功能的提示内容
UILabel *hintLable = [[UILabelalloc]initWithFrame:CGRectMake(50,60,220, 20)];
hintLable.textAlignment =NSTextAlignmentCenter;
hintLable.text =@"文本显示:";
hintLable.font = [UIFontfontWithName:@"AlNile"size:12];
[self.viewaddSubview:hintLable];
[hintLablerelease];
//copyrightLine(版权)
UILabel *copyrightLine = [[UILabelalloc]initWithFrame:CGRectMake(20,520,280, 2)];
copyrightLine.backgroundColor = [UIColorgrayColor];
copyrightLine.layer.cornerRadius =5;
copyrightLine.layer.masksToBounds =YES;
[self.viewaddSubview:copyrightLine];
[copyrightLinerelease];
UILabel *copyrightLable = [[UILabelalloc]initWithFrame:CGRectMake(20,480,280, 20)];
copyrightLable.textAlignment =NSTextAlignmentCenter;
copyrightLable.text =@"Copyright ? 2014 summer";
copyrightLable.font = [UIFontfontWithName:@"AlNile"size:12];
[self.viewaddSubview:copyrightLable];
[copyrightLablerelease];
UILabel *copyrightLable1 = [[UILabelalloc]initWithFrame:CGRectMake(20,500,280, 20)];
copyrightLable1.textAlignment =NSTextAlignmentCenter;
copyrightLable1.text =@"summer2014mht@sina.com";
copyrightLable1.font = [UIFontfontWithName:@"AlNile"size:12];
[self.viewaddSubview:copyrightLable1];
[copyrightLable1release];
}
//修改父视图的颜色
- (void)changeSuperviewColor:(CustomView *)view
{
view.superview.backgroundColor = [UIColorrandomColor];
}
//修改自身的颜色
- (void)changeSelfColor:(CustomView *)view
{
view.backgroundColor = [UIColorrandomColor];
}
//修改自身的位置
- (void)changeSelfLocation:(CustomView *)view
{
view.center =CGPointMake(arc4random() % (260 -60 +1) + 60,arc4random() % (420 -60 + 1) + 60);
}
第二 构建Target-Action
// CustomView.h
// LessonTragetAction
// Copyright (c) 2014年 Summer. All rights reserved.
#import<UIKit/UIKit.h>
@interface CustomView :UIView
//创建方法
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
@end
//*********************
// CustomView.m
// LessonTragetAction
// Copyright (c) 2014年 Summer. All rights reserved.
#import"CustomView.h"
#import"UIColor+RandomColor.h"
@interfaceCustomView()
{
id _target; //目标
SEL _action; //行为
UIControlEvents _controlEvents;
}
@end
@implementation CustomView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if (UIControlEventTouchDown ==_controlEvents) {
//当当前视图接触到触摸事件之后,交由target去处理
[_targetperformSelector:_actionwithObject:self];
}
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if (UIControlEventTouchUpInside ==_controlEvents) {
[_targetperformSelector:_actionwithObject:self];
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
}
//为当前视图指定当视图接收到响应事件之后,由target来通过action方法进行响应.
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
{
//用实例变量存储外界传入的参数,方便在其他方法中使用.
_target = target;
_action = action;
_controlEvents = controlEvents;//触发时机
}
@end
原文地址:http://blog.csdn.net/mhtios/article/details/38947121