2013-07-05 65 views
2

我想了解根據指定的空閒時間使對象失效的最佳方法。我有一個操縱桿對象,從下面的joystickAdded方法實例化時,會自動啓動一個NSTimer爲該實例:可可對象空閒計時器

操縱桿

idleTimer = [NSTimer scheduledTimerWithTimeInterval:300 target:self selector:@selector(invalidate) userInfo:nil repeats:YES]; 

這工作得很好,但我的操縱桿陣列犯規得到清理,因爲該方法當閒置時應該調用joystickRemoved,但我不知道如何調用它,或者如果NSTimer是最佳方式。

JoystickController

void joystickAdded(void *inContext, IOReturn inResult, void *inSender, IOHIDDeviceRef device) { 
    JoystickController *self = (__bridge JoystickController*)inContext; 
    IOHIDDeviceOpen(device, kIOHIDOptionsTypeNone); 

    // Filter events for joystickAction 
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; 
    [dict setObject:[NSNumber numberWithInt:kIOHIDElementTypeInput_Button] forKey:(NSString*)CFSTR(kIOHIDElementTypeKey)]; 
    IOHIDDeviceSetInputValueMatching(device, (__bridge CFDictionaryRef)(dict)); 

    // Register callback for action event to find the joystick easier 
    IOHIDDeviceRegisterInputValueCallback(device, joystickAction, (__bridge void*)self); 
    Joystick *js = [[Joystick alloc] initWithDevice:device]; 
    [[self joysticks] addObject:js]; 
} 

void joystickRemoved(void *inContext, IOReturn inResult, void *inSender, IOHIDDeviceRef device) { 
    // Find joystick 
    JoystickController *self = (__bridge JoystickController*)inContext; 
    Joystick *js = [self findJoystickByRef:device]; 

    if(!js) { 
     NSLog(@"Warning: No joysticks to remove"); 
     return; 
    } 

    [[self joysticks] removeObject:js]; 
    [js invalidate]; 
} 

void joystickAction(void *inContext, IOReturn inResult, void *inSender, IOHIDValueRef value) { 
    long buttonState; 

    // Find joystick 
    JoystickController *self = (__bridge JoystickController*)inContext; 
    IOHIDDeviceRef device = IOHIDQueueGetDevice((IOHIDQueueRef) inSender); 
    Joystick *js = [self findJoystickByRef:device]; 

    // Get button state 
    buttonState = IOHIDValueGetIntegerValue(value); 

    switch (buttonState) { 
     // Button pressed 
     case 1: { 
      // Reset joystick idle timer 
      [[js idleTimer] setFireDate:[NSDate dateWithTimeIntervalSinceNow:300]]; 
      break; 
     } 
     // Button released 
     case 0: 
      break; 
    } 
} 
+0

的'NSTimer'將調用'invalidate'你的'JoystickController'。那個方法在哪裏? – Danilo

+0

'invalidate'在Joystick類中,因此,'[js invalidate]' – nmajin

+0

啊定時器在'操縱桿'而不是控制器,我的壞。 – Danilo

回答

0

你可以使用委託模式。

1)制定協議JoystickDelegate,聲明方法- (void)joystickInvalidated:(Joystick *)joystick

2)JoystickController應該執行JoystickDelegate。實施該方法從陣列中刪除joystick

3)Joystick界面以及在joystickAddedJoystick每個初始化分配上js.delegateself最後做一個弱屬性調用delegate

4)現在,每當invalidate被調用,只需調用delegateselfJoystick

[self.delegate joystickInvalidated:self]; 
+0

感謝您的答覆:) 雖然這工作,我辦將操縱桿手動現在從藍牙除去有一個例外: ' - (無效){無效// 從操縱桿刪除操縱桿陣列 [self.delegate removeFromJoysticks:自]; IOHIDDeviceClose(device,kIOHIDOptionsTypeNone); NSLog(@「Removed HID device:%@」,productName); if(bluetoothDevice){bluetoothDevice closeConnection]; NSLog(@「Removed Bluetooth device:%@」,[bluetoothDevice nameOrAddress]); } }' IOHIDDeviceClose:線程1:EXC_BAD_ACCESS(碼= 1,地址=爲0x0) – nmajin

+0

似乎計時器是造成這一點,我不得不使用'[idleTimer無效]'中內'Joystick'的'invalidate'方法,因爲如果手動移除操縱桿,定時器仍有可能觸發...感謝您的幫助:D – nmajin

+0

很高興爲您提供幫助,請將其標記爲已回答 – Danilo