经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Swift » 查看文章
Swift中添加双击手势识别器
来源:jb51  时间:2019/8/12 8:37:10  对本文有异议

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

代码:

  1. import Foundation
  2. import UIKit
  3. class MainBoardController: UIViewController{
  4. let tap = UITapGestureRecognizer()
  5. override func viewDidLoad() {
  6. super.viewDidLoad()
  7. // Do any additional setup after loading the view,typically from a nib.
  8. var swipe: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self,action: "GotoProfile")
  9. swipe.direction = UISwipeGestureRecognizerDirection.Right
  10. self.view.addGestureRecognizer(swipe)
  11. tap.addTarget(self,action: "GotoCamera")
  12. view.userInteractionEnabled = true
  13. view.addGestureRecognizer(tap)
  14. }
  15. override func didReceiveMemoryWarning() {
  16. super.didReceiveMemoryWarning()
  17. // Dispose of any resources that can be recreated.
  18. }
  19. func GotoProfile(){
  20. self.performSegueWithIdentifier("Profilesegue",sender: nil)
  21. }
  22. func GotoCamera(){
  23. self.performSegueWithIdentifier("Camerasegue",sender: nil)
  24. }
  25. }

解决方法

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

  1. override func viewDidLoad() {
  2. super.viewDidLoad()
  3. let tapGR = UITapGestureRecognizer(target: self,action: #selector(PostlistViewController.handleTap(_:)))
  4. tapGR.delegate = self
  5. tapGR.numberOfTapsRequired = 2
  6. view.addGestureRecognizer(tapGR)
  7. }
  8. extension MainBoardController: UIGestureRecognizerDelegate {
  9. func handleTap(_ gesture: UITapGestureRecognizer){
  10. print("doubletapped")
  11. }
  12. }

总结

以上是w3xue为你收集整理的如何在Swift中添加双击手势识别器全部内容,希望文章能够帮你解决如何在Swift中添加双击手势识别器所遇到的程序开发问题。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号