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之前,都需要检查手势的状态属性值

总结

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,546评论 6 507
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,224评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,911评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,737评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,753评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,598评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,338评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,249评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,696评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,888评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,013评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,731评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,348评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,929评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,048评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,203评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,960评论 2 355

推荐阅读更多精彩内容