The failure of Ios-scrollview and Nstimer

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.