iOS文档--六种手势识别器的文档集合Handling Six Kind Of Gestures

Article

Handling Tap Gestures(link)                    --点击手势

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

         --使用屏幕上的简短点击来实现手势与视图内容的按钮式交互。

Overview            --  概览

Tap gestures detect one or more fingers touching the screen briefly. The fingers involved in these gestures must not move significantly from the initial touch points, and you can configure the number of times the fingers must touch the screen. For example, you might configure tap gesture recognizers to detect single taps, double taps, or triple taps.

       --轻触手势检测一个或多个手指短暂触摸屏幕。这些手势中涉及的手指不能从初始触摸点明显移动,您可以配置手指必须触摸屏幕的次数。例如,您可以将tap手势识别器配置为检测单拍、双拍或三拍。

You can attach a gesture recognizer in one of these ways:

         --关联手势的两种方式

  • Programmatically. Call the addGestureRecognizer(_:) method of your view.
    --调用view的addGestureRecognizer(_:)方法

  • In Interface Builder. Drag the appropriate object from the library and drop it onto your view.
    --IB上拖动手势组件

A diagram showing a single-finger tap gesture

 

A UITapGestureRecognizer object provides event handling capabilities similar to those of a button—it detects a tap in its view and reports that tap to your action method. Tap gestures are discrete, so your action method is called only when the tap gesture is recognized successfully. You can configure a tap gesture recognizer to require any number of taps—for example, single taps or double taps—before your action method is called.

          --UITapGestureRecognizer object负责检测手势并且报告给你定义的动作方法。在点击手势被UITapGestureRecognizer object成功检测到的时候,才会调用你定义的动作方法。所以你可以在动作方法被调用之前,设置被唤起时手势所需被点击的次数。

Listing 1 shows an action method that responds to a successful tap in a view by animating that view to a new location. Always check the gesture recognizer’s state property before taking any actions, even for a discrete gesture recognizer.

        --下面演示了动作方法实现,在动作方法中,记得在采取任何动作之前都要先检查recognizer’s state property

Listing 1

Handling a tap gesture

@IBAction func tapPiece(_ gestureRecognizer : UITapGestureRecognizer ) {
   guard gestureRecognizer.view != nil else { return }
        
   if gestureRecognizer.state == .ended {      // Move the view down and to the right when tapped.
      let animator = UIViewPropertyAnimator(duration: 0.2, curve: .easeInOut, animations: {
         gestureRecognizer.view!.center.x += 100
         gestureRecognizer.view!.center.y += 100
      })
      animator.startAnimation()
   }}

 

If the code for your tap gesture recognizer is not called, check to see if the following conditions are true, and make corrections as needed:

        --如果你的手势识别器没有被调用,看一下是否有以下的误操作:

 

 

Article

Handling Long-Press Gestures(Link)               --长按手势

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

         --检测屏幕上的长时间点击,并用它们来显示与上下文相关的内容。
 

Overview         --概览

Long-press (also known as press-and-hold) gestures detect one or more fingers (or a stylus) touching the screen for an extended period of time. You configure the minimum duration required to recognize the press and the number of times the fingers must be touching the screen. (The gesture recognizer is triggered only by the duration of the touches and not by the force associated with them.) You might use a long-press gesture to initiate an action on the object being pressed. For example, you might use it to display a context-sensitive menu.

        --长按(也称为按住)手势探测一个或多个手指(或触笔)在屏幕上长时间的触摸。您可以配置“识别压力时”所需的最短持续时间以及手指触摸屏幕的次数。(手势识别器仅由触摸的持续时间触发,而不是由与之关联的力触发。)您可以使用“长按手势”来启动“被按下的对象”的操作。例如,您可以使用它来显示上下文相关的菜单。

You can attach a gesture recognizer in one of these ways:
    --可以通过以下方式之一来关联手势识别器:

  • Programmatically. Call the addGestureRecognizer(_:) method of your view.
    --调用view的addGestureRecognizer(_:)方法

  • In Interface Builder. Drag the appropriate object from the library and drop it onto your view.
    --IB上拖动手势组件

A diagram showing a single-finger long-press gesture

Long-press gestures are continuous gestures, meaning that your action method may be called multiple times as the state changes. After the user's fingers have touched the screen for the minimum amount of time time, a long-press gesture recognizer enters the UIGestureRecognizer.State.began state. The gesture recognizer moves to the UIGestureRecognizer.State.changed state if the fingers move or if any other changes occur to the touches. The gesture recognizer remains in the UIGestureRecognizer.State.changed state as long as the fingers remain down, even if those fingers move outside of the initial view. When the user’s fingers lift from the screen, the gesture recognizer enters the UIGestureRecognizer.State.ended state.

          --长按手势是连续的手势,这意味着随着状态的变化,您的动作方法可能会被多次调用。在用户的手指触摸屏幕达到最短时间之后,长按手势识别器就进入UIGestureRecognizer.State.began 状态。如果手指移动或触摸发生任何其他的变化,手势识别器将转移到UIGestureRecognizer.State.changed 状态。只要手指还保持在向下按压,手势识别器就保持在UIGestureRecognizer.State.changed 状态,即使这些手指移动到了初始视图之外。当用户的手指从屏幕上抬起时,手势识别器进入 UIGestureRecognizer.State.ended 状态。

Listing 1 shows an action method that displays a context menu on top of the view. It displays the context menu at the beginning of the gesture, while the user’s finger is still on the screen. The view controller that implements this method also sets itself as the first responder so that it can respond to menu actions selected by the user.

     --清单1显示了一个action方法,该方法在视图顶部显示一个上下文菜单。它在手势开始时显示上下文菜单,而用户的手指仍在屏幕上。实现此方法的视图控制器还将自身设置为第一响应者,以便它可以响应用户选择的菜单操作。

Listing 1

Handling a long-press gesture

@IBAction func showResetMenu(_ gestureRecognizer: UILongPressGestureRecognizer) {
   if gestureRecognizer.state == .began {
      self.becomeFirstResponder()
      self.viewForReset = gestureRecognizer.view

      // Configure the menu item to display
      let menuItemTitle = NSLocalizedString("Reset", comment: "Reset menu item title")
      let action = #selector(ViewController.resetPiece(controller:))
      let resetMenuItem = UIMenuItem(title: menuItemTitle, action: action)

      // Configure the shared menu controller
      let menuController = UIMenuController.shared
      menuController.menuItems = [resetMenuItem]

      // Set the location of the menu in the view.
      let location = gestureRecognizer.location(in: gestureRecognizer.view)
      let menuLocation = CGRect(x: location.x, y: location.y, width: 0, height: 0)
      menuController.setTargetRect(menuLocation, in: gestureRecognizer.view!)

      // Show the menu.
      menuController.setMenuVisible(true, animated: true)
   }
}

If the code for your long-press gesture recognizer is not called, check to see if the following conditions are true, and make corrections as needed:
      --如果未调用长按手势识别器的代码,请检查以下情况是否正确,并根据需要进行更正:

 

 

这六种,其实也是类似的用法,直接看类UIGestureRecognizer

 

 

 

 

 

 

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值