UIButton लॉन्ग प्रेस इवेंट


86

मैं एक लंबे प्रेस बटन का अनुकरण करना चाहता हूं, मैं यह कैसे कर सकता हूं? मुझे लगता है कि एक टाइमर की जरूरत है। मैं देखता हूं UILongPressGestureRecognizerलेकिन मैं इस प्रकार का उपयोग कैसे कर सकता हूं?

जवाबों:


160

आप UILongPressGestureRecognizerबटन को उदाहरण बनाकर और संलग्न करके शुरू कर सकते हैं ।

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[self.button addGestureRecognizer:longPress];
[longPress release];

और फिर उस विधि को लागू करें जो इशारे को संभालती है

- (void)longPress:(UILongPressGestureRecognizer*)gesture {
    if ( gesture.state == UIGestureRecognizerStateEnded ) {
         NSLog(@"Long Press");
    }
}

अब यह मूल दृष्टिकोण होगा। आप प्रेस की न्यूनतम अवधि और कितनी त्रुटि सहनीय है, यह भी निर्धारित कर सकते हैं। और यह भी ध्यान दें कि विधि को कुछ समय कहा जाता है यदि आप इशारे को पहचानने के बाद हैं इसलिए यदि आप इसके अंत में कुछ करना चाहते हैं, तो आपको इसकी स्थिति की जांच करनी होगी और इसे संभालना होगा।


उत्तम! धन्यवाद! Btw: अगर (इशारा.स्टेट == UIGestureRecognizerStateEnded) बहुत महत्वपूर्ण है, अन्यथा आप अपने लॉन्गप्रेस शून्य में घटनाओं का एक बहुत कुछ प्राप्त करेंगे
RecycleRobot

29
आप शायद उपयोग करना चाहते हैं if(gesture.state == UIGestureRecognizerStateBegan), क्योंकि उपयोगकर्ता को कुछ होने की उम्मीद है जब वे अभी भी दबा रहे हैं (राज्य की शुरुआत), न कि जब वे रिलीज (एंडी) जारी करते हैं।
shengbinmeng

28

स्वीकृत उत्तर के विकल्प के रूप में, यह इंटरफ़ेस बिल्डर का उपयोग करके Xcode में बहुत आसानी से किया जा सकता है।

बस ऑब्जेक्ट लाइब्रेरी से एक लॉन्ग प्रेस जेस्चर रिकग्निज़र खींचें और इसे उस बटन के ऊपर छोड़ दें जहाँ आप लंबी प्रेस कार्रवाई चाहते हैं।

इसके बाद, लॉन्ग प्रेस जेस्चर रिकॉगनाइज़र से एक एक्शन कनेक्ट करें, जो कि आपके व्यू कंट्रोलर के पास है, जो भेजने वाले का प्रकार का चयन करता है UILongPressGestureRecognizer। उस कोड के IBActionउपयोग में, जो स्वीकृत उत्तर में सुझाए गए कोड के समान है:

में ऑब्जेक्टिव-सी :

if ( sender.state == UIGestureRecognizerStateEnded ) {
     // Do your stuff here
}

या स्विफ्ट में :

if sender.state == .Ended {
    // Do your stuff here
}

लेकिन मुझे यह स्वीकार करना होगा कि इसकी कोशिश करने के बाद, मैं @shengbinmeng द्वारा दिए गए सुझाव को स्वीकार किए गए उत्तर के लिए एक टिप्पणी के रूप में पसंद करता हूं, जिसका उपयोग करना था:

में ऑब्जेक्टिव-सी :

if ( sender.state == UIGestureRecognizerStateBegan ) {
     // Do your stuff here
}

या स्विफ्ट में :

if sender.state == .Began {
    // Do your stuff here
}

अंतर यह है कि Endedजब आप अपनी उंगली उठाते हैं तो आपको लंबे प्रेस का प्रभाव दिखाई देता है। इसके साथ Began, आप लंबे प्रेस के प्रभाव को देखते हैं जैसे ही लंबे प्रेस को सिस्टम द्वारा पकड़ा जाता है, इससे पहले ही आप स्क्रीन से उंगली उठा लेते हैं।


18

स्वीकृत उत्तर का स्विफ्ट संस्करण

