iPhone dev - performSelector:withObject:afterDelay或NSTimer? [英] iPhone dev -- performSelector:withObject:afterDelay or NSTimer?

查看:96
本文介绍了iPhone dev - performSelector:withObject:afterDelay或NSTimer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要重复方法调用(或消息发送,我猜适当的术语是每个 x 秒),最好使用NSTimer(NSTimer的scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:或者让方法在结尾处递归调用自身(使用performSelector:withObject:afterDelay)?后者不使用对象,但可能不太清晰/可读?另外,为了让您了解我正在做什么,它只是一个带有标签的视图,倒计时到午夜12点,当它变为0时,它会闪烁时间(00:00:00)并且永远发出哔哔声。

To repeat a method call (or message send, I guess the appropriate term is) every x seconds, is it better to use an NSTimer (NSTimer's scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:) or to have the method recursively call itself at the end (using performSelector:withObject:afterDelay)? The latter doesn't use an object, but maybe its less clear/readable? Also, just to give you an idea of what I'm doing, its just a view with a label which counts down to 12:00 midnight, and when it gets to 0, it will blink the time (00:00:00) and play a beep sound forever.

谢谢。

编辑:同样,最好的方法是什么反复播放SystemSoundID(永远)?
编辑:我最终使用它来永远播放SystemSoundID:

also, what would be the best way to repeatedly play a SystemSoundID (forever) ? I ended up using this to play the SystemSoundID forever:

// 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我猜。

Seems to work fine.. And for the label blinking I'll use an NSTimer I guess.

推荐答案

计时器更适合严格定义的间隔。如果您的函数调用本身有延迟,则会失去准确性,因为它实际上并非同步到一个时间间隔。总是有时间运行实际的方法本身也会把时间间隔排除。

A timer is more suited to a strictly defined interval. You will lose accuracy if you have your function call itself with a delay because its not really synced to a time interval. There's always the time taken to run the actual method itself as well which puts the interval out.

坚持使用NSTimer,我会说。

Stick with an NSTimer, I'd say.

这篇关于iPhone dev - performSelector:withObject:afterDelay或NSTimer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