<span style="font-size:18px;">#import "CustomViewDelegate.h"
typedef NS_ENUM(int, MyCustomViewType){
MyCustomViewType_Down=0,
MyCustomViewType_Up,
MyCustomViewType_Drag
};
@interface CustomView : UIView
{
id _target;
SEL _action;
MyCustomViewType _eventType;
}
- (void)addTarget:(id)target action:(SEL)action forMyEventType:(MyCustomViewType)eventType;
@end
</span>
<span style="font-size:18px;">@implementation CustomView
- (void)addTarget:(id)target action:(SEL)action forMyEventType:(MyCustomViewType)eventType
{
_target=target;
_action=action;
_eventType=eventType;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if (_eventType==MyCustomViewType_Down) {
[_target performSelector:_action withObject:self];
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
if (_eventType==MyCustomViewType_Drag) {
[_target performSelector:_action withObject:self];
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if (_eventType==MyCustomViewType_Up) {
[_target performSelector:_action withObject:self];
}
}
@end
</span>