你真的瞭解UIControl嗎?,UIControl

來源:互聯網
上載者:User

你真的瞭解UIControl嗎?,UIControl

一:首先查看一下關於UIControl的定義

NS_CLASS_AVAILABLE_IOS(2_0) @interface UIControl : UIView//控制項預設是啟用的YES。是否要禁用控制項@property(nonatomic,getter=isEnabled) BOOL enabled;                                  @property(nonatomic,getter=isSelected) BOOL selected;                                // 預設值NO 當使用者選中控制項時,UIControl類會將其selected屬性設定為YES。子類有時使用這個屬性來讓控制項選擇自身,或者來表現不同的行為方式。@property(nonatomic,getter=isHighlighted) BOOL highlighted;                          // 預設是NO。這是設定/清除自動當觸摸進入/退出在跟蹤過程中,並清除//控制項如何在垂直方向上布置自身的內容。預設是將內容頂端對其@property(nonatomic) UIControlContentVerticalAlignment contentVerticalAlignment;  //水平對齊@property(nonatomic) UIControlContentHorizontalAlignment contentHorizontalAlignment; //當前所處的UIControlState狀態 唯讀@property(nonatomic,readonly) UIControlState state;                  //為了判斷當前對象是否正在追蹤觸摸操作,該值如果為YES,則表明正在追蹤。唯讀@property(nonatomic,readonly,getter=isTracking) BOOL tracking;//為了判斷當前觸摸點是否在控制項地區類,可以使用touchInside屬性,這是個唯讀屬性@property(nonatomic,readonly,getter=isTouchInside) BOOL touchInside; //跟蹤觸摸事件- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(nullable UIEvent *)event;- (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(nullable UIEvent *)event;- (void)endTrackingWithTouch:(nullable UITouch *)touch withEvent:(nullable UIEvent *)event; - (void)cancelTrackingWithEvent:(nullable UIEvent *)event;  //增加Target- (void)addTarget:(nullable id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;//移除Target- (void)removeTarget:(nullable id)target action:(nullable SEL)action forControlEvents:(UIControlEvents)controlEvents;//擷取控制項對象所有相關的target對象,則可以調用allTargets方法,該方法返回一個集合。集合中可能包含NSNull對象,表示至少有一個nil目標對象- (NSSet *)allTargets;//獲得最後一個action的所有Events                                                                     - (UIControlEvents)allControlEvents;                                                       //擷取某個target對象及事件相關的所有action,則可以調用- (nullable NSArray<NSString *> *)actionsForTarget:(nullable id)target forControlEvent:(UIControlEvents)controlEvent;    //來將行為訊息轉寄到UIApplication對象,再由UIApplication對象調用其sendAction:to:fromSender:forEvent:方法來將訊息分發到指定的target上,而如果我們沒有指定target,則會將事件分發到響應鏈上第一個想處理訊息的對象上。而如果子類想監控或修改這種行為的話,則可以重寫這個方法。- (void)sendAction:(SEL)action to:(nullable id)target forEvent:(nullable UIEvent *)event;//方法的作用是發送與指定類型相關的所有行為訊息- (void)sendActionsForControlEvents:(UIControlEvents)controlEvents;                       @end

UIControl是繼承於UIView,當然也是UIResponder的子類。UIControl是諸如UISwitch、UIButton、UISegmentedControl、UISlider、UITextField、UIPageControl等控制項的父類,它本身也包含了一些屬性和方法,但是不能直接使用UIControl類,它只是定義了子類都需要使用的方法。UIControl是控制項類的基類,它是一個抽象基類,我們不能直接使用UIControl類來執行個體化控制項,它只是為控制項子類定義一些通用的介面,並提供一些基礎實現,以在事件發生時,預先處理這些訊息並將它們發送到指定目標對象上。UIControl對象採用了一種新的事件處理機制,將觸摸事件轉換成簡單操作,這樣可以無需關心使用者訪問控制項的具體方式。

知識點1:addTarget:action:forControlEvents

這是UIControl的一個方法,為指定的控制項對象添加事件,例如:[controlObj addTarget:recepientObj action @selector(method) froControlEvents :UIControlEvents]; controlObj是要響應事件的控制項對象;參數receientObj是要把訊息發送到哪裡,一般是self,通常指執行個體化控制項對象的控制器;action後面是一個選取器,表示該事件需要響應的方法,事件做什麼其實就寫在這個方法裡面;最後一個是事件類型,表示響應什麼樣的事件。

知識點2:代碼類比使用者點擊

類比UI的事件sendActionsForControlEvents,比如類比使用者點擊事件: 

[myBtn sendActionsForControlEvents:UIControlEventTouchUpInside];
執行個體:- (void)viewDidLoad {    // ...    [control addTarget:self action:@selector(tapImageControl:) forControlEvents:UIControlEventTouchUpInside];     [control sendActionsForControlEvents:UIControlEventTouchUpInside];}

可以看到在未點擊控制項的情況下,觸發了UIControlEventTouchUpInside事件

知識點3:要刪除一個或多個事件的相應動作,可以使用UIControl類的removeTarget方法。使用nil值就可以將給定事件目標的所有動作刪除:

[ myControl removeTarget:myDelegate                     action:nil                    forControlEvents:UIControlEventAllEvents];  

知識點4:重寫sendAction的運用

/ ImageControl.m- (void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event {  // 將事件傳遞到對象本身來處理    [super sendAction:@selector(handleAction:) to:self forEvent:event];}- (void)handleAction:(id)sender {    NSLog(@"handle Action");}// ViewController.m- (void)viewDidLoad {    [super viewDidLoad];    self.view.backgroundColor = [UIColor whiteColor];    ImageControl *control = [[ImageControl alloc] initWithFrame:(CGRect){50.0f, 100.0f, 200.0f, 300.0f} title:@"This is a demo" image:[UIImage imageNamed:@"demo"]];    // ...    [control addTarget:self action:@selector(tapImageControl:) forControlEvents:UIControlEventTouchUpInside];}- (void)tapImageControl:(id)sender {    NSLog(@"sender = %@", sender);}

由於我們重寫了sendAction:to:forEvent:方法,所以最後處理事件的Selector是ImageControl的handleAction:方法,而不是ViewController的tapImageControl:方法。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.