Swift:Long Press手势识别器 - 检测水龙头和长按

blu*_*inc 35 ios long-press swift

我想连接一个动作,如果手势是一个轻击,它会以特定方式动画一个对象,但如果按下持续时间超过.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)
        })
Run Code Online (Sandbox Code Playgroud)

rak*_*hbs 77

定义两个IBActionsGesture Recognizer为每个设置一个.这样,您可以为每个手势执行两个不同的操作.

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

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

@IBAction func longPressed(sender: UILongPressGestureRecognizer)
{
    println("longpressed")
    //Different code
}
Run Code Online (Sandbox Code Playgroud)

通过代码没有界面构建器

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")
}
Run Code Online (Sandbox Code Playgroud)

  • 这似乎没有正常工作,好像我长按,水龙头和长按火...... (4认同)
  • 感谢您添加代码版本,只是找到界面构建器的答案并开始感到沮丧 (3认同)
  • @blue_zinc这可以解决问题:tapGestureRecognizer.require(toFail:longPressRecognizer) (2认同)

Har*_*kar 15

迅捷2

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

行动

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

对于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
        }
    }
Run Code Online (Sandbox Code Playgroud)


Asi*_*han 5

通过代码没有界面构建器

// 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 !!
        }
Run Code Online (Sandbox Code Playgroud)

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

  • 检查识别器状态`if(gestureRecognizer.state == UIGestureRecognizerStateBegan)` (6认同)
  • 选择器动作功能中的@PlateReverb (2认同)