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

问题描述:

我想布线的动作,如果手势是龙头,它确实以特定的方式动画对象,但如果按下持续时间超过0.5秒它做了别的事情。Swift:长按手势识别器 - 检测水龙头和长按

现在,我只是将动画连接起来。我不知道如何区分长按和水龙头? 我如何获得印刷持续时间以达到上述目的?

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

定义两个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") 
} 
+4

这似乎不能正常工作,因为如果我长按,水龙头和长按火... – 2015-01-21 14:57:02

+0

您使用的是接口构建器还是代码? – rakeshbs 2015-01-21 15:50:30

+0

Interface Builder – 2015-01-21 16:04:28

通过代码,而界面构建

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

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

+4

检查识别器状态'if(gestureRecognizer.state == UIGestureRecognizerStateBegan)' – Arvis 2017-01-05 09:56:26

+0

@Arvis在哪里可以放置这个'if'语句来防止多次执行长按手势功能? – PlateReverb 2017-07-19 22:13:01

+1

@PlateReverb在选择器动作函数中 – Arvis 2017-07-20 09:03:17