2015-01-21 115 views
25

我想佈線的動作,如果手勢是龍頭,它確實以特定的方式動畫對象,但如果按下持續時間超過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) 
     }) 

回答

58

定義兩個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

2

通過代碼,而界面構建

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