2015-08-28 7 views
6

में डबल टैप जेस्चर रिकॉग्नाइज़र जोड़ने के लिए मैंने पहले से ही एक टैप पहचानकर्ता पूरा कर लिया है लेकिन यह पता नहीं लगा सकता कि उस एकल टैप पहचानकर्ता को इसके बजाय दो बार टैप कैसे बनाया जाए। मैं कुछ मार्गदर्शन का उपयोग कर सकता था।स्विफ्ट

कोड:

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) 
    } 
} 
+0

' tap.numberOfTapsRequired = 2' – vacawama

+1

का प्रयोग करें, 'UISwipeGestureRecognizer' नहीं। जब तक आप दो अंगुली स्वाइप का मतलब न लें और डबल-टैप न करें। – rmaddy

+0

आप नंबर ले रहे हैं OfTapsRequired = 2, https://iosdevcenters.blogspot.com/2017/05/uitapgesturerecognizer-tutorial-in.html –

उत्तर

1

मैं एक विस्तार के साथ इस हल: `नल के लिए UITapGestureRecognizer`

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") 
    } 
} 
8

कोड

नीचे
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) 

    // DOUBLE TAP 
    tap.numberOfTapsRequired = 2 
    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) 
} 
} 
5

यहाँ की कोशिश करो एक सिंगल और डबल नल कार्रवाई के लिए कुछ उदाहरण कोड है।

ध्यान दें कि एकल टैप एक्शन में एक कोलन (handleSingleTap:) है लेकिन डबल टैप एक्शन (handleDoubleTap) नहीं है? यह इंगित करता है कि चयनकर्ता एक तर्क लेता है। तो नीचे दिए गए कार्यों में, हम एकल टैप के प्रेषक को चेक कर सकते हैं जो उपयोगी होगा यदि आप कई तत्वों को टैप फ़ंक्शन असाइन कर रहे थे। यदि आपके पास कोलन है तो आपके पास फ़ंक्शन में पैरामीटर होना चाहिए।

override func viewDidLoad() { 
    super.viewDidLoad() 

    // Single Tap 
    let singleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "handleSingleTap:") 
    singleTap.numberOfTapsRequired = 1 
    self.view.addGestureRecognizer(singleTap) 

    // Double Tap 
    let doubleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "handleDoubleTap") 
    doubleTap.numberOfTapsRequired = 2 
    self.view.addGestureRecognizer(doubleTap) 
} 

func handleSingleTap(sender: AnyObject?) { 
    print("Single Tap!") 
} 
func handleDoubleTap() { 
    print("Double Tap!") 
} 

नोट: यह उदाहरण एक डबल टैप में एकल टैप कार्रवाई चलाएगा।

7
 // Single Tap 
    let singleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(PostDetailViewController.handleSingleTap(sender:))) 
    singleTap.numberOfTapsRequired = 1 
    self.viewTop.addGestureRecognizer(singleTap) 

    // Double Tap 
    let doubleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(PostDetailViewController.handleDoubleTap)) 
    doubleTap.numberOfTapsRequired = 2 
    self.viewTop.addGestureRecognizer(doubleTap) 

    singleTap.require(toFail: doubleTap) 
    singleTap.delaysTouchesBegan = true 
    doubleTap.delaysTouchesBegan = true 

    func handleSingleTap(sender: AnyObject?) { 

    print("Single Tap!") 

    } 
    func handleDoubleTap() { 

    print("Double Tap!") 
    } 

जादू में तीन लाइनों

नीचे
singleTap.require(toFail: doubleTap) 
    singleTap.delaysTouchesBegan = true 
    doubleTap.delaysTouchesBegan = true 
0

अधिक Swifty रास्ता:

view?.tap(numberOfTapsRequired: 2, action: action) 

बंद:

private lazy var action: Action = Action(action: {[weak self] _ in 
    print("Double click") 
}) 

यह अपनी कार्रवाई के संदर्भ में खोने के लिए नहीं बहुत महत्वपूर्ण है (आप इसे उपरोक्त एक्सा में फ़ील्ड के रूप में बना सकते हैं mple)।

इस UIView एक्सटेंशन का उपयोग करके: https://gist.github.com/yoman07/7aae5abb957ff656b2f118ad1e6c2d36