0
我有一个UIScrollView
并且想要设置一个定时器,所以如果我滚动并且什么也不做,大约10秒钟将会调用一个工作方法,并且如果在这10秒之前滚动,定时器将会重置。我的代码不起作用。UIScrollView定时器
// Global vars
NSDate *startDate, *endDate;
// myTimer (NSTimer) and timerIsValid(BOOL) are part of my viewController class
- (BOOL)timerExpired
{
NSDate *dateNow=[NSDate date];
if ([[dateNow laterDate:endDate] isEqual:endDate]) {
return YES;
}
else {
return NO;
}
}
// If i begin scrolling i want to stop the timer
- (void)scrollViewWillBeginDragging:(UIScrollView *)myScrollView
{
// Check if the timer is alive and invalidate it
if ([self timerExpired] == NO) {
NSLog(@"TIMER STILL ALIVE...KILL IT");
[myTimer invalidate];
myTimer = nil;
timerIsValid = NO;
}
else {
NSLog(@"TIMER IS DEAD");
timerIsValid = YES;
}
}
// After 10 seconds if i didn't scroll, i want to some job
- (void)scrollViewDidEndDecelerating:(UIScrollView *)myScrollView
{
if (endDate) {
[endDate release];
}
startDate = [NSDate date];
endDate = [[startDate addTimeInterval:10.0] retain];
if (!myScrollView.dragging)
{
if (autoscrollTimer == nil) {
// Create a new timer
myTimer = [NSTimer scheduledTimerWithTimeInterval:10.0
target:self
selector:@selector(timerFireMethod:)
userInfo:nil
repeats:YES];
}
}
}
- (void)timerFireMethod:(NSTimer*)theTimer
{
if (timerIsValid) {
// do nothing
NSLog(@"DO NOTHING");
}
else {
NSLog(@"DO SOME JOB");
[self doSomeJob];
timerIsValid = YES;
}
}
嗨,伙计,这是一种优雅的方式来做到这一点:),真的谢谢。上帝保佑你 :)。 – funnyCoder 2011-05-23 20:01:16
非常欢迎。但我更喜欢是否有任何传奇人物让我一个人待着。 – 2011-05-23 20:59:10
我也是:)谢谢。 – funnyCoder 2011-05-23 21:59:06