Original
There is a runloop mode inside the timer, the general timer is running on the DefaultMode. However, if you swipe this page, the main thread runloop will go to the Uitrackingrunloopmode, this time can not handle the timer, resulting in timer failure, the reason is Runroop mode problem
NSDefaultRunLoopMode(kCFRunLoopDefaultMode): Default, Idle state
UITrackingRunLoopMode: ScrollView will switch to the mode when sliding
UIInitializationRunLoopMode: When run loop is started, it switches to the mode
NSRunLoopCommonModes(kCFRunLoopCommonModes)
two workarounds are available:1. Add the timer to the current thread message loop and specify the mode of the message loop asNsrunloopcommonmodes (can run regardless of which mode Runloop is running)2. Switch to the main thread to update the UI
Step 1. Put the nstimer in the child thread, but note that because the message loop from the thread is not turned on by default, there is a need to turn on the message loop of a thread
Step 2. Switch to the main thread to update the UI #import "ViewController.h"
@interface Viewcontroller ()
@property (Nonatomic,strong) Uiscrollview *scrollview;
@property (Nonatomic,strong) Nstimer *timer;
@property (Nonatomic,strong) UILabel *label;
@property (nonatomic,assign) float times;
@end
@implementation Viewcontroller
-(void) Viewdidload {
[Super Viewdidload];
Self.scrollview = [[Uiscrollview alloc]init];
self.scrollView.frame = self.view.frame;
Self.scrollView.backgroundColor = [Uicolor cyancolor];
self.scrollView.contentSize = Cgsizemake (375, a);
[Self.view AddSubview:self.scrollView];
Self.label = [[UILabel alloc]init];
self.label.frame = CGRectMake ( +, +, +);
Self.label.backgroundColor = [Uicolor whitecolor];
[Self.view AddSubview:self.label];
_times = 0;
//self.timer = [Nstimer scheduledtimerwithtimeinterval:0.1 target:self selector: @selector (demo) Userinfo:nil Repe Ats:yes];
//Workaround 1: Add the timer to the current thread message loop and specify the mode of the message loop as
//Cnsrunloopcommonmodes (can run regardless of which mode Runloop is running)
//plus the perfect solution to this sentence
//[[nsrunloop Currentrunloop]addtimer:self.timer formode:nsrunloopcommonmodes];
//Workaround 2:
//1. Put the Nstimer in the sub-thread, but note that because the message loop from the thread is not turned on by default, there is a need to turn on the message loop of a thread
//2. Switch to the main thread to update the UI
Dispatch_async (dispatch_get_global_queue (0, 0), ^{
Self.timer = [Nstimer scheduledtimerwithtimeinterval:0.1 target:self selector: @selector (Demo) Userinfo:nil Rep Eats:yes];
[[Nsrunloop Currentrunloop] AddTimer:self.timer Formode:nsdefaultrunloopmode];
[[Nsrunloop Currentrunloop] run];
});
}
-(void) demo{
_times+=0.1;
//self.label.text = [NSString stringwithformat:@ "%f", _times];
//Queue mode (update UI on main thread)
[[Nsoperationqueue mainqueue] addoperationwithblock:^{
Self.label.text = [NSString stringwithformat:@ "%f", _times];
}];
//GCD way
/*dispatch_async (Dispatch_get_main_queue (), ^{
Self.label.text = [NSString stringwithformat:@ "%f", _times];
});*/
}
@end
The failure of Ios-scrollview and Nstimer