Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
508 views
in Technique[技术] by (71.8m points)

ios - Swift: Long Press Gesture Recognizer - Detect taps and Long Press

I want to wire an action such that if the gesture is a tap, it does animates an object in a particular way but if the press duration was more than .5 secs it does something else.

Right now, I just have the animation hooked up. I don't know how I can differentiate between a long press and a tap? How do I access the press duration to achieve the above?

 @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)
        })
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Define two IBActions and set one Gesture Recognizer to each of them. This way you can perform two different actions for each gesture.

You can set each Gesture Recognizer to different IBActions in the interface builder.

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

@IBAction func longPressed(sender: UILongPressGestureRecognizer)
{
    println("longpressed")
    //Different code
}

Through code without interface builder

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...