你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS UIControl

iOS UIControl

編輯:IOS開發綜合

提要:

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__);

    }



     

     

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved