iOS-UIControl官方API大总结

概述:

  • 类继承关系
    NSObject->UIResponder->UIView->UIControl->(UIButton
    UIDatePicker/UIPageControl/UIRefreshControl/UISegmentedControl/UISlider/UIStepper/UISwitch/UITextField)
    )

UIView及其子类的继承关系图.png

  • 该类常用来对用户的交互做出视觉可看到的行为或意图,常见的有UIButton,UISlider,UITextField等.
  • 采用Taget-Action的机制来进行人机交互.
  • 不必直接创建UIControl,其已被子类化各种控件,你直接使用对应的子类即可.
  • 当然你也可以扩展或者修改其子类的行为.
  • UIControl的state决定其外观和交互能力.其state被UIControlState决定.你可以改变state根据APP的需要.

Taget-Action机制浅析

代码示例

1
addTarget:action:forControlEvents:
  • 简化了代码(跟踪触摸事件的代码).
  • 必须指定The UIControlEvents type去触发the method.比如 UIButton ->UIControlEventTouchDown or UIControlEventTouchUpInside,UISlider->UIControlEventValueChanged
  • 当control的event发生时,control会马上唤醒关联的action methods,action methods被当前的UIApplication Object派发,UIApplication Object会按照响应者链,寻找到合适的对象去处理the message.

子类化说明

子类化已存在的controls 和 修改他们的 behavior 使用下面方法:

  • Override the sendAction:to:forEvent: method of an existing subclass to observe or modify the dispatching of action methods to the control’s associated targets. You might use this method to modify the dispatch behavior based on the specified object, selector, or event.
  • Override the beginTrackingWithTouch:withEvent:, continueTrackingWithTouch:withEvent:, endTrackingWithTouch:withEvent:, and cancelTrackingWithEvent: methods to track touch events occurring in the control. You can use the tracking information to perform additional actions. Always use these methods to track touch events instead of the methods defined by the UIResponder class.

属性

1
2
3
4
5
6
state
enabled
selected
highlighted
contentVerticalAlignment
contentHorzontalAlignment

Accessing the Control’s Targets and Actions

  • addTarget:action:forControlEvents:
    Associates a target object and action method with the control.
  • removeTarget:action:forControlEvents:
    Stops the delivery of events to the specified target object.
  • actionsForTarget:forControlEvent:
    Returns the actions performed on a target object when the specified event occurs.
  • allControlEvents
    Returns the events for which the control has associated actions.
    Beta
  • allTargets
    Returns all target objects associated with the control.

Triggering Actions(触发事件)

  • sendAction:to:forEvent:
    Calls the specified action method.
  • sendActionsForControlEvents:
    Calls the action methods associated with the specified events.

Tracking Touches and Redrawing Controls(跟踪触摸事件和重画Controls)

  • beginTrackingWithTouch:withEvent:
    Called when a touch event enters the control’s bounds.
  • continueTrackingWithTouch:withEvent:
    Called when a touch event associated with the control is updated.
  • endTrackingWithTouch:withEvent:
    Called when a touch event associated with the control ends.
  • cancelTrackingWithEvent:
    Tells the control to cancel tracking related to the given event.
  • tracking
    A Boolean value indicating whether the control is currently tracking touch events.
  • touchInside
    A Boolean value indicating whether a tracked touch event is currently inside the control’s bounds.

Constants(常量)

  • UIControlEvents
    Constants describing the types of events possible for controls.
  • UIControlState
    Constants describing the state of a control.
  • UIControlContentVerticalAlignment
    Constants for specifying the vertical alignment of content (text and images) in a control.
  • UIControlContentHorizontalAlignment
    The horizontal alignment of content (text and images) within a control.

参考:UIKit: UIControl,这一篇博客写的更浅显易懂

坚持原创技术分享,您的支持将鼓励我继续创作!