Buttonが複数回クリックして繰り返し実行する方法の問題を解決する

11842 ワード

NSInteger _currentClickNum;//Save the current value of the tag button is clicked //Button click event
- (void)tabBt1nClicked:(UIButton *)sender 
{     NSInteger index = sender.tag;
   
if (index == _currentClickNum) {
       
NSLog(@"Click on the selected current topic, not execution method, avoiding duplicate clicks");
   
}else {
       
[[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(tabBtnClicked:) object:sender];
        sender
.enabled = NO;
        dispatch_after
(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            sender
.enabled = YES;
       
});
        _currentClickNum 
= index;
       
NSLog(@"Column is the current click:%ld",_currentClickNum);
   
}

}
or -(IBAction) buttonClick:(id)sender{
    button
.enabled = false;
   
[UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionAllowAnimatedContent animations:^{
       
// code to execute
     
}
     completion
:^(BOOL finished){
         
// code to execute 
        button
.enabled = true; //This is correct.
   
}];
   
//button.enabled = true; //This is wrong.

}
参照ドキュメント:http://stackoverflow.com/questions/28343070/how-do-i-prevent-multiple-event-on-same-uibutton-in-ios/39724239#39724239

iOSボタンの高速連続クリックによる複数回の応答を防止する方法


文字数256
667を読む
コメント4
好きです
日常開発でよく遭遇するバグは,ユーザがボタンをすばやくクリックし,ページがpushを繰り返したり,ネットワークリクエストを繰り返したりするためである.このような問題は、ユーザー体験に影響を与えるだけでなく、サーバの圧力もある程度増加します.現在、私はボタンの急速なクリックを防止するために主に以下の2つの方法を使用している.クリックするたびに前の操作をキャンセルする(ネットで見た方法)- (void)buttonClicked:(id)sender { // , , [[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(buttonClicked:) object:sender]; [self performSelector:@selector(buttonClicked: )withObject:sender afterDelay:0.2f]; }2.クリックしてボタンをクリック不可にし、数秒後に回復する-(void)buttonClicked:(id)sender{ self.button.enabled = NO; [self performSelector:@selector(changeButtonStatus) withObject:nil afterDelay:1.0f];// } -(void)changeButtonStatus{ self.button.enabled = YES; }もし皆さんがもっと良い解決方法があれば、私に教えてください.