UIControl

UIControl

  • 父类是UIView
  • 凡是继承自UIControl的控件,都可以通过addTarget:方法来监听事件

UIControl的常用方法->监听控件的行为

// UIControlEventTouchUpInside: 点击事件(UIcontrol适用)
// UIControlEventValueChanged : 值改变事件(UISwitch、UISegmentControl、UISlider适用)
// UIControlEventEditingChanged : 文字改变事件(UITextField适用)

[control addTarget:self action:@selector(controlClick:) forControlEvents:UIControlEventTouchUpInside];
  • IOS其他监听方法

UIControl常用属性

// 功能是否可用
control.enabled = YES;

// 是否选中
control.selected = NO;

// 是否高亮
control.highlighted = NO;

UIControl其他属性及方法

    @property(nonatomic) UIControlContentVerticalAlignment contentVerticalAlignment;     // how to position content vertically inside control. default is center
    @property(nonatomic) UIControlContentHorizontalAlignment contentHorizontalAlignment; // how to position content hozontally inside control. default is center

    @property(nonatomic,readonly) UIControlState state;                  // could be more than one state (e.g. disabled|selected). synthesized from other flags.
    @property(nonatomic,readonly,getter=isTracking) BOOL tracking;
    @property(nonatomic,readonly,getter=isTouchInside) BOOL touchInside; // valid during tracking only

    - (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; // touch is sometimes nil if cancelTracking calls through to this.
    - (void)cancelTrackingWithEvent:(nullable UIEvent *)event;   // event may be nil if cancelled for non-event reasons, e.g. removed from window

    // remove the target/action for a set of events. pass in NULL for the action to remove all actions for that target
    - (void)removeTarget:(nullable id)target action:(nullable SEL)action forControlEvents:(UIControlEvents)controlEvents;

    // get info about target & actions. this makes it possible to enumerate all target/actions by checking for each event kind
    - (NSSet *)allTargets;                                                                     // set may include NSNull to indicate at least one nil target
    - (UIControlEvents)allControlEvents;                                                       // list of all events that have at least one action
    - (nullable NSArray *)actionsForTarget:(nullable id)target forControlEvent:(UIControlEvents)controlEvent;    // single event. returns NSArray of NSString selector names. returns nil if none

    // send the action. the first method is called for the event and is a point at which you can observe or override behavior. it is called repeately by the second.
    - (void)sendAction:(SEL)action to:(nullable id)target forEvent:(nullable UIEvent *)event;
    - (void)sendActionsForControlEvents:(UIControlEvents)controlEvents;                        // send all actions associated with events
    ```

你可能感兴趣的:(UIControl)