Swift中添加雙擊手勢識別器

在這次IOS應用開發教程中,我們打算實現手勢識別。正如你所知道的,IOS支持大量的手勢操作,它們能提供了很好的應用控制和出色用戶體驗。

已經完成了單擊識別器,但無法弄清楚如何將該單擊識別器改爲雙擊.

代碼:

import Foundation
import UIKit
 
class MainBoardController: UIViewController{
 
  let tap = UITapGestureRecognizer()
 
  override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view,typically from a nib.
    var swipe: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self,action: "GotoProfile")
    swipe.direction = UISwipeGestureRecognizerDirection.Right
          self.view.addGestureRecognizer(swipe)
 
    tap.addTarget(self,action: "GotoCamera")
    view.userInteractionEnabled = true
    view.addGestureRecognizer(tap)
  }
 
  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
  }
 
  func GotoProfile(){
    self.performSegueWithIdentifier("Profilesegue",sender: nil)
  }
 
  func GotoCamera(){
    self.performSegueWithIdentifier("Camerasegue",sender: nil)
  }
}

解決方法

最終用擴展解決了這個問題:

override func viewDidLoad() {
  super.viewDidLoad()
 
  let tapGR = UITapGestureRecognizer(target: self,action: #selector(PostlistViewController.handleTap(_:)))
  tapGR.delegate = self
  tapGR.numberOfTapsRequired = 2
  view.addGestureRecognizer(tapGR)
}
extension MainBoardController: UIGestureRecognizerDelegate {
  func handleTap(_ gesture: UITapGestureRecognizer){
    print("doubletapped")
  }
}

總結

以上是神馬文庫爲你收集整理的如何在Swift中添加雙擊手勢識別器全部內容,希望文章能夠幫你解決如何在Swift中添加雙擊手勢識別器所遇到的程序開發問題。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章