iOS UIControl

来源:互联网 发布:知错与改错议论文 编辑:程序博客网 时间:2024/05/21 18:40

提要:

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:

  • To observe or modify the dispatch of action messages to targets for particular events

    To do this, override sendAction:to:forEvent:, evaluate the passed-in selector, target object, or UIControlEvents bit mask, and proceed as required.

  • To provide custom tracking behavior (for example, to change the highlight appearance)

    To do this, override one or all of the following methods: beginTrackingWithTouch:withEvent:continueTrackingWithTouch:withEvent:endTrackingWithTouch:withEvent:.

代码举例

实现控件点击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__);

}





0 0