iOS UIControl

自定义UIControl实现特定事件的消息分发与自定义跟踪行为
本博客介绍如何通过继承自UIView的UIControl类来自定义控件,实现针对特定事件的消息分发和自定义跟踪行为。通过重写指定方法,用户可以控制控件在接收到不同事件时的行为,如点击、触摸等。包括如何通过`sendAction:to:forEvent:`方法实现按次数触发消息分发,以及如何通过`beginTrackingWithTouch:withEvent:`, `continueTrackingWithTouch:withEvent:`, `endTrackingWithTouch:withEvent:`方法来定制触摸事件的处理流程。

提要:

UIControl 是基于Target-Action模式的控件的基类

不应该直接使用

继承自UIView

内容:

UIControl可以实现自定义控件,支持 subclass

Apple Doc给extend  UIControl的建议:

1.针对于特定实践,观察or修改(实现想怎么调用就怎么调用),action消息的分发。

2.提供自定义跟踪行为(想让控件有什么样的track行为)

You may want to extend a UIControl subclass for either of two reasons:

代码举例

实现控件点击3才有消息分发即(方法调用)

- (void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event {

    if (touchTime == 3) {

        touchTime = 0;

        if ([target respondsToSelector:action]) {

            #pragma clang diagnostic push

            #pragma clang diagnostic ignored "-Warc-performSelector-leaks"

            [target performSelector:action];

            #pragma clang diagnostic pop

        }

    }

}

- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {

    NSLog(@"%s", __FUNCTION__);

    BOOL isBegin = NO;

    if (++touchTime < 3) {

        isBegin = NO;

    } else {

        isBegin = YES;

    }

    return isBegin;

}

- (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {

    NSLog(@"%s", __FUNCTION__);

}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值