target/action设计模式

1.target/actinon(目标行为)

学过UIButton按钮的响应事件都知道,当我们定义一个按钮的时候,会给它设置一个触发的方法来实现按钮触发后所要做的行为:

如:

[btn addTarget:selfaction:@selector(display:)forControlEvents:UIControlEventTouchUpInside];

target/action设计模式的原理跟UIButton的设计原理相似,触发对象后,调用为对象设计的方法,实现所要做的操作.


下面设计一个简单的traget/action模式,实现点击为自身视图随机改变背景颜色

1.创建一个UIView视图

ControlerView.h

#import 

@interface ControlerView : UIView
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
@end

ControlerView.m

#import "ControlerView.h"
#import "UIColor+RandonColor.h"
@interface ControlerView(){
    id _target;//目标
    SEL _action;//行为
    UIControlEvents _controlEvents;//触发时机
}
@end
@implementation ControlerView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}
//为当前视图指定当视图接受到响应事件之后,由target来通过action方法进行响应.
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents{
    //用实例变量存储外界传入的参数,方便在其他方法中使用.
    _target = target;
    _action = action;
    _controlEvents = controlEvents;
}
//开始触摸时触发
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    if (UIControlEventTouchDown == _controlEvents) {
        //当当前视图接收到触摸事件之后,交由target去处理
        [_target performSelector:_action withObject:self ];
    }
}
//结束触摸时触发
 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    if (UIControlEventTouchUpInside == _controlEvents) {
        [_target performSelector:_action withObject:self];
    }
}
@end

根视图文件

RootViewControlier.h

#import 

@interface RootViewController : UIViewController

@end
RootViewControlier.m

#import "RootViewController.h"
#import "ControlerView.h"
#import "UIColor+RandonColor.h"
@interface RootViewController ()

@end

@implementation RootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor grayColor];
    ControlerView *redView = [[ControlerView alloc] initWithFrame:CGRectMake(20, 40, 280,100)];
    redView.backgroundColor = [UIColor redColor];
    [redView addTarget:self action:@selector(kissCJ:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:redView];
    [redView release];
}
- (void)kissCJ:(ControlerView *)view{
    view.backgroundColor = [UIColor random];
}
//内存警告
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end

添加一个类目,实为视图添加随机颜色

UIColor +RandomColor.h

#import 

@interface UIColor (RandomColor)
+ (UIColor *)random;
@end

UIColor +RandomColor.m

#import "UIColor+RandomColor.h"

@implementation UIColor (RandomColor)
+ (UIColor *)random{
    return [self colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];
}
@end






你可能感兴趣的:(UI)