在做点击按钮实现界面的跳转的时候,有这么一个需求:希望看到选中的按钮呈现出被选中的状态颜色.而实际情况是点击了按钮马上就跳转了。所以需要在点击后让实现跳转的代码暂时不执行.暂停的方式有很多,以下是比较常用的。
//测试方法
- (void)test{
NSLog(@"xxx");
}
//让某个方法暂停有以下方式:
OC自带的线程方法
self performSelector:@selector(test) withObject:nil afterDelay:<#(NSTimeInterval)#>
定时器
[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(test) userInfo:nil repeats:NO];
sleep
[NSThread sleepForTimeInterval:1.0f]; [self test];
GCD类似GCD的就不再鳌述了
double delayInSeconds = 1.0;
__block typeof(self) weakSelf = self;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[weakSelf test];
});