[ios] button countdown

1. Android method:

Using the CountDownTimer Class

new CountDownTimer(getResources().getInteger(R.integer.change_count_max_value) * 1000, 1000) {

                public void onTick(long millisUntilFinished) {
                    valueCode.setText("" + (int)(millisUntilFinished/1000));
                }

                public void onFinish() {
                    //timeOutFlag = true;
                    valueCode.setText(R.string.get_code);

                    changeButton.setEnabled(true);
                }
            }.start();
        }
    }
});

 

2. Under IOS:

Ideas:

  • Create button, add click method;
  • Use the NSTimer timer, execute once per second, change the title of the Button regularly, change the style of the Button, and set the Button to be unclickable;
  • If the countdown is over, the timer is turned off, and the style of the Button is changed, which can be clicked;

Call this method on the button's click event:

-(void)openCountDonw{
     __block NSInteger time = 59;
    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //execute every second
    
    dispatch_source_set_event_handler(_timer, ^{
        
        if(time <= 0){ //Countdown ends, close
            
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //set the style of the button
                [_getCode setTitle:NSLocalizedString(@"get_code", @"") forState:UIControlStateNormal];
                _getCode.userInteractionEnabled = YES;
            });
            
        }else{
            
            int seconds = time % 60;
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //Set the button to display the countdown effect
                [_getCode setTitle:[NSString stringWithFormat:@"%.2d", seconds] forState:UIControlStateNormal];
                [_getCode setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                _getCode.userInteractionEnabled = NO;
            });
            time--;
        }
    });
    dispatch_resume(_timer);
}

 

important point:

 

When we create a Button, we need to set the style of the Button:
when the type is: UIButtonTypeCustom, it is a countdown effect.
When the type is: other, it is a flashing effect.

 

concept:

1. __block : The value of external local variables can be read inside the block. But when we need to change the value of this variable, we need to attach the __block modifier to it. The above NSInteger time variable is defined outside the Block, and time is used inside the Block--;

2. userInteractionEnabled : When the current view is set to view.userInteractionEnabled=NO, the current view cannot interact, and the subviews above the view cannot interact with the user (if they cannot respond, they are ignored by the view), and the response event is passed to the parent view below. No other buttons can be clicked at this time.

3. dispatch_queue_t  queue =  dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); //Get the default concurrent queue generated by the program process, you can set the priority to select high, medium and low priority queues. Since it is generated by default by the system, dispatch_resume() and dispatch_suspend() cannot be called to control execution continuation or interruption. It is important to note that three queues do not represent three threads, there may be more threads. The concurrent queue can automatically generate a reasonable number of threads according to the actual situation. It can also be understood that the dispatch queue implements the management of a thread pool and is transparent to the program logic.

4. dispatch_source_t: a timer with higher precision than NSTimer, which is automatically triggered by the system and is a system-level source.

5. dispatch_resume() : restore the queue  dispatch_source_cancel: cancel the queue

 

6. dispatch_async(queue,block)  async asynchronous queue, the dispatch_async function will return immediately, and the block will be executed asynchronously in the background

 

refer to:

1. Implementation of iOS SMS verification code countdown button: http://www.cnblogs.com/leixu/articles/5457580.html

2. userInteractionEnabled : http://www.jianshu.com/p/febef5ce9adc

3. GCD basic knowledge collection: http://www.jianshu.com/p/1f48eb97922b

4. Timer: http://www.jianshu.com/p/faa6ffe4fac3

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326297553&siteId=291194637