一.target --action设计模式
1
.创建视图控制器,在视图控制器.m文件中,创建CustomView (UIView的子类)的对象
CustomView *blueView = [[CustomView alloc] initWithFrame:CGRectMake(
20
,
180
,
280
,
100
)];
blueView.backgroundColor = [UIColor blueColor];
[blueView addTarget:self action:
@selector
(changeLocat:)forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:blueView];
[blueView release];
2
.在CustomView.h文件中定义方法
- (
void
)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;(这个方法和UIButton类中相似)
3
.在CustomView.m文件中,
@interface
CustomView ()
{
id _target;
SEL _action;
UIControlEvents _controlEvents;
}
@end
@implementation
CustomView
- (id)initWithFrame:(CGRect)frame
{
self = [
super
initWithFrame:frame];
if
(self) {
}
return
self;
}
- (
void
)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
当当前视图接收到触摸事件之后,交由target去处理
if
( UIControlEventTouchDown==_controlEvents) {
[_target performSelector:_action withObject:self];
}
}
- (
void
)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if
(UIControlEventTouchUpInside ==_controlEvents) {
[_target performSelector:_action withObject: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;
}
4
.在视图控制器的.m文件中,实现调用的方法
- (
void
)changeSupColor:(CustomView *)view
{
view.superview.backgroundColor = [UIColor random];
}
二.delegate设计模式
1
.代理设计模式的思想:对于当前视图对象,只负责接收触摸事件,当触摸事件发生之后,通知代理做相应处理,代理如何处理,视图不关心.
2
.创建视图控制,在其.m文件中,定义TouchView(UIView的子类)类的对象
TouchView *redView = [[TouchView alloc] initWithFrame:CGRectMake(
20
,
100
,
280
,
100
)];
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView];
redView.tag =
100
;
redView.delegate = self;
[redView release];
3
.在TouchView .h文件,定义协议以及方法和属性
@class
TouchView;
@protocol
TouchViewDelegate <nsobject>
@optional
- (
void
)touchViewTouchesBegan:(TouchView *)touchView;
- (
void
)touchViewTouchesMoved:(TouchView *)touchView;
- (
void
)touchViewTouchesEnded:(TouchView *)touchView;
- (
void
)touchViewTouchesCancelled:(TouchView *)touchView;
@end
@interface
TouchView : UIView
@property
(nonatomic , assign) id<touchviewdelegate> delegate;
@end
4
.在TouchView .m文件
代理设计模式的思想:对于当前视图对象,只负责接收触摸事件,当触摸事件发生之后,通知代理做相应处理,代理如何处理,视图不关心.
(
1
).- (
void
)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if
([self.delegate respondsToSelector:
@selector
(touchViewTouchesBegan:)]) {
[self.delegate touchViewTouchesBegan:self];
}
}
(
2
).- (
void
)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if
([self.delegate respondsToSelector:
@selector
(touchViewTouchesEnded:)]) {
[self.delegate touchViewTouchesEnded:self];
}
}
(
3
).-(
void
)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
if
([self.delegate respondsToSelector:
@selector
(touchViewTouchesMoved:)]) {
[self.delegate touchViewTouchesMoved:self];
}
}
(
4
).- (
void
)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
if
([self.delegate respondsToSelector:
@selector
(touchViewTouchesCancelled:)]) {
[self.delegate touchViewTouchesCancelled:self];
}
}
5
.在视图控制器的.m文件中,实现方法.</touchviewdelegate></nsobject>