Swift中添加双击手势识别器

时间:2021-05-20

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

代码:

import Foundationimport 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中添加双击手势识别器所遇到的程序开发问题。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章