开发者

NSTimer freezes while touching UIScrollview

I'm programming a midi based app. To get an exact midi clock timing I use NSTimer. But while scrolling/touching a subview, NSTimer freeze. I've tried to run the Timer in Subview, but same problem. Has anyone an idea?

Here's the code of my ViewController:

-(void)timerPlay{

    midiclock = [NSTimer scheduledTimerWithTimeInterval:0.5/24 target:self selector:@selector(clocksignal)  userInfo:nil repeats:YES];
}

-(void)clocksignal{
    beatClock ++;

    if (beatClock == 1){
        const UInt8 clock[2] = { 0xF8, 0xF9};
        [midi sendBytes:clock size:2];
    }

    if (beatClock == 2){
        const UInt8 clock[2] = { 0xF8, 0xF9};
    开发者_运维问答    [midi sendBytes:clock size:2];
    }

    .......
    if (beatClock == 24){

        const UInt8 clock[2] = { 0xF8, 0xF9};

        [midi sendBytes:clock size:2];
        beatClock = 1;

    }
}


- (void)viewDidLoad {
    SubView = [[SubView alloc] initWithNibName:@"SubView" bundle:nil];
    [scrollView addSubview:matrixView.view];
    ......
}


I think this is because your timer is performed on Main Thread, such as the UI stuff, so your Timer has to wait for the Main Thread become "idle" to perform things on it. Since you're not updating UI, you're not required to perform your code on Main Thread.

One solution is to create a new Thread (NSThread detachNewThread) and do a loop in here, or to use GCD, instead of using a NSTimer

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