UIKit Gestures
Use gesture recognizers to simplify touch handling and create a consistent user experience.
使用手勢(shì)識(shí)別可以簡化觸摸操作,實(shí)現(xiàn)流暢的用戶體驗(yàn)
介紹
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.
對(duì)視圖來說,手勢(shì)識(shí)別是最簡單的處理觸摸、按壓事件的方式。
你可以在任意視圖上添加一個(gè)或多個(gè)手勢(shì).
對(duì)view接收到的事件的所有邏輯處理和說明,手勢(shì)識(shí)別都進(jìn)行了封裝,并會(huì)將它們匹配到已知模式中(??)
當(dāng)與之匹配的手勢(shì)事件處理者被檢測到,手勢(shì)識(shí)別器就會(huì)通知為它分配的目標(biāo)對(duì)象,這個(gè)對(duì)象可以是viewcontroller或是view自己,或app中的其他任何對(duì)象
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.
手勢(shì)識(shí)別使用 目標(biāo)-動(dòng)作設(shè)計(jì)模式來發(fā)送通知,如圖所示。當(dāng)UITapGestureRecognizer檢測到視圖上有單點(diǎn)觸摸時(shí),就會(huì)調(diào)用一個(gè)在當(dāng)前view所屬viewController上定義的動(dòng)作方法,這個(gè)方法內(nèi)部對(duì)該手勢(shì)做出響應(yīng)。

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.
手勢(shì)識(shí)別有兩種類型:分離型和連續(xù)型。一個(gè)分離型的手勢(shì)識(shí)別器會(huì)在手勢(shì)被識(shí)別到之后立即調(diào)用action方法。而連續(xù)識(shí)別手勢(shì),則會(huì)在滿足初始識(shí)別條件后連續(xù)多次調(diào)用action方法,只要手勢(shì)事件信息發(fā)生改變都會(huì)通知到你。比如,UIPanGestureRecognizer對(duì)象會(huì)在每次觸摸位置改變的時(shí)候調(diào)用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 包含了每種標(biāo)準(zhǔn)UIKit手勢(shì)識(shí)別器對(duì)象。也包含了繼承自UIGestureRecognizer的可以直接使用的自定義手勢(shì)識(shí)別對(duì)象
響應(yīng)手勢(shì)
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.
與手勢(shì)關(guān)聯(lián)的action方法提供了對(duì)手勢(shì)的響應(yīng)。對(duì)于分離型手勢(shì)來說,action方法跟按鈕的action方法差不多。一旦方法被調(diào)用,就會(huì)執(zhí)行適合該手勢(shì)的任務(wù)。對(duì)連續(xù)型手勢(shì),action方法可以響應(yīng)對(duì)手勢(shì)的識(shí)別,但它也可以在手勢(shì)被識(shí)別到之前跟蹤事件。事件跟蹤可以讓你創(chuàng)造更具有交互性的體驗(yàn)。比如,你可以使用UIPanGestureRecognizer對(duì)象重新定位app中的內(nèi)容。
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.
手勢(shì)識(shí)別的狀態(tài)屬性可以顯示出對(duì)象被識(shí)別到時(shí)的當(dāng)前狀態(tài)。對(duì)于連續(xù)型手勢(shì)來說,手勢(shì)識(shí)別器會(huì)將屬性值從began更新到changed,到ended,或者到cancelled。你定義的action方法用這些屬性值來決定接下來執(zhí)行哪些合適的行為。比如,你可以使用began和changed狀態(tài)來對(duì)內(nèi)容做一下短暫的改變,用ended狀態(tài)做一些最終的改變,用cancelled狀態(tài)回滾改變。在每次執(zhí)行action之前,都需要檢查手勢(shì)的狀態(tài)屬性值
總結(jié)
作用:簡化觸摸操作,優(yōu)化用戶體驗(yàn)。是對(duì)視圖接收到的事件的封裝
使用范圍:任意視圖都可以添加一個(gè)或多個(gè)手勢(shì)
分類:分離型,連續(xù)型
區(qū)別:連續(xù)型手續(xù)有各種狀態(tài),開發(fā)者可根據(jù)狀態(tài)不同實(shí)現(xiàn)不同的業(yè)務(wù)邏輯