Long press button for fast growth and fast reduction of values

Source: Internet
Author: User

Whether in the application or in the game to let the user enter the number is troublesome, first need to call out the keyboard, followed by entering the number, and finally confirm the input, on such a small screen steps so troublesome, imagine, if let the user long press button to achieve rapid growth and rapid reduction, that much simple ah, well, nonsense not much to say, directly on the , the implementation is relatively simple, do not do too much explanation (if there is inappropriate, please point out, thank you)


viewcontroller.m////Created by Hunk on 15/2/12.//Copyright (c) 2015 Hunk. All rights reserved.//#import "ViewController.h" #define Stage_one #define STAGE_TWO (#define STAGE_THRE)        E (+) #define MINUS_BTN_TAG #define PLUS_BTN_TAG (101) @interface Viewcontroller () {UILabel *label;        Nstimer *timer;        Nsinteger Flag; Nsinteger index;}    @end @implementation viewcontroller-(void) viewdidload {[Super viewdidload];    index = 1;        Flag = 0;    label = [[UILabel alloc] Initwithframe:cgrectmake (50.0, 100.0, Cgrectgetwidth (self.view.frame)-100.0, 60.0)];    [Label Setbackgroundcolor:[uicolor Yellowcolor];    [Label Setfont:[uifont systemfontofsize:50.0];    [Label Settextalignment:nstextalignmentcenter];    [Label settext:@ (Index). StringValue];        [Self.view Addsubview:label];    UIButton *minusbutton = [UIButton buttonwithtype:uibuttontypecustom]; [Minusbutton setframe:cgrectmake (0.0, Cgrectgetminy (label.frame), 50.0, Cgrectgetheight (Label.frame))];    [Minusbutton Setbackgroundcolor:[uicolor Redcolor];    [Minusbutton settitle:@ "-" forstate:uicontrolstatenormal];    [Minusbutton.titlelabel Setfont:[uifont systemfontofsize:50];    [Minusbutton addtarget:self Action: @selector (buttonaction:) forcontrolevents:uicontroleventtouchupinside];    [Minusbutton Settag:minus_btn_tag];        [Self.view Addsubview:minusbutton];    Uilongpressgesturerecognizer *minuslongpressgesturerecognizer = [[Uilongpressgesturerecognizer alloc] init];    [Minuslongpressgesturerecognizer addtarget:self Action: @selector (minusgesturerecognizeraction:)];    [Minuslongpressgesturerecognizer setminimumpressduration:.5f];    [Minuslongpressgesturerecognizer setallowablemovement:10.0];        [Minusbutton Addgesturerecognizer:minuslongpressgesturerecognizer];    UIButton *plusbutton = [UIButton buttonwithtype:uibuttontypecustom]; [Plusbutton Setframe:cgrectmake (Cgrectgetwidth (self.view.frame)-50.0, Cgrectgetminy (label.frame), 50.0, Cgrectgetheight (Label.frame))];    [Plusbutton Setbackgroundcolor:[uicolor Greencolor];    [Plusbutton settitle:@ "+" forstate:uicontrolstatenormal];    [Plusbutton.titlelabel Setfont:[uifont systemfontofsize:50];    [Plusbutton addtarget:self Action: @selector (buttonaction:) forcontrolevents:uicontroleventtouchupinside];    [Plusbutton Settag:plus_btn_tag];        [Self.view Addsubview:plusbutton];    Uilongpressgesturerecognizer *pluslongpressgesturerecognizer = [[Uilongpressgesturerecognizer alloc] init];    [Pluslongpressgesturerecognizer addtarget:self Action: @selector (plusgesturerecognizeraction:)];    [Pluslongpressgesturerecognizer setminimumpressduration:.5f];    [Pluslongpressgesturerecognizer setallowablemovement:10.0]; [Plusbutton Addgesturerecognizer:pluslongpressgesturerecognizer];}                -(void) Buttonaction: (UIButton *) button{if (Minus_btn_tag = = [Button TAG]) {index--;        if (Index < 1) {index = 1; }} ElSe if (plus_btn_tag = = [Button TAG]) {index++; } [Label settext:@ (Index). StringValue];} -(void) Minusgesturerecognizeraction: (Uilongpressgesturerecognizer *) pangesturerecognizer{switch (                        Pangesturerecognizer.state) {case Uigesturerecognizerstatebegan: {flag =-1; Timer = [Nstimer scheduledtimerwithtimeinterval:0.1 target:self selector: @selector (updatetimer:) Userinfo:nil Rep        Eats:yes];        } break;        Case uigesturerecognizerstatechanged: {} break;            Case uigesturerecognizerstateended: {[timer invalidate];        Timer = nil;        } break;    Default:break; }}-(void) Plusgesturerecognizeraction: (Uilongpressgesturerecognizer *) pangesturerecognizer{switch (                        Pangesturerecognizer.state) {case Uigesturerecognizerstatebegan: {flag = 1; Timer = [Nstimer timerwithtimeinterval:0.1 target:self selector: @selector (updatetimer:) Userinfo:nil Repeats:yes];        [[Nsrunloop Currentrunloop] Addtimer:timer Formode:nsdefaultrunloopmode];        } break;        Case uigesturerecognizerstatechanged: {} break;            Case uigesturerecognizerstateended: {[timer invalidate];        Timer = nil;        } break;    Default:break; }}-(void) Updatetimer: (nstimeinterval) dt{if (Index > 0 && Index < stage_one) {index + = (1 * FL    AG);    } else if (index >= stage_one && Index < stage_two) {index + = (5 * flag);    } else if (index >= stage_two && Index < stage_three) {index + = (ten * flag);    } else {index + = (* flag);    } if (Index < 1) {index = 1; } [Label settext:@ (Index). StringValue];} -(void) Didreceivememorywarning {[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} @end


Long press button for fast growth and fast reduction of values

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.