iOS 9兼容版NSTimer scheduledTimerWithTimeInterval:重复:阻止:?

时间:2017-08-30 16:53:07

标签: ios objective-c

我在iOS 10中使用以下代码,但是当我在iOS 9中运行它时崩溃了。我不认为NSTimer scheduledTimerWithTimeInterval:repeats:block:支持iOS 9.如何实现可在iOS 8 - 10中运行的计时器?

static NSTimer* timer = nil;

- (void)documentInteractionControllerWillBeginPreview:(UIDocumentInteractionController *)controller
{
    timer = [NSTimer scheduledTimerWithTimeInterval:0.1 repeats:YES block:^(NSTimer * _Nonnull timer) {
        [[UIApplication sharedApplication] setStatusBarHidden:YES];
    }];
}

-(void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller
{
    [timer invalidate];
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
}

2 个答案:

答案 0 :(得分:5)

您似乎正在使用iOS10中引入的功能。您在链接的页面下方是iOS 2中的一个功能。

https://developer.apple.com/documentation/foundation/nstimer/1408356-timerwithtimeinterval?language=objc

请改用它。

答案 1 :(得分:3)

这解决了它:

static NSTimer* timer = nil;

- (void)documentInteractionControllerWillBeginPreview:(UIDocumentInteractionController *)controller
{
    timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(hideStatusBar) userInfo:nil repeats:YES];
}

-(void)hideStatusBar
{
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
}

-(void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller
{
    [timer invalidate];
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
}