मैं एक लंबे प्रेस बटन का अनुकरण करना चाहता हूं, मैं यह कैसे कर सकता हूं? मुझे लगता है कि एक टाइमर की जरूरत है। मुझे UILongPressGestureRecognizer
दिखाई दे रहा है लेकिन मैं इस प्रकार का उपयोग कैसे कर सकता हूं?
8 जवाब
आप बटन पर 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");
}
}
अब यह मूल दृष्टिकोण होगा। आप प्रेस की न्यूनतम अवधि भी निर्धारित कर सकते हैं और कितनी त्रुटि सहनीय है। और यह भी ध्यान दें कि यदि आप हावभाव को पहचानने के बाद विधि को कुछ बार कहते हैं, तो यदि आप इसके अंत में कुछ करना चाहते हैं, तो आपको इसकी स्थिति की जांच करनी होगी और इसे संभालना होगा।
स्वीकृत उत्तर का स्विफ्ट संस्करण
मैंने .Ended
के बजाय UIGestureRecognizerState.Began
का उपयोग करने का अतिरिक्त संशोधन किया है क्योंकि संभवतः अधिकांश उपयोगकर्ता स्वाभाविक रूप से यही उम्मीद करेंगे। हालाँकि, उन दोनों को आज़माएँ और अपने लिए देखें।
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")
}
}
ये कोशिश करें:
नीचे की तरह 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
}
मुझे लगता है कि आपको मेरे समाधान की जरूरत है।
आपके पास सिंगल प्रेस के लिए यह कोड होना चाहिए
- (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;
}
}
स्विफ्ट 4 के लिए, इसे काम करने के लिए "func longPress" को बदलने की जरूरत है:
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")
}
}
एक-पंक्ति वाला उत्तर, बिना इशारों के:
[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 ईवेंट से कनेक्ट करें, न कि केवल डिफ़ॉल्ट जिसे स्टोरीबोर्ड चुनता है (इनसाइड टच अप)।
मेरे पास मेरे ऐप के लिए उप-वर्गीकृत UIButton है, इसलिए मैंने अपना कार्यान्वयन निकाला है। आप इसे अपने उपवर्ग में जोड़ सकते हैं या इसे आसानी से 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];
}
}
कार्रवाई असाइन करने के लिए इस लाइन को अपने viewDidLoad मेथड में जोड़ें।
[_myLongButton addLongPressTarget:0.75 target:self selector:@selector(longPressAction:)];
कार्रवाई को सभी IBActions (IBAction के बिना) की तरह परिभाषित किया जाना चाहिए।
- (void)longPressAction:(id)sender {
// sender is the button
}
किसी ने काम नहीं किया इसलिए मैंने IBAction
में लॉन्गप्रेस कोड लिखने की कोशिश की या viewDidLoad
में लिखने के बजाय storyboard
से Controller
में बटन क्लिक किया।
- (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];
}
संबंधित सवाल
जुड़े हुए प्रश्न
नए सवाल
ios
iOS, Apple iPhone, iPod टच और iPad पर चलने वाला मोबाइल ऑपरेटिंग सिस्टम है। IOS प्लेटफॉर्म पर प्रोग्रामिंग से संबंधित प्रश्नों के लिए इस टैग [ios] का उपयोग करें। उन प्रोग्रामिंग भाषाओं के लिए विशिष्ट मुद्दों के लिए संबंधित टैग [उद्देश्य-सी] और [स्विफ्ट] का उपयोग करें।