???在滾動視圖(UITableView,UIScrollView)上添加的定時器在父視圖滾動的時候,定時器會停止:
整個頁面是一個UITableView,其實還沒截取完,上面還有圖片的滾動視圖,和一行滾動的公告信息。現在主要來看黃色框里面的,這是一個分區,分區在添加一個UIScrollView,里面的信息可以手動滑動,點擊查看。主要是自己滾動:
//添加一個定時器來讓UISrollView自己滾動,兩秒上移一行
NSTimer *bidTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(changeBidScroll) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:bidTimer forMode:NSDefaultRunLoopMode];
//滾動的方法,就是一個簡單的UIView動畫。
-(void)changeBidScroll{
[UIView animateWithDuration:0.5 animations:^{
CGPoint point = bidScroll.contentOffset; //bidScroll黃色框
point.y += 30*SCALE; //SCALE是一個宏定義,比例高度
bidScroll.contentOffset = point;
}];
//判斷信息數組是否滾動到最后一行,在滾動到第一行
if (bidScroll.contentOffset.y > self.bidNewArr.count * 30*SCALE) {
bidScroll.contentOffset = CGPointMake(0, 30*SCALE);
}
}
要想在整個視圖滾動的時候,下面的黃框內仍然自己滾動著:
[[NSRunLoop currentRunLoop] addTimer:bidTimer forMode:NSRunLoopCommonModes];
把NSDefaultRunLoopMode改為NSRunLoopCommonModes,就可以實現了。
程序啟動的時候,系統默認注冊了RunLoop的五種mode:
kCFRunLoopDefaultMode: 默認 mode,通常主線程在這個 mode 下運行,一般這樣寫我們:
NSTimer *bidTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(changeBidScroll) userInfo:nil repeats:YES];
定時器也可以在不添加進runloop時也自動滾動,是因為程序默認把這個定時器加入當前的默認kCFRunLoopDefaultMode(NSDefaultRunLoopMode)下。mainRunLoop一般也會得到這個mode。UITrackingRunLoopMode: 追蹤mode,這個就是我們剛剛說的NSRunLoopCommonModes,保證UIScrollview滑動順暢不受其他 mode 影響。所以當頁面滾動的時候,RunLoop的Mode會自動切換成UITrackingRunLoopMode模式,因此NSTimer會失效,當停止滑動,RunLoop又會切換回NSDefaultRunLoopMode模式,因此timer又會重新啟動了。滾動時mainRunLoop會得到這個mode。
UIInitializationRunLoopMode: 啟動程序后的過渡mode,啟動完成后就不再使用。
4: GSEventReceiveRunLoopMode: Graphic相關事件的mode,通常用不到。
5: kCFRunLoopCommonModes: 占位用的mode,作為標記kCFRunLoopDefaultMode和UITrackingRunLoopMode用
Runloop還有很多用處,但是我自己也沒用到就不瞎說了。
想深入了解RunLoop的話點這里。http://blog.ibireme.com/2015/05/18/runloop/
這里我還想問問路過的大神,從當前頁面push到下個頁面,再返回的時候怎么做才可以讓當前的頁面上定時器的滾動不會重頭開始,而是push時的那一個。方法盡量簡單合理一勞永逸,不要出現亂滾動。