代码之家  ›  专栏  ›  技术社区  ›  mk12

iPhone开发-性能选择器:带对象:延迟后还是NSTimer?

  •  10
  • mk12  · 技术社区  · 16 年前

    // Utilities.h
    #import <Foundation/Foundation.h>
    #import <AudioToolbox/AudioServices.h>
    
    
    static void soundCompleted(SystemSoundID soundID, void *myself);
    
    @interface Utilities : NSObject {
    
    }
    
    + (SystemSoundID)createSystemSoundIDFromFile:(NSString *)fileName ofType:(NSString *)type;
    + (void)playAndRepeatSystemSoundID:(SystemSoundID)soundID;
    + (void)stopPlayingAndDisposeSystemSoundID;
    
    @end
    
    
    // Utilities.m
    #import "Utilities.h"
    
    
    static BOOL play;
    
    static void soundCompleted(SystemSoundID soundID, void *interval) {
        if(play) {
            [NSThread sleepForTimeInterval:(NSTimeInterval)interval];
            AudioServicesPlaySystemSound(soundID);
        } else {
            AudioServicesRemoveSystemSoundCompletion(soundID);
            AudioServicesDisposeSystemSoundID(soundID);
        }
    
    }
    
    @implementation Utilities
    
    + (SystemSoundID)createSystemSoundIDFromFile:(NSString *)fileName ofType:(NSString *)type {
        NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:type];
        SystemSoundID soundID;
    
        NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
    
        AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);
        return soundID;
    }
    
    + (void)playAndRepeatSystemSoundID:(SystemSoundID)soundID interval:(NSTimeInterval)interval {
        play = YES
        AudioServicesAddSystemSoundCompletion(soundID, NULL, NULL,
                                              soundCompleted, (void *)interval);
        AudioServicesPlaySystemSound(soundID);
    }
    
    + (void)stopPlayingAndDisposeSystemSoundID {
        play = NO
    }
    
    @end
    

    似乎工作得很好。。对于标签闪烁,我想我会使用NSTimer。

    3 回复  |  直到 16 年前
        1
  •  6
  •   Nick Bedford    16 年前

    我会说,坚持使用NSTimer。

        2
  •  2
  •   Kendall Helmstetter Gelner    16 年前

    为了对其他答案稍作补充,递归调用的情况是,调用可能需要未知的时间——比如你正在用少量数据重复调用一个web服务,直到完成。每次调用可能需要一些未知的时间,因此您可以让代码在web调用返回之前什么都不做,然后发送下一批,直到没有更多数据需要发送,并且代码不会再次调用自己。

        3
  •  1
  •   Elliot    16 年前

    由于您的应用程序依赖于时间精度(即它需要每秒执行一次),因此NSTimer会更好。方法本身需要一些时间来执行,NSTimer也可以(只要你的方法每秒调用一次,时间不到1秒)。

    要反复播放声音,您可以设置一个完成回调并在那里重放声音:

    SystemSoundID tickingSound;
    
    ...
    
    AudioServicesAddSystemSoundCompletion(tickingSound, NULL, NULL, completionCallback, (void*) self);
    
    ...
    
    static void completionCallback(SystemSoundID mySSID, void* myself) {
      NSLog(@"completionCallback");
    
      // You can use this when/if you want to remove the completion callback
      //AudioServicesRemoveSystemSoundCompletion(mySSID);
    
      // myself is the object that called set the callback, because we set it up that way above
      // Cast it to whatever object that is (e.g. MyViewController, in this case)
      [(MyViewController *)myself playSound:mySSID];
    }