मैंने इसके UIGestureRecognizerState.Beganबजाय उपयोग करने का अतिरिक्त संशोधन किया.Ended क्योंकि संभवतः वह है जो अधिकांश उपयोगकर्ता स्वाभाविक रूप से उम्मीद करेंगे। उन दोनों को आज़माएं और अपने लिए देखें, हालाँकि।

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var button: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // add gesture recognizer
        let longPress = UILongPressGestureRecognizer(target: self, action: #selector(longPress(_:)))
        self.button.addGestureRecognizer(longPress)
        
    }

    func longPress(gesture: UILongPressGestureRecognizer) {
        if gesture.state == UIGestureRecognizerState.began {
            print("Long Press")
        }
    }
    
    @IBAction func normalButtonTap(sender: UIButton) {
        print("Button tapped")
    }
}

8

इसे इस्तेमाल करे:

viewDidLoad:नीचे की तरह बटन जोड़ना

-(void)viewDidLoad {
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btn setTag:1]; //you can set any integer value as tag number
    btn.title = @"Press Me";
    [btn setFrame:CGRectMake(50.0, 50.0, 60.0, 60.0)];

    // now create a long press gesture
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressTap:)];
    [btn addGestureRecognizer:longPress];
}

अब जेस्चर विधि को इस तरह से कॉल करें

-(void)longPressTap:(id)sender {
     UIGestureRecognizer *recognizer = (UIGestureRecognizer*) sender
    // Recogniser have all property of button on which you have clicked
    // Now you can compare button's tag with recogniser's view.tag  
    // View frame for getting the info on which button the click event happened 
    // Then compare tag like this
    if(recognizer.view.tag == 1) { 
       // Put your button's click code here
    }

    // And you can also compare the frame of your button with recogniser's view
    CGRect btnRect = CGRectMake(50.0, 50.0, 60.0, 60.0);
    if(recogniser.view.frame == btnRect) {
       //put your button's click code here
    }

   // Remember frame comparing is alternative method you don't need  to write frame comparing code if you are matching the tag number of button 
}

recognizer.view.tagमुझे UIButton क्लिक का गलत टैग देता है। कोई भी समाधान?
रोहन-पटेल

3

मुझे लगता है कि आपको मेरे समाधान की आवश्यकता है।

आपके पास एकल प्रेस के लिए यह कोड होना चाहिए

- (IBAction)buttonDidPress:(id)sender {
    NSLog("buttonDidPress");
}

सबसे पहले, बटन के लिए लंबे प्रेस इशारा जोड़ें

- (void)viewWillAppear:(BOOL)animated
{
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(buttonDidLongPress:)];
    [self.button addGestureRecognizer:longPress];
}

फिर सिंगल प्रेस इवेंट को बार-बार कॉल करें यदि लॉन्ग प्रेस जेस्चर को पहचाना जाता है।

- (void)buttonDidLongPress:(UILongPressGestureRecognizer*)gesture
{
    switch (gesture.state) {
        case UIGestureRecognizerStateBegan:
        {
            self.timer = [NSTimer timerWithTimeInterval:0.1 target:self selector:@selector(buttonDidPress:) userInfo:nil repeats:YES];

            NSRunLoop * theRunLoop = [NSRunLoop currentRunLoop];
            [theRunLoop addTimer:self.timer forMode:NSDefaultRunLoopMode];
        }
            break;
        case UIGestureRecognizerStateEnded:
        {
            [self.timer invalidate];
            self.timer = nil;
        }
            break;
        default:
            break;
    }
}

आपको जीवनचक्र घटना के UIGestureRecognizerदौरान नहीं जोड़ना चाहिए viewWillAppearक्योंकि जब भी दृश्य दिखाई देता है, एक और इशारा पहचानकर्ता जोड़ा जाएगा। यह एक निजी विधि में किया जाना चाहिए जिसे आरंभीकरण के दौरान कहा जाता है।
विकिपीडिया

3

स्विफ्ट 4 के लिए, "फंक लॉन्गप्रेस" को इसे काम करने के लिए बदलने की आवश्यकता है:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var button: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()

        // add guesture recognizer
        let longPress = UILongPressGestureRecognizer(target: self, action: #selector(longPress(_:)))
        self.button.addGestureRecognizer(longPress)

    }

   @objc func longPress(_ guesture: UILongPressGestureRecognizer) {
        if guesture.state == UIGestureRecognizerState.began {
            print("Long Press")
        }
    }

    @IBAction func normalButtonTap(sender: UIButton) {
        print("Button tapped")
    }
}

1

