iOS 防止按鈕快速點擊造成多次響應的避免方法

來源:互聯網
上載者:User

標籤:使用   os   for   io   時間   ar   

有時候有些操作是防止使用者在一次響應結束中再響應下一個。但有些測試使用者就要猛點,狂點。像這種惡意就要進行防止。

當然有些非同步作業時,可以在調用前enable 掉。等CallBACK 後再enable起來。過程中按鈕是不能點的。

 

1、可以使用:

- (void) timeEnough

{

  UIButton *btn=(UIButton*)[self.view viewWithTag:33];

  btn.selected=NO; 

[timer invalidate];

  timer=nil; 

}

 

 - (void) btnDone:(UIButton*)btn

 {

 if(btn.selected) return;

 btn.selected=YES;

 [self performSelector:@selector(timeEnough) withObject:nil afterDelay:3.0]; //使用延時進行限制。

//to do something.

}

 

2、個人覺得這種方法更為好用些。

 

- (void)todoSomething:(id)sender

{

    //在這裡做按鈕的想做的事情。

}

 

- (void)starButtonClicked:(id)sender

{

    //先將未到時間執行前的任務取消。

    [[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(todoSomething:) object:sender];

    [self performSelector:@selector(todoSomething:) withObject:sender afterDelay:0.2f];

}

 

對於第二種方法,快速點擊N次,只要每次間隔在0.2秒內的都不響應操作,等到停下點擊到達0.2秒後再執行。所以按照自己的需要設定回應時間,狂點吧,只響應一次。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.