2010-07-19 13 views
15

मुझे ऐसा करने का एक आसान तरीका नहीं मिला है। मैंने जिन तरीकों को देखा है, इन सभी टाइमर और सामान की आवश्यकता है। क्या कोई आसान तरीका है कि मैं UIButton पकड़ सकता हूं और इसे जारी होने तक कार्रवाई को बार-बार दोहराने का कारण बनता है?यूआईबटन टच और होल्ड

उत्तर

17

आप निम्न कार्य कर सकते हैं: एक एनएसटीमर बनाएं जो ऐप शुरू होने पर या दृश्य में दिखाई देगा और एक बूलियन भी बनायेगा।

उदाहरण के लिए:

//Declare the timer, boolean and the needed IBActions in interface. 
@interface className { 
NSTimer * timer; 
bool g; 
} 
-(IBAction)theTouchDown(id)sender; 
-(IBAction)theTouchUpInside(id)sender; 
-(IBAction)theTouchUpOutside(id)sender; 

//Give the timer properties. 
@property (nonatomic, retain) NSTimer * timer; 
अब आप अपने कार्यान्वयन फ़ाइल में

(मीटर):

//Synthesize the timer 
@synthesize timer; 
//When your view loads initialize the timer and boolean. 
-(void)viewDidLoad { 
    g = false; 
    timer = [NSTimer scheduledTimerWithInterval: 1.0 target:self selector:@selector(targetMethod:) userInfo:nil repeats: YES]; 
} 

अब के लिए एक IBAction बनाने "टच डाउन" बूलियन सेट सच कहना देता है। फिर "टच अप इनसाइड" और "टच अप आउटसाइड" के लिए एक और IBAction बटन बनाएं, बूलियन को झूठी असाइन करें।

उदाहरण के लिए: (मान जी बूलियन आप घोषणा की है है)

-(void) targetmethod:(id)sender { 
    if (g == true) { 
     //This is for "Touch and Hold" 
    } 
    else { 
     //This is for the person is off the button. 
    } 
} 

मुझे आशा है कि यह सब कुछ सरल करता है ... मैं:

-(IBAction)theTouchDown { 
    g = true; 
} 

-(IBAction)theTouchUpInside { 
    g = false; 
} 

-(IBAction)theTouchUpOutside { 
    g = false; 
} 

फिर उस NSTimer विधि में, निम्नलिखित डाल पता है कि यह अभी भी एक टाइमर का उपयोग करता है लेकिन एक और तरीका नहीं है।

+1

बहुत "hacky" लेकिन यह पूरी तरह से काम करता है! –

+0

समय निकालें और इसका सही – junaidsidhu

11

दुर्भाग्यवश, ऐसा लगता है कि आपको अपने लिए इस कार्यक्षमता को कोड करना होगा। सबसे आसान तरीका है (आप अभी भी हालांकि एक टाइमर की जरूरत है):

एक समारोह है कि कार्रवाई आप दोहराना चाहते हैं करता है:

-(void) actionToRepeat:(NSTimer *)timer 
{ 
    NSLog(@"Action triggered"); 
} 

अपने ज फ़ाइल घोषणा में और एक टाइमर के लिए एक गुण सेट:

@interface ClassFoo 
{ 
    NSTimer* holdTimer; 
} 

तो मीटर में बना दो IBActions:

-(IBAction) startAction: (id)sender 
{ 
    holdTimer = [NSTimer scheduledTimerWithTimeInterval:0.4 target:self selector:@selector(actionToRepeat:) userInfo:nil repeats:YES]; 
    [holdTimer retain]; 
} 

-(IBAction) stopAction: (id)sender 
{ 
    [holdTimer invalidate]; 
    [holdTimer release]; 
    holdTimer = nil; 
} 

तो बस +०१२३६०६४८६६४ से लिंकबटन से आईबी में startAction और Touch Up Inside 'स्टॉप एक्शन' में ईवेंट। यह एक लाइनर नहीं है लेकिन यह आपको एक्शन दोहराने की दर को अनुकूलित करने के साथ-साथ आपको इसे किसी अन्य आउटलेट/एक्शन से ट्रिगर करने की अनुमति देता है।

आप UIButton उपclassing पर विचार कर सकते हैं और यदि आप अक्सर इस कार्यक्षमता का उपयोग करने जा रहे हैं तो यह कार्यक्षमता जोड़ना - तो यह पहली बार लागू करने के लिए केवल (थोड़ा) दर्दनाक है।

+0

मुझे लगता है कि अमान्य स्थायी रूप से टाइमर को हटा देता है, है ना? आईफोन एसडीके संदर्भ से: अमान्य: रिसीवर को फिर से फायरिंग से रोकता है और इसके रन लूप से हटाने का अनुरोध करता है। –

+0

हां - क्या यह आवश्यक कार्यक्षमता नहीं है? अगली बार जब आप – davbryn

1

मैं पहले एक का जवाब नहीं दे सकता है, लेकिन इस लाइन:

timer = [NSTimer scheduledTimerWithInterval: 1.0 target:self selector:@selector(targetMethod:) userInfo:nil repeats: YES]; 
के लिए कम से कम iOS 4.1 और नए की जरूरत होने की

:

timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(targetMethod:) userInfo:nil repeats: YES]; 
9

एक अन्य this NBTouchAndHoldButton उपयोग करने के लिए जिस तरह से। यह वही है जो आप चाहते हैं, और इसे कार्यान्वित करने में बहुत आसान है:

TouchAndHoldButton * pageDownButton = [TouchAndHoldButton buttonWithType:UIButtonTypeCustom]; 
[pageDownButton addTarget:self action:@selector(pageDownAction:) forTouchAndHoldControlEventWithTimeInterval:0.2]; 

शुभकामनाएँ!

+1

बटन दबाएंगे तो एक नया टाइमर बनाया जाएगा। किसी भी व्यक्ति को यह कैसे आयात करेगा। स्विफ्ट नियंत्रक प्रदान करता है कि हम हेडर को ब्रिजिंग-हेडर में आयात करते हैं ??? –

+2

@ जॉर्ज एस्डा स्विफ्ट संस्करण: https://github.com/vvit/TouchAndHoldButton – SoftDesigner

+1

ओपन सोर्स वर्ल्ड कैसे विकसित होता है इसका अनुभव करने के लिए एक सुखद क्षण ... – ingaham

0

मुझे पता है कि यह एक पुराना सवाल है, लेकिन किसी भी विशेष समय अंतराल में बार-बार तरीकों का आह्वान करने के लिए "[NSObject performSelector: withObject: afterDelay:]" का उपयोग करने पर विचार करना आसान तरीका है।

इस मामले में:

NSTimeInterval someTimeInterval = 1; 

- (IBAction)action:(id)sender { 
    UIButton * const button = sender; 
    if (button.state != UIControlStateHighlighted) { 
     return; 
    } 
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:_cmd object:sender]; 
    [self performSelector:_cmd withObject:sender afterDelay:someTimeInterval]; 
} 
संबंधित मुद्दे