首页 > 代码库 > target - action设计模式的思想
target - action设计模式的思想
不同的实例点击效果不同:点击改变自身颜色,点击改变父视图颜色,点击修改视图位置.以上效果可由target - action设计模式实现.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor yellowColor];
CustomView *greenView = [[CustomView alloc] initWithFrame:CGRectMake(20, 40, 280, 100)];
greenView.backgroundColor = [UIColor greenColor];
[greenView addTarget:self acton:@selector(changeColor:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:greenView];
[greenView release];
CustomView *redView = [[CustomView alloc] initWithFrame:CGRectMake(20, 200, 280, 100)];
redView.backgroundColor = [UIColor redColor];
[redView addTarget:self acton:@selector(changeSuperviewColor:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:redView];
[redView release];
CustomView *blueView = [[CustomView alloc] initWithFrame:CGRectMake(20, 360, 280, 100)];
blueView.backgroundColor = [UIColor blueColor];
[blueView addTarget:self acton:@selector(changelocation:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:blueView];
[blueView release];
}
- (void)changeColor:(CustomView *)view
{
view.backgroundColor = [UIColor randomColor];
}
- (void)changeSuperviewColor:(CustomView *)view
{
view.superview.backgroundColor = [UIColor randomColor];
}
- (void)changelocation:(CustomView *)view
{
view.center = CGPointMake(arc4random() % 200 + 100, arc4random() % 400 + 100);
}
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor yellowColor];
CustomView *greenView = [[CustomView alloc] initWithFrame:CGRectMake(20, 40, 280, 100)];
greenView.backgroundColor = [UIColor greenColor];
[greenView addTarget:self acton:@selector(changeColor:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:greenView];
[greenView release];
CustomView *redView = [[CustomView alloc] initWithFrame:CGRectMake(20, 200, 280, 100)];
redView.backgroundColor = [UIColor redColor];
[redView addTarget:self acton:@selector(changeSuperviewColor:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:redView];
[redView release];
CustomView *blueView = [[CustomView alloc] initWithFrame:CGRectMake(20, 360, 280, 100)];
blueView.backgroundColor = [UIColor blueColor];
[blueView addTarget:self acton:@selector(changelocation:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:blueView];
[blueView release];
}
- (void)changeColor:(CustomView *)view
{
view.backgroundColor = [UIColor randomColor];
}
- (void)changeSuperviewColor:(CustomView *)view
{
view.superview.backgroundColor = [UIColor randomColor];
}
- (void)changelocation:(CustomView *)view
{
view.center = CGPointMake(arc4random() % 200 + 100, arc4random() % 400 + 100);
}
@interface CustomView ()
{
id _target; //目标
SEL _action; //行为
UIControlEvents _controlEvents;
}
@end
{
id _target; //目标
SEL _action; //行为
UIControlEvents _controlEvents;
}
@end
//为当前视图指定当视图接收到响应事件之后,由target来通过action方法进行响应.
- (void)addTarget:(id)target acton:(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];
}
}
- (void)addTarget:(id)target acton:(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];
}
}
target - action设计模式的思想
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。