【问题标题】:Swift: Long Press Gesture Recognizer - Detect taps and Long PressSwift:长按手势识别器 - 检测轻按和长按
【发布时间】:2015-03-19 10:23:46
【问题描述】:

我想连接一个动作,如果手势是点击,它会以特定方式为对象设置动画,但如果按下持续时间超过 0.5 秒,它会执行其他操作。

现在,我刚刚连接了动画。我不知道如何区分长按和点击? 如何访问新闻持续时间以实现上述目标?

 @IBAction func tapOrHold(sender: AnyObject) {
        UIView.animateKeyframesWithDuration(duration, delay: delay, options: options, animations: {

            UIView.addKeyframeWithRelativeStartTime(0, relativeDuration: 0, animations: {

                self.polyRotate.transform = CGAffineTransformMakeRotation(1/3 * CGFloat(M_PI * 2))
            })
            UIView.addKeyframeWithRelativeStartTime(0, relativeDuration: 0, animations: {
                self.polyRotate.transform = CGAffineTransformMakeRotation(2/3 * CGFloat(M_PI * 2))
            })
            UIView.addKeyframeWithRelativeStartTime(0, relativeDuration: 0, animations: {
                self.polyRotate.transform = CGAffineTransformMakeRotation(3/3 * CGFloat(M_PI * 2))
            })

            }, completion: { (Bool) in
                let vc : AnyObject! = self.storyboard?.instantiateViewControllerWithIdentifier("NextView")
                self.showViewController(vc as UIViewController, sender: vc)
        })

【问题讨论】:

    标签: ios swift long-press


    【解决方案1】:

    定义两个IBActions 并为每个设置一个Gesture Recognizer。这样,您可以为每个手势执行两种不同的操作。

    您可以在界面构建器中将每个 Gesture Recognizer 设置为不同的 IBAction。

    @IBAction func tapped(sender: UITapGestureRecognizer)
    {
        println("tapped")
        //Your animation code.
    }
    
    @IBAction func longPressed(sender: UILongPressGestureRecognizer)
    {
        println("longpressed")
        //Different code
    }
    

    通过没有界面生成器的代码

    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: "tapped:")
        self.view.addGestureRecognizer(tapGestureRecognizer)
        
    let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: "longPressed:")
        self.view.addGestureRecognizer(longPressRecognizer)
    
    func tapped(sender: UITapGestureRecognizer)
    {
        println("tapped")
    }
    
    func longPressed(sender: UILongPressGestureRecognizer)
    {
        println("longpressed")
    }
    

    斯威夫特 5

    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(tapped))
    self.view.addGestureRecognizer(tapGestureRecognizer)
        
    let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(longPressed))
    self.view.addGestureRecognizer(longPressRecognizer)
        
    @objc func tapped(sender: UITapGestureRecognizer){
        print("tapped")
    }
    
    @objc func longPressed(sender: UILongPressGestureRecognizer) {
        print("longpressed")
    }
    

    【讨论】:

    • 这似乎无法正常工作,就像我长按一样,水龙头和长按都开火......
    • 您是否像上面的代码那样为每个手势识别器绑定了不同的 IBAction。它对我有用
    • 也感谢您添加代码版本,只是寻找界面构建器的答案并开始感到沮丧
    • 我有一个表格,在 cellForRowAtIndexPath 方法中我已经编写了你建议的代码。如何将 rowId 发送到操作函数? @rakeshbs
    • @blue_zinc 这解决了问题:tapGestureRecognizer.require(toFail: longPressRecognizer)
    【解决方案2】:

    对于 swift2

    let lpgr = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress))
    lpgr.minimumPressDuration = 0.5
    lpgr.delaysTouchesBegan = true
    lpgr.delegate = self
    self.featuredCouponColView.addGestureRecognizer(lpgr)
    

    动作

    //MARK: - UILongPressGestureRecognizer Action -
        func handleLongPress(gestureReconizer: UILongPressGestureRecognizer) {
            if gestureReconizer.state != UIGestureRecognizerState.Ended {
                //When lognpress is start or running
            }
            else {
                //When lognpress is finish
            }
        }
    

    适用于 Swift 4.2/ Swift 5

    let lpgr = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress))
    lpgr.minimumPressDuration = 0.5
    lpgr.delaysTouchesBegan = true
    lpgr.delegate = self
    self.colVw.addGestureRecognizer(lpgr)
    
    //MARK: - UILongPressGestureRecognizer Action -
        @objc func handleLongPress(gestureReconizer: UILongPressGestureRecognizer) {
            if gestureReconizer.state != UIGestureRecognizer.State.ended {
                //When lognpress is start or running
            }
            else {
                //When lognpress is finish
            }
        }
    

    【讨论】:

      【解决方案3】:

      通过没有界面生成器的代码

      // Global variables declaration
      var longPressed = false
      var selectedRow = 0
      
      
      
      override func viewDidLoad() {
              super.viewDidLoad()  
              let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(ContactListTableViewController.handleLongPress(_:)))
              longPressGesture.minimumPressDuration = 1.0 // 1 second press
              longPressGesture.allowableMovement = 15 // 15 points
              longPressGesture.delegate = self
              self.tableView.addGestureRecognizer(longPressGesture)
          }
      
      // Long tap work goes here !!
      if (longPressed == true) {
      
             if(tableView.cellForRowAtIndexPath(indexPath)?.accessoryType == .Checkmark){
                      tableView.cellForRowAtIndexPath(indexPath)?.accessoryType = .None
                      self.selectedRow -= 1
      
                      if(self.selectedRow == 0){
                          self.longPressed = false
                      }
      
                  } else {
                      self.selectedRow += 1
                      tableView.cellForRowAtIndexPath(indexPath)?.accessoryType = .Checkmark
                  }
      
              } else if(self.selectedRow == 0) {
                // Single tape work goes here !!
              }
      

      但唯一的问题是长按手势运行了两次。如果您找到任何解决方案,请在下方发表评论!

      【讨论】:

      • 检查识别器状态if (gestureRecognizer.state == UIGestureRecognizerStateBegan)
      • @Arvis 你会把这个if 语句放在哪里,以防止多次执行长按手势功能?
      • @PlateReverb 在选择器动作函数中
      【解决方案4】:

      Swift 5 使用界面构建器

      对于普通的点击,您可以简单地从您的按钮创建一个touch up inside 操作。

      对于长按,为您的按钮创建一个插座,创建轻击手势识别器并将其设置为按钮,然后创建选择器方法来执行长按任务。

      @IBOutlet var myButton: UIButton!
      
      override func viewDidLoad() {
          super.viewDidLoad()
          
          let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(addToListButtonLongPress(_:)))
          longPressRecognizer.numberOfTouchesRequired = 1
          longPressRecognizer.allowableMovement = 10
          longPressRecognizer.minimumPressDuration = 0.5
          myButton.addGestureRecognizer(longPressRecognizer)
      }
      
      // Connected to myButton in interface builder.
      @IBAction func myButtonTapped(_ sender: UIButton) {
          print("button tapped")
      }
      
      @objc func myButtonLongPressed(_ sender: UILongPressGestureRecognizer) {
          print("button long pressed")
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多