UIControl控件影响着手势识别器和响应链的优先级你能信~~(手动笑哭~~)
UIControl是控件的基类,它是一种视觉元素,可以响应用户交互传递特定action或intention。
Controls implement elements such as buttons and sliders, which your app might use to facilitate navigation, gather user input, or manipulate content. Controls use the Target-Action mechanism to report user interactions to your app.
控件执行元件,例如按钮、滑块等,这些都是app中可能用到的,可以用来优化导航、获取用户输入、或控制内容等。控件使用目标-动作(Target-Action)方式将用户交互传递到app中
You do not create instances of this class directly. The UIControl class is a subclassing point that you extend to implement custom controls. You can also subclass existing control classes to extend or modify their behaviors. For example, you might override the methods of this class to track touch events yourself or to determine when the state of the control changes.
使用中不是直接创建该类的具体实例。UIControl类是一个子类扩展点,用来实现自定义控件。你也可以通过继承已有控件类来扩展或修改他们的特性。例如,你可以重写该类的一些方法来自己追踪touch事件,或自己决定控件状态变化的时机。
A control’s state determines its appearance and its ability to support user interactions. Controls can be in one of several states, which are defined by the
UIControState
type. You can change the state of a control programmatically based on your app’s needs. For example, you might disable a control to prevent the user from interacting with it. User interactions can also change the state of a control.
控件的状态决定了它的外观和支持用户交互的能力。控件可以有一个或多个状态,这些状态由UIControlState
来定义。你可以根据需要用程序来改变控件的状态。例如,你可以通过改变状态来阻止用户交互。用户交互同样也可以改变控件的状态。
目标-动作机制
Controls use the target-action mechanism to report interesting events happening to your code. The target-action mechanism simplifies the code that you write to use controls in your app. Instead of writing code to track touch events, you write action methods to respond to control-specific events. For example, you might write an action method that responds to changes in the value of a slider. The control handles all the work of tracking incoming touch events and determining when to call your methods.
控件使用目标-动作机制来传达发生在代码中的有趣事件。目标-动作机制简化了在app中使用控件的代码。不同于写代码来追踪触摸事件,你可以定义action方法来响应控制类事件。例如,你可以定义一个action方法来响应一个滑块值的改变。着所有追踪输入触摸事件的事情都由控件来控制,同时也控制着什么时候滴啊用你定义的方法。
When adding an action method to a control, you specify both the action method and an object that defines that method to the [
addTarget(_:action:for:)
] method. (You can also configure the target and action of a control in Interface Builder.) The target object can be any object, but it is typically the view controller whose root view contains the control. If you specifynil
for the target object, the control searches the responder chain for an object that defines the specified action method.
当为一个控件添加action方法时,要通过addTarget(_:action:for:)
方法同时指定action方法和对象(你也可以通过interface builder来定义目标和action)。目标对象可以是任意对象,但一般是该控件所在view所属的viewController。如果你定义nil为目标对象,控件会根据响应链来寻找定义了特定action方法的对象。
Action methods are called when the user interacts with the control in specific ways. The [
UIControlEvents
] type defines the types of user interactions that a control can report and those interactions mostly correlate to specific touch events within the control. When configuring a control, you must specify which events trigger the calling of your method. For a button control, you might use the [touchDown
] or [touchUpInside
] event to trigger calls to your action method. For a slider, you might care only about changes to the slider’s value changes and so you might choose to attach your action method to [valueChanged
] events.
Action方法在用户交互的时候以特定的方式被调用。UIControlEvents
定义了控件可以传达的用户交互事件类型,这些交互大部分都与控件的特定触摸事件相关。当配置控件时,你必须明确特定的事件触发器来调用你所定义的方法。对按钮控件来说,你可能会用到[touchDown
] or [touchUpInside
]事件来触发调用你定义的方法。对滑块来说,你可能只能用到滑块值变化这一种事件类型,即valueChanged
When a control-specific event occurs, the control calls any associated action methods right away. Action methods are dispatched through the current [
UIApplication
] object, which finds an appropriate object to handle the message, following the responder chain if needed.
当一个控制型事件发生时,控件会立即调用所有相关的action方法。action方法会被当前UIApplication对象分发,去寻找合适的对象来处理这一消息,如有必要会沿着响应链进行寻找。
(一个控件可以添加多个action方法,用户交互时会依次触发)
可访问性
Controls are accessible by default. To be useful, an accessible user interface element must provide accurate and helpful information about its screen position, name, behavior, value, and type. This is the information VoiceOver speaks to users. Visually impaired users can rely on VoiceOver to help them use their devices.
默认情况下,控件是可访问的。为了有用,一个可访问的用户界面元素必须提供精确和有用的信息,包括位置、名称、行为、值和类型等。这对用户来说相当于是画外音信息。就如同一个视障用户可以依靠画外音来帮助他们使用设备。
控件支持以下可访问属性:
- Label. A short, localized word or phrase that succinctly describes the control or view, but does not identify the element’s type. Examples are “Add” or “Play.”
- Traits. A combination of one or more individual traits, each of which describes a single aspect of an element’s state, behavior, or usage. For example, an element that behaves like a keyboard key and that is currently selected can be characterized by the combination of the Keyboard Key and Selected traits.
- Hint. A brief, localized phrase that describes the results of an action on an element. Examples are “Adds a title” or “Opens the shopping list.”
- Frame. The frame of the element in screen coordinates, which is given by the CGRect structure that specifies an element’s screen location and size.
- Value. The current value of an element, when the value is not represented by the label. For example, the label for a slider might be “Speed,” but its current value might be “50%.”
The UIControl class provides default content for the value and frame attributes. Many controls enable additional specific traits automatically as well. You can configure other accessibility attributes programmatically or using the Identity inspector in Interface Builder.
控件类提供会为value提供默认的内容、frame等属性。许多控件可以添加特定属性。你可以编程来配置其他可访问属性,也可以使用interface builder中的检查器
子类使用注意点
Subclassing UIControl gives you access to the built-in target-action mechanism and simplified event-handling support. You can subclass existing controls and modify its behavior in one of two ways:
创建UIControl子类可以提供内置的目标-动作机制并且能简化事件处理机制。创建UIControl子类后,可以使用以下方式修改其行为:
- Override the
sendAction(_:to:for:)
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
beginTracking(_:with:)
,continueTracking(_:with:)
,endTracking(_:with:)
, andcancelTracking(with:)
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 theUIResponder
class.
- 重写现有子类的
sendAction(_:to:for:)
方法,可以观察或修改调度action方法相关的控件的目标。你可以用这个方法来修改基于特定对象、选择器、事件的分发行为。 - 重写
beginTracking(_:with:)
,continueTracking(_:with:)
,endTracking(_:with:)
, 和cancelTracking(with:)
方法来追踪控件上的触摸事件。你可以使用这些追踪到的信息来执行其他操作。可以用这些方法代替UIResponder类中定义的方法来追踪触摸事件。
If you subclass UIControl directly, your subclass is responsible for setting up and managing your control’s visual appearance. Use the methods for tracking events to update your control’s state and to send an action when the control’s value changes.
如果你直接创建UIControl的子类,该子类需要负责设置和管理控件的可视外观。使用这些方法追踪事件来更新控件状态,并且在控件值改变的时候发送action