服务器之家

服务器之家 > 正文

Swift中添加双击手势识别器

时间:2021-01-13 16:53     来源/作者:Swift教程网

已经完成了单击识别器,但无法弄清楚如何将该单击识别器改为双击.

代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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)
  }
}

解决方法

最终用扩展解决了这个问题:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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中添加双击手势识别器所遇到的程序开发问题。

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部