iOS 文档--UIKit的手势识别器处理--Handling UIKit Gestures

由于使用textfield引起的阅读,应该结合前面的笔记一起看会比较好一点

Article

Handling UIKit Gestures

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

 

Overview            -- 概览

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.

       --手势识别器封装了“处理和解释”该视图传入的事件的所有逻辑,还有将事件与“已知模式”匹配所需的所有逻辑。当检测到匹配时,手势识别器会通知其指派的目标对象,该对象可以是视图控制器、视图本身或应用程序中的任何其他对象

Gesture recognizers use the target-action design pattern to send notifications. 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对象检测到视图中有一个手指点击时,它将自动调用视图的vc的操作方法,所以你可以复写该方法来进行响应。

       

Figure 1

Gesture recognizer notifying its target

A diagram demonstrating how a gesture recognizer links user interactions with your view controller action methods.

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.

           --一个离散的手势识别器在识别出你的手势后,只调用你的动作方法一次。连续手势识别器在满足初始识别条件后,多次调用动作方法,并在手势事件中的信息发生更改时通知您。

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.

 

 

Configuring a gesture recognizer            --  配置手势识别器

To configure a gesture recognizer:

  1. In your storyboard, drag the gesture recognizer onto your view.

  2. Implement an action method to be called when the gesture is recognized; see Listing 1.

  3. Connect your action method to the gesture recognizer.

You can create this connection in Interface Builder by right-clicking the gesture recognizer and connecting its Sent Action selector to the appropriate object in your interface. You can also configure the action method programmatically using the addTarget(_:action:) method of the gesture recognizer.

Listing 1 shows the generic format for the action method of a gesture recognizer. If you prefer, you can change the parameter type to match a specific gesture recognizer subclass.

       --自定义一个动作方法,通过recognizer的addTarget(_:action:)方法进行绑定。

Listing 1

Gesture recognizer action methods

@IBAction func myActionMethod(_ sender: UIGestureRecognizer)

 

Responding to Gestures         --响应手势

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.

             --离散手势识别器,一次识别,动作方法一次调用。持续手势识别器,持续识别,持续调用动作方法。

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 UIGestureRecognizer.State.began to UIGestureRecognizer.State.changed to UIGestureRecognizer.State.ended, or to UIGestureRecognizer.State.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.

              --recognizer的state property实时更新,三个值代表三种状态,你可以从这些值中获取当时手势的状态,从而作出动作。

For examples of how to handle specific types of gestures, see the following information:

--各种手势的教程

 

For more information about gesture recognizer states and how they affect your code, see Implementing a Custom Gesture Recognizer.

Topics

Gestures

 

Handling Tap Gestures                   --点击的手势

Use brief taps on the screen to implement button-like interactions with your content.

 

Handling Long-Press Gestures               --长按的手势

Detect extended duration taps on the screen, and use them to reveal contextually relevant content.

 

Handling Pan Gestures             --平移的手势

Trace the movement of fingers around the screen, and apply that movement to your content.

 

Handling Swipe Gestures          --滑动的手势

Detect a horizontal or vertical swipe motion on the screen, and use it to trigger navigation through your content.

 

Handling Pinch Gestures          --捏合的手势

Track the distance between two fingers and use that information to scale or zoom your content.

 

Handling Rotation Gestures          --旋转的手势

Measure the relative rotation of two fingers on the screen, and use that motion to rotate your content.

See Also

Standard Gestures

 

Coordinating Multiple Gesture Recognizers                                                    --坐标多个手势

Discover how to use multiple gesture recognizers on the same view.

 

class UIHoverGestureRecognizer

A gesture recognizer that looks for pointer movement over a view.

class UILongPressGestureRecognizer

A concrete subclass of UIGestureRecognizer that looks for long-press gestures.

class UIPanGestureRecognizer

A concrete subclass of UIGestureRecognizer that looks for panning (dragging) gestures.

class UIPinchGestureRecognizer

A concrete subclass of UIGestureRecognizer that looks for pinching gestures involving two touches.

class UIRotationGestureRecognizer

A concrete subclass of UIGestureRecognizer that looks for rotation gestures involving two touches.

class UIScreenEdgePanGestureRecognizer

A gesture recognizer that looks for panning (dragging) gestures that start near an edge of the screen.

class UISwipeGestureRecognizer

A concrete subclass of UIGestureRecognizer that looks for swiping gestures in one or more directions.

class UITapGestureRecognizer

A concrete subclass of UIGestureRecognizer that looks for single or multiple taps.

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值