以编程方式使用NSTimer更新UILabel文本

时间:2014-12-12 12:31:18

标签: ios xcode uilabel nstimer

我正在尝试每隔1秒实现一次影响UILabel文本的方法,这将是一个提示文本,如"您可以跳过5,4,3,2,1跳过广告>>"

我的方法工作正常,除了第二和第三秒计时器调用方法,每秒超过1次。我正在使用UIApplication sharedapplication.keywindows在全屏MPMoviePlayerController上显示当前UI。这是我的方法:

-(void)closeButtonForVideoPlayer
{

    myButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width-30, 12, 25, 25)];

    window = [UIApplication sharedApplication].keyWindow;

    if (!window)
    {
        window = [[UIApplication sharedApplication].windows objectAtIndex:0];
    }

    [myButton addTarget:self action:@selector(videoFinished:) forControlEvents:UIControlEventTouchUpInside];

    [myButton setImage:self.closeButton forState:UIControlStateNormal];

    labelReminder = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width-130, 12, 130, 35)];

    labelReminder.textColor = [UIColor whiteColor];

    labelReminder.backgroundColor = [UIColor blackColor];

    self.timderReminder = [NSTimer scheduledTimerWithTimeInterval:1.0
                                                       target: self
                                                         selector:@selector(reminderForVideoPlayer:)
                                                         userInfo: @{@"boolType":@"YES"} repeats:NO];


}

提醒方法:

-(void)reminderForVideoPlayer:(NSTimer *)userInfo
{

    NSString *isVideoAdvertisement = [userInfo.userInfo objectForKey:@"boolType"];

    NSLog(@"NSTimer info = %@",userInfo.userInfo);


    if([isVideoAdvertisement isEqualToString:@"YES"])
    {
        if(remind >= 5)
        {
            [self.timderReminder invalidate];

            remind = 0;

            NSLog(@"reklam geçildi window");

            [labelReminder removeFromSuperview];

            labelReminder = nil;

            [labelReminder setHidden:YES];

            [window addSubview:myButton];


        } else {

            [window addSubview:labelReminder];

            [labelReminder setText:[NSString stringWithFormat:@"Reklami geç: %d",5-remind]];

            NSLog(@"reminder window = %d",remind);

            remind ++;

            self.timderReminder = [NSTimer scheduledTimerWithTimeInterval:1.0
                                                                   target: self
                                                                 selector:@selector(reminderForVideoPlayer:)
                                                                 userInfo: @{@"boolType":@"YES"} repeats:NO];

        }
}

我错误的哪一部分?以及我如何解决这个问题?

最诚挚的问候,

Onder OZCAN

3 个答案:

答案 0 :(得分:1)

尝试此控件,它将满足您的要求

https://github.com/mineschan/MZTimerLabel

答案 1 :(得分:1)

在viewDidLoad

self.timderReminder = [NSTimer scheduledTimerWithTimeInterval:1.0
                                                   target: self
                                                     selector:@selector(reminderForVideoPlayer:)
                                                     userInfo: @{@"boolType":@"YES"} repeats:YES];

-(void)reminderForVideoPlayer:(NSTimer *)userInfo
{
    NSString *isVideoAdvertisement = [userInfo.userInfo objectForKey:@"boolType"];
    NSLog(@"NSTimer info = %@",userInfo.userInfo);

    if([isVideoAdvertisement isEqualToString:@"YES"])
    {
        if(remind < 5)
        {
            [labelReminder setText:[NSString stringWithFormat:@"Reklami geç: %d",5-remind]];
            NSLog(@"reminder window = %d",remind);

            [labelReminder setHidden:NO]; 
        } else {
            [labelReminder setHidden:YES];

            // [window addSubview:myButton]; //add this button in view did load only over here use setHidden method
            [myButton setHidden: NO];
        }
}

尝试使用此代码。

答案 2 :(得分:0)

将NSTimer的repeats参数更改为YES。而且每次触发时你都不必再创建一个计时器。当你的计数器达到5时,只需invalidate你的计时器。