Gestures

UIKit Gestures

Use gesture recognizers to simplify touch handling and create a consistent user experience.

使用手势识别可以简化触摸操作,实现流畅的用户体验

介绍

Gesture recognizers are the simplest way to handle touch or press events in your views.
You can attach one or more gesture recognizers to any view.
Gesture recognizers encapsulate all of the logic needed to process and interpret incoming events for that view and match them to a known pattern.
When a match is detected, the gesture recognizer notifies its assigned target
object, which can be a view controller, the view itself, or any other object in your app.

对视图来说,手势识别是最简单的处理触摸、按压事件的方式。
你可以在任意视图上添加一个或多个手势.
对view接收到的事件的所有逻辑处理和说明,手势识别都进行了封装,并会将它们匹配到已知模式中(??)
当与之匹配的手势事件处理者被检测到,手势识别器就会通知为它分配的目标对象,这个对象可以是viewcontroller或是view自己,或app中的其他任何对象

Gesture recognizers use the target-action design pattern to send notifications, an example of which is shown in [Figure 1] .
When the UITapGestureRecognizer object detects a single-finger tap in the view, it calls an action method of the view’s view controller, which you use to provide a response.

手势识别使用 目标-动作设计模式来发送通知,如图所示。当UITapGestureRecognizer检测到视图上有单点触摸时,就会调用一个在当前view所属viewController上定义的动作方法,这个方法内部对该手势做出响应。

Figure1.png

Gesture recognizers come in two types: discrete and continuous.
A discrete gesture recognizer calls your action method exactly once after the gesture is recognized. After its initial recognition criteria are met, a continuous gesture recognizer performs calls your action method many times, notifying you whenever the information in the gesture's event changes. For example, a UIPanGestureRecognizer object calls your action method each time the touch position changes.

手势识别有两种类型:分离型和连续型。一个分离型的手势识别器会在手势被识别到之后立即调用action方法。而连续识别手势,则会在满足初始识别条件后连续多次调用action方法,只要手势事件信息发生改变都会通知到你。比如,UIPanGestureRecognizer对象会在每次触摸位置改变的时候调用action方法。

Interface Builder includes objects for each of the standard UIKit gesture recognizers. It also includes a custom gesture recognizer object that you can use to represent your custom UIGestureRecognizer subclasses.

Interface Builder 包含了每种标准UIKit手势识别器对象。也包含了继承自UIGestureRecognizer的可以直接使用的自定义手势识别对象

响应手势

The action method associated with a gesture recognizer provides your app’s response to that gesture. For discrete gestures, your action method is similar to the action method for a button. Once the action method is called, you perform whatever task is appropriate for that gesture. For continuous gestures, your action method can respond to the recognition of the gesture, but it can also track events before the gesture is recognized. Tracking events lets you create a more interactive experience. For example, you might use the updates from a UIPanGestureRecognizer object to reposition content in your app.

与手势关联的action方法提供了对手势的响应。对于分离型手势来说,action方法跟按钮的action方法差不多。一旦方法被调用,就会执行适合该手势的任务。对连续型手势,action方法可以响应对手势的识别,但它也可以在手势被识别到之前跟踪事件。事件跟踪可以让你创造更具有交互性的体验。比如,你可以使用UIPanGestureRecognizer对象重新定位app中的内容。

The state property of a gesture recognizer communicates the object’s current state of recognition. For continuous gestures, the gesture recognizer updates the value of this property from [began] to [changed] to [ended] , or to [cancelled] . Your action methods use this property to determine an appropriate course of action. For example, you might use the began and changed states to make temporary changes to your content, use the ended state to make those changes permanent, and use the cancelled state to discard the changes. Always check the value of the [state] property of a gesture recognizer before taking actions.

手势识别的状态属性可以显示出对象被识别到时的当前状态。对于连续型手势来说,手势识别器会将属性值从began更新到changed,到ended,或者到cancelled。你定义的action方法用这些属性值来决定接下来执行哪些合适的行为。比如,你可以使用began和changed状态来对内容做一下短暂的改变,用ended状态做一些最终的改变,用cancelled状态回滚改变。在每次执行action之前,都需要检查手势的状态属性值

总结

作用:简化触摸操作,优化用户体验。是对视图接收到的事件的封装
使用范围:任意视图都可以添加一个或多个手势
分类:分离型,连续型
区别:连续型手续有各种状态,开发者可根据状态不同实现不同的业务逻辑

你可能感兴趣的:(Gestures)