Swift:长按手势识别器-检测点击和长按

nc1teljy  于 2023-10-15  发布在  Swift
关注(0)|答案(5)|浏览(155)

我想连接一个动作,如果手势是一个点击,它确实以特定的方式动画对象,但如果按下持续时间超过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)
        })
vtwuwzda

vtwuwzda1#

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

您可以在接口构建器中将每个Gesture Recognizer设置为不同的IBActions。

@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")
}
  • Swift 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")
}
lf3rwulv

lf3rwulv2#

对于swift 2

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
        }
    }
yptwkmov

yptwkmov3#

通过代码,无需接口构建器

// 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 !!
        }

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

mzsu5hc0

mzsu5hc04#

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")
}
sigwle7e

sigwle7e5#

iOS轻触手势和长按手势

在本例中,onLongPress() body不会在每次更改或结束运动时执行

let tap = UITapGestureRecognizer(target: self, action: #selector(self.onClick))
self.myView.addGestureRecognizer(tap)

let longPress = UILongPressGestureRecognizer(target: self, action: #selector(self.onLongPress))
longPress.minimumPressDuration = 0.5
self.myView.addGestureRecognizer(longPress)

@objc func onClick() {
    //logic
}

@objc func onLongPress(sender: UILongPressGestureRecognizer) {
    guard sender.state == .began else { return }
    //logic
}

相关问题