एक-पंक्ति का उत्तर, बिना किसी इशारे के:

[btn addTarget:self action:@selector(handleTouch:) forControlEvents:UIControlEventTouchDown | UIControlEventTouchUpInside | UIControlEventTouchUpOutside];

विवरण: यह तीन घटनाओं पर आपके लक्ष्य को ट्रिगर करता है: 1- तुरंत एक बार उंगली बटन को छूने पर UIControlEventTouchDown:। इससे लंबे प्रेस शुरू होते हैं। 2 और 3- जब उपयोगकर्ता उंगली उठाता है: UIControlEventTouchUpOutside& UIControlEventTouchUpInside। यह उपयोगकर्ता प्रेस के अंत को दर्शाता है।

ध्यान दें: यह अच्छी तरह से काम करता है यदि आप जेस्चर पहचानकर्ता द्वारा प्रदान की गई अतिरिक्त जानकारी (जैसे स्पर्श का स्थान, आदि) की परवाह नहीं करते हैं

यदि आप उन सभी को यहां देख सकते हैं, तो आप अधिक मध्यवर्ती ईवेंट जोड़ सकते हैं https://developer.apple.com/documentation/uikit/uicontrolevents?language=objc

स्टोरीबोर्ड में: अपने बटन को 3 घटनाओं से कनेक्ट करें, न कि केवल एक डिफ़ॉल्ट जिसे स्टोरीबोर्ड चुनता है (अंदर टच करें)।

स्टोरीबोर्ड में 3 घटनाएं


0

मेरे पास मेरे ऐप के लिए एक उप-वर्गित UIButton है, इसलिए मैंने अपना कार्यान्वयन निकाल लिया है। आप इसे अपने उपवर्ग में जोड़ सकते हैं या इसे यूआईबूटन श्रेणी के रूप में आसानी से पुन: व्यवस्थित किया जा सकता है।

मेरा लक्ष्य कोड के सभी के साथ अपने दृश्य नियंत्रकों को अव्यवस्थित किए बिना मेरे बटन को लंबे समय तक जोड़ना था। मैंने फैसला किया है कि इशारा पहचानकर्ता राज्य शुरू होने पर कार्रवाई को बुलाया जाना चाहिए।

एक चेतावनी है जो यह बताती है कि मैंने कभी हल करने की जहमत नहीं उठाई। कहते हैं, यह एक संभावित लीक है, मुझे लगा कि मैंने कोड का परीक्षण किया है और यह लीक नहीं हुआ है।

@interface MYLongButton ()
@property (nonatomic, strong) UILongPressGestureRecognizer *gestureRecognizer;
@property (nonatomic, strong) id gestureRecognizerTarget;
@property (nonatomic, assign) SEL gestureRecognizerSelector;
@end

@implementation MYLongButton

- (void)addLongPressTarget:(CGFloat)interval target:(id)target action:(SEL)selector
{
    _gestureRecognizerTarget = target;
    _gestureRecognizerSelector = selector;
    _gestureRecognizer = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPressGestureRecognizer:)];
    _gestureRecognizer.minimumPressDuration = interval;

    [self addGestureRecognizer:_gestureRecognizer];
}

- (void)handleLongPressGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
{
    if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
        NSAssert([_gestureRecognizerTarget respondsToSelector:_gestureRecognizerSelector], @"target does not respond to selector");

        self.highlighted = NO;

        // warning on possible leak -- can anybody fix it?
        [_gestureRecognizerTarget performSelector:_gestureRecognizerSelector withObject:self];
    }
}

इस कार्य को असाइन करने के लिए इस लाइन को अपने व्यूडलॉड विधि से जोड़ें।

[_myLongButton addLongPressTarget:0.75 target:self selector:@selector(longPressAction:)];

कार्रवाई को सभी IBActions (IBAction के बिना) की तरह परिभाषित किया जाना चाहिए।

- (void)longPressAction:(id)sender {
    // sender is the button
}

0

कोई भी काम नहीं किया इसलिए मैंने लिखने के बजाय में IBActionसे लॉन्गप्रेस कोड या बटन क्लिक करने की कोशिश कीstoryboardControllerviewDidLoad

- (IBAction)btnClick:(id)sender {

    tag = (int)((UIButton *)sender).tag;

// Long press here instead of in viewDidLoad

    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    longPress.cancelsTouchesInView = NO;
    [sender addGestureRecognizer:longPress];

}
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.