触摸的target-action delegate设计思想的运用

6 篇文章 0 订阅

目标:当点击一个类的视图对象的时候,做一些响应,点击该类不同的视图对象做出的响应不同

而触发时机是点击该视图时,所以在- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event这个方法让代理去执行任务

1.定义协议

@class TouchView;//当定义协议的时候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


2.遵守协议

@interface DelegateViewController ()<TouchViewDelegate>


@end



3.设置代理

- (void)viewDidLoad

{

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor cyanColor];

    TouchView *redView = [[TouchView alloc]initWithFrame:CGRectMake(20, 50, 280, 40)];

    redView.backgroundColor = [UIColor redColor];

    redView.tag = 100;

        //为视图指定代理,来处理视图的触摸事件

    redView.delegate = self;

    [self.view addSubview:redView];

    [redView release];

    

    

    

    TouchView *blueView = [[TouchView alloc]initWithFrame:CGRectMake(20, 120, 280, 40)];

    blueView.backgroundColor = [UIColor blueColor];

    blueView.tag = 101;

    blueView.delegate = self;

    [self.view addSubview:blueView];

    [blueView release];

    

        //grayView

    TouchView *grayView = [[TouchView alloc]initWithFrame:CGRectMake(20, 450, 280, 100)];

    grayView.backgroundColor = [UIColor grayColor];

    grayView.tag = 102;

    grayView.delegate = self;

    [self.view addSubview:grayView];

    [grayView release];

}

4.让代理做事情的一个时机

//代理设计模式思想:对于当前视图对象,只负责接收触摸事件,当触摸事件发生之后,通知代理做相应处理,代理如何处理视图不关心


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

        //原理:

        //如果代理实现了协议的方法,就让代理调用方法,如果没有实现就不要调用

        //判断代理是否实现了对应的方法(判断一个对象是否实现某个特定的方法)

    if ([self.delegate respondsToSelector:@selector(touchViewTouchesBegan:)]) {

            //触摸开始时通知代理做相应的操作

        [self.delegate touchViewTouchesBegan:self];

    }

    

}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

{

        //respondsToSelector:@selector 返回BOOL类型

    if ([self.delegate respondsToSelector:@selector(touchViewTouchesMoved:)]) {

            //触摸移动时通知代理做相应的操作

        [self.delegate touchViewTouchesMoved:self];

    }

    

}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

{

    if ([self.delegate respondsToSelector:@selector(touchViewTouchesEnded:)]) {

            //触摸结束时通知代理做相应的操作

        [self.delegate touchViewTouchesEnded:self];

    }

    

}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

{

    if ([self.delegate respondsToSelector:@selector(touchViewTouchesCancelled:)]) {

            //触摸取消时通知代理做相应的操作

        [self.delegate touchViewTouchesCancelled:self];

    }

    

}

5.代理做的事情

- (void)touchViewTouchesBegan:(TouchView *)touchView

{


    //匹配的用switch

    switch (touchView.tag) {

        case 100:

            touchView.backgroundColor = [UIColor randomColor];

            break;

        case 101:

            touchView.superview.backgroundColor = [UIColor randomColor];

            break;

        case 102:

            touchView.center = CGPointMake(arc4random() % 200 + 100, arc4random() % 400 + 100);

            break;

            

        default:

            break;

    }

}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值