手势识别(二)使用手势识别器来简化事件处理

来源:互联网 发布:淘宝手机助手5.0.0 编辑:程序博客网 时间:2024/06/05 13:45

Use Gesture Recognizers to Simplify Event Handling

二、使用手势识别器来简化事件处理

The UIKit framework provides predefined gesture recognizers that detect common gestures. It’s best to use a predefined gesture recognizer when possible because their simplicity reduces the amount of code you have to write. In addition, using a standard gesture recognizer instead of writing your own ensures that your app behaves the way users expect.

UIKit 框架提供了一些已经预先定义好的手势识别来侦测各种常用手势。如果可能的话,最好是使用预定义的手势识别,因为它们缩减了你必须写的代码总量。另外,使用一个标准的手势识别而不是自己编写以确保你的应用程序如用户期望的那样工作。

If you want your app to recognize a unique gesture, such as a checkmark or a swirly motion, you can create your own custom gesture recognizer. To learn how to design and implement your own gesture recognizer, see “Creating a Custom Gesture Recognizer.”

如果你想你的应用程序识别一个独特的手势,比如一个勾或一个漩涡状运动,你可以创建你自己的自定义手势识别。 学习如何设计和实现你自己的手势识别,请看 “Creating a Custom Gesture Recognizer.”

Built-in Gesture Recognizers Recognize Common Gestures

1、内建手势识别来识别各种常用手势

When designing your app, consider what gestures you want to enable. Then, for each gesture, determine whether one of the predefined gesture recognizers listed in Table 1-1 is sufficient.

当你设计应用程序时,考虑你想要开启什么手势。  然后,对于每个手势,决定列表1-1中的预定义手势识别是否就足够。

Table 1-1  Gesture recognizer classes of the UIKit framework

Gesture

UIKit class

Tapping (any number of taps)

UITapGestureRecognizer

Pinching in and out (for zooming a view)

UIPinchGestureRecognizer

Panning or dragging

UIPanGestureRecognizer

Swiping (in any direction)

UISwipeGestureRecognizer

Rotating (fingers moving in opposite directions)

UIRotationGestureRecognizer

Long press (also known as “touch and hold”)

UILongPressGestureRecognizer

Your app should respond to gestures only in ways that users expect. For example, a pinch should zoom in and out whereas a tap should select something. For guidelines about how to properly use gestures, see “Apps Respond to Gestures, Not Clicks” in iOS Human Interface Guidelines.

你的应用程序应该只以用户期待的方式来响应。 比如,一个捏合动作应该缩放,而轻击动作应该选择一些东西。 关于正确使用手势的指南,请看iOS Human Interface Guidelines 中的 “Apps Respond to Gestures, Not Clicks”。

Gesture Recognizers Are Attached to a View

2、手势识别连接到视图

Every gesture recognizer is associated with one view. By contrast, a view can have multiple gesture recognizers, because a single view might respond to many different gestures. For a gesture recognizer to recognize touches that occur in a particular view, you must attach the gesture recognizer to that view. When a user touches that view, the gesture recognizer receives a message that a touch occurred before the view object does. As a result, the gesture recognizer can respond to touches on behalf of the view.

每个手势识别都跟一个视图相关联。 通常,视图可以有多个手势识别,因为单个视图可能响应多个不同的手势。 要想一个手势识别能够识别在一个特殊视图上发生的各种触摸,你必须把手势识别连接到那个视图。 当用户触摸了那个视图,手势识别先于视图对象接收一个触摸发生的信息。 结果,手势识别可以通过视图的行为来响应各种触摸。

Gestures Trigger Action Messages

3、 手势触发操作消息

When a gesture recognizer recognizes its specified gesture, it sends an action message to its target. To create a gesture recognizer, you initialize it with a target and an action.

当一个手势识别识别出其制定的手势后,它给它的目标发送一个操作消息。 要想创建一个手势识别,你需要初始化一个目标和一个操作。

Discrete and Continuous Gestures

1). 离散和连续的手势

Gestures are either discrete or continuous. A discrete gesture, such as a tap, occurs once. A continuous gesture, such as pinching, takes place over a period of time. For discrete gestures, a gesture recognizer sends its target a single action message. A gesture recognizer for continuous gestures keeps sending action messages to its target until the multitouch sequence ends, as shown in Figure 1-2.

手势有离散手势,也有连续手势。 一个离散手势,比如一个轻击(tap),只发生一次。一个连续手势,比如捏合(pinching),发生时持续一段时间。 对于离散手势,手势识别就给目标发送一个操作消息。对于连续手势,手势识别则保持发送操作消息给目标直到多点触摸序列结束,如图1-2.

Figure 1-2  Discrete and continuous gestures

图1-2 离散和连续手势


0 0
原创粉丝点击