2013-10-26 8 views
27

से छूएं IUiewiew को छूने और UIView को टैप करने के तरीके का पता लगाने की समस्या के साथ अटक गया है। जब इसे छुआ जाता है, तो मैं UIView को अपने पृष्ठभूमि रंग को बदलना चाहता हूं। जब इसे छुआ जाता है, तो मैं UIView को कुछ कार्य करने के लिए चाहता हूं। मैं जानना चाहता हूं कि मैं इस समस्या को कैसे ठीक कर सकता हूं।आईओएस टैप डाउन का पता लगाएं और UIView

-(void)viewDidLoad 
{   
    UITapGestureRecognizer *dismissGestureRecognition = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDismissDoubleTap:)]; 
    dismissGestureRecognition.numberOfTapsRequired = 1; 
    [sectionDismissDoubleView addGestureRecognizer:dismissGestureRecognition]; 

    UITapGestureRecognizer *dismissGestureDownRecognition = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissGestureDownRecognition:)]; 
    dismissGestureRecognition.numberOfTouchesRequired = 1; 
    [sectionDismissDoubleView addGestureRecognizer:dismissGestureDownRecognition]; 
} 

- (void)handleDismissDoubleTap:(UIGestureRecognizer*)tap { 
    SettingsDismissDoubleViewController *settingsDouble = [[SettingsDismissDoubleViewController alloc] initWithNibName:@"SettingsDismissDoubleViewController" bundle:nil]; 
    [self.navigationController pushViewController:settingsDouble animated:YES]; 
} 

- (void)dismissGestureDownRecognition:(UIGestureRecognizer*)tap { 
    NSLog(@"Down"); 
} 

उत्तर

34

एक इशारा पहचानकर्ता शायद आप क्या चाहते हैं के लिए overkill है। आप शायद -touchesBegan:withEvent: और -touchesEnded:withEvent: के संयोजन का उपयोग करना चाहते हैं।

यह त्रुटिपूर्ण है, लेकिन आपको यह करना चाहिए कि आप क्या करना चाहते हैं।

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    self.touchDown = YES; 
    self.backgroundColor = [UIColor redColor]; 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    // Triggered when touch is released 
    if (self.isTouchDown) { 
     self.backgroundColor = [UIColor whiteColor]; 
     self.touchDown = NO; 
    } 
} 

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    // Triggered if touch leaves view 
    if (self.isTouchDown) { 
     self.backgroundColor = [UIColor whiteColor]; 
     self.touchDown = NO; 
    } 
} 

इस कोड UIView आपके द्वारा बनाए गए के एक कस्टम उपवर्ग में जाना चाहिए। फिर UIView के बजाय इस कस्टम व्यू प्रकार का उपयोग करें और आपको टच हैंडलिंग मिल जाएगी।

1

सबसे पहले, अपने चयनकर्ता handleDismissDoubleTap: से, मैं तुम्हें एक डबल टैप को खारिज करने के लिए देख रहे हैं मान चाहते हैं। इस लक्ष्य को हासिल करने के लिए आपको क्या करना चाहिए: dismissGestureRecognition.numberOfTapsRequired = 2;

दूसरे, अगर स्पर्श से नीचे आप एक लंबे समय तक नल मतलब (या एक "टैप करके रखें") इशारा की तरह, आप एक UILongPressGestureRecognizerUITapGestureRecognizer के बजाय का उपयोग करना चाहिए।

6

हर UIControl उपवर्ग (UIButton, आदि) आप इस का उपयोग कर सकते UIControlEvent का एक विशिष्ट सेट की सदस्यता के लिए में:

addTarget:action:forControlEvents 

आप UIControlEventTouchDown के लिए उपयुक्त चयनकर्ता और UIControlEventTouchUpInside घटना के लिए एक और लक्ष्य/चयनकर्ता के साथ लक्ष्य जोड़ना चाहिए ।

UIControl reference

+4

प्रश्न uiview नहीं है uicontrol – trickster77777

0

Holly's answer के लिए धन्यवाद मैंने ButtonView सुविधा वर्ग बनाया।

संपादित करें: इस answer कहते हैं, UILongPressGestureRecognizer काफी तेजी से प्रतिक्रिया करता है इसलिए मैंने अपनी कक्षा अपडेट की।

उपयोग:

let btn = ButtonView() 
btn.onNormal = { btn.backgroundColor = .clearColor() } 
btn.onPressed = { btn.backgroundColor = .blueColor() } 
btn.onReleased = yourAction // Function to be called 

कक्षा:

/** View that can be pressed like a button */ 

import UIKit 

class ButtonView : UIView { 

    /* Called when the view goes to normal state (set desired appearance) */ 
    var onNormal = {} 
    /* Called when the view goes to pressed state (set desired appearance) */ 
    var onPressed = {} 
    /* Called when the view is released (perform desired action) */ 
    var onReleased = {} 

    override init(frame: CGRect) 
    { 
     super.init(frame: frame) 

     let recognizer = UILongPressGestureRecognizer(target: self, action: Selector("touched:")) 
     recognizer.delegate = self 
     recognizer.minimumPressDuration = 0.0 
     addGestureRecognizer(recognizer) 
     userInteractionEnabled = true 

     onNormal() 
    } 

    func touched(sender: UILongPressGestureRecognizer) 
    { 
     if sender.state == .Began { 
      onPressed(self) 
     } else if sender.state == .Ended { 
      onNormal(self) 
      onReleased() 
     } 
    } 

    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 
} 
+0

मैं अंत में 'यूआईसींट्रोल' का उपयोग कर एक संस्करण में बदल गया हूं, जो इतनी जल्दी प्रतिक्रिया नहीं करता है लेकिन अन्य फायदे हैं: यदि आप बटन खींचते हैं तो आप नीचे 'UIScrollView' खींच सकते हैं। इस तरह स्क्रॉल व्यू फंस नहीं जाता है जब आप इसे खींचने का प्रयास करते हैं और बटन खींचने से आपका ड्रैग इशारा शुरू होता है। http://stackoverflow.com/a/40740616/1121497 –

27

enter image description here

इस विधि कुछ भी उपवर्गीकरण आवश्यकता नहीं है। आप दृश्य में UILongPressGestureRecognizer जोड़ें और minimumPressDuration को शून्य पर सेट करें। फिर आप राज्य की जांच करते हैं जब इशारा घटनाओं को देखने के लिए कहा जाता है कि स्पर्श घटना शुरू हो रही है या समाप्त हो रही है या नहीं।

उपर्युक्त उदाहरण छवि के लिए यहां संपूर्ण प्रोजेक्ट कोड है।

import UIKit 
class ViewController: UIViewController { 

    @IBOutlet weak var myView: UIView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Add "long" press gesture recognizer 
     let tap = UILongPressGestureRecognizer(target: self, action: #selector(tapHandler)) 
     tap.minimumPressDuration = 0 
     myView.addGestureRecognizer(tap) 
    } 

    // called by gesture recognizer 
    func tapHandler(gesture: UITapGestureRecognizer) { 

     // handle touch down and touch up events separately 
     if gesture.state == .Began { 
      myView.backgroundColor = UIColor.darkGrayColor() 
     } else if gesture.state == .Ended { 
      myView.backgroundColor = UIColor.lightGrayColor() 
     } 
    } 
} 

विचार के लिए this answer पर धन्यवाद।

+2

यह एक सर्वश्रेष्ठ स्क्रॉलव्यू को – xleon

+0

स्क्रॉलिंग से सबसे अच्छा जवाब (सामान्य रूप से सुरगच के लिए सामान्य रूप से) से रोक देगा! – Fattie

संबंधित मुद्दे