NSTimer无法按预期运行

时间:2013-12-19 04:38:19

标签: objective-c macos nstimer

我查看了许多与NSTimer问题相关的不同帖子,我尝试了很多不同的组合,让它按照我想要的方式工作,但是我仍然无法让它正常工作。

我对计时器有两个要求,一个是重复计时器,每隔10秒从Web服务中提取数据。这似乎工作正常。第二个要求是,无论何时在接口中更新值,都会创建一个非重复计时器,该计时器将在值更改后0.3秒更改接口中值的文本颜色。

我似乎只是第一次看到文字颜色发生变化,随后也不会改变......这让我疯了。

这是一些代码......

在界面中我定义了一个属性:

@property (strong) NSTimer *colorTimer;

在实现中,只要需要在接口中更新值,就会调用此函数:

- (void)setBtcValue:(float)value
{
    bool moveIsPositive = (value > btcValue) ? true : false;
    bool moveIsNeutral = (value == btcValue) ? true : false;
    btcValue = value;
    if(moveIsPositive && !moveIsNeutral)
    {
        [uiValueTextField setTextColor:[NSColor greenColor]];
        self.colorTimer = [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(resetTextColor) userInfo:nil repeats:YES];

    } else if(!moveIsPositive && !moveIsNeutral)
    {
        [uiValueTextField setTextColor:[NSColor redColor]];
        self.colorTimer = [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(resetTextColor) userInfo:nil repeats:YES];
    }
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    [formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
    [uiValueTextField setStringValue: [formatter stringFromNumber:[NSNumber numberWithFloat:btcValue]]];
    [uiValueTextField sizeToFit];
    NSRect newFrame = NSMakeRect(0, 0, uiValueTextField.bounds.size.width+20, self.view.bounds.size.height);

    [self.view setFrame:newFrame];
}

定时器关闭时触发的功能如下:

- (void) resetTextColor
{
    NSLog(@"Timer Fired");
    [uiValueTextField setTextColor:[NSColor blackColor]];
    [self.colorTimer invalidate];
    self.colorTimer = nil;
}

我知道让计时器重复然后手动使其无效是没有意义的,但正如我所说,我尝试使用重复设置为NO,使用局部变量而不是属性,使用弱,强,赋值,保留......行为总是一样......第一次发射,从那之后发射。

非常感谢任何帮助......我是一名学习Objective-C的C#开发人员,因此对我来说一切都还是很陌生!提前谢谢。

UPDATE:这是设置主计时器的AppDelegate,它是setBtcValue的调用者:

#import "LBAppDelegate.h"
#import "LBBitStampCommunicator.h"

@implementation LBAppDelegate
@synthesize statusItem;
@synthesize statusItemViewController;
@synthesize bitStampManager;
@synthesize dataTimer;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
    [self activateStatusMenu];
    bitStampManager = [[LBBitStampManager alloc] init];
    bitStampManager.communicator = [[LBBitStampCommunicator alloc] init];
    bitStampManager.communicator.delegate = bitStampManager;
    bitStampManager.delegate = self;

    dataTimer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(fetchData) userInfo:nil repeats:YES];
}

- (void)activateStatusMenu {
    NSStatusBar *bar = [NSStatusBar systemStatusBar];
    statusItem = [bar statusItemWithLength:NSVariableStatusItemLength];
    statusItemViewController = [[LBStatusItemViewController alloc]init];
    [statusItem setView:statusItemViewController.view];
    [statusItemViewController setBtcValue: 8888.88f];
}

- (void)didReceiveData:(LBTicker *)data {
    [statusItemViewController setBtcValue:[data.last floatValue]];
}

- (void)fetchingDataFailedWithError:(NSError *)error {
    [statusItemViewController setBtcValue:0.00f];
}

- (void)fetchData {
    [bitStampManager.communicator getData];
}

@end

1 个答案:

答案 0 :(得分:0)

你考虑过而不是试图产生一个计时器;以0.3秒的淡入效果制作动画?这样你只需要听取通知并启动动画,让系统完成剩下的工作。

相关问题