简体   繁体   English

使用NSTimer,以相同的名称,不同的参数调用两个函数,都使用scheduleTimerWithTimeInterval

[英]Using NSTimer, calling two functions with the same name, different parameters, both use scheduledTimerWithTimeInterval

I'm trying to write my own fade-ins and fade-outs using the AVAudioPlayer as a helper. 我正在尝试使用AVAudioPlayer作为助手编写自己的淡入和淡出。

My problem is this: I have two method definitions with the same name, but one takes an int and the other takes no parameters. 我的问题是:我有两个名称相同的方法定义,但一个方法使用int,另一个方法则不使用参数。 Is there a way for me to tell NSTimer which one to call? 我有办法告诉NSTimer呼叫哪一个吗? Couldn't really make sense of the documentation: 无法真正理解文档:

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nstimer_Class/Reference/NSTimer.html https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nstimer_Class/Reference/NSTimer.html

-(void) stopWithFadeOut 
{
if (_player.volume > 0.1) {
    [self adjustVolume:-.1];
    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(stopWithFadeOut) userInfo:NULL repeats:NO];
}
else {
    [self stop];
}
}

and

-(void) stopWithFadeOut:(NSString *)speed 
{
int incr = [speed intValue];
if (_player.volume > 0.1) {
    [self adjustVolume:-incr];
    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(stopWithFadeOut) userInfo:NULL repeats:NO];
}
else {
    [self stop];
}
}

Those actually have different names. 那些实际上有不同的名称。 The colon is significant, so the name (and hence the argument to @selector() ) of the second method is stopWithFadeOut: .* 冒号是有效的,因此第二个方法的名称(以及@selector()的参数)为stopWithFadeOut: 。*

To create the timer, then, you want: 要创建计时器,您需要:

[NSTimer scheduledTimerWithTimeInterval:0.1 
                                 target:self 
                               selector:@selector(stopWithFadeOut:) 
                               userInfo:NULL                   //^ Note! Colon!
                                repeats:NO];

However, this method is incorrect, because an NSTimer passes itself to its action method; 但是,此方法是不正确的,因为NSTimer会将自身传递给其action方法。 it's not possible for you to pass in an arbitrary object. 您不可能传递任意对象。 This is what the userInfo: parameter is for. 这就是userInfo:参数的作用。 You can sort of attach some object to the timer, and retrieve it inside the action method using the userInfo method, like so: 您可以一些对象附加到计时器,然后使用userInfo方法在action方法内检索它,如下所示:

- (void)stopWithFadeOut: (NSTimer *)tim 
{
    NSString * speed = [tim userInfo];
    int incr = [speed intValue];
    if (_player.volume > 0.1) {
        [self adjustVolume:-incr];
        [NSTimer scheduledTimerWithTimeInterval:0.1 
                                         target:self 
                                       selector:@selector(stopWithFadeOut:) 
                                       userInfo:speed
                                        repeats:NO];
    }

(Also note that this means your first method isn't really a correct NSTimer action method, because it doesn't have the NSTimer parameter.) (还要注意,这意味着您的第一个方法实际上不是正确的NSTimer操作方法,因为它没有NSTimer参数。)


*The compiler wouldn't have let you declare or define two methods with the same name in one class, and the parameter type doesn't count as part of the selector, so trying to create -(void)dumblethwaite:(int)circumstance; *编译器不会让您在一个类中声明或定义两个具有相同名称的方法,并且参数类型不计入选择器的一部分,因此请尝试创建-(void)dumblethwaite:(int)circumstance; and -(void)dumblethwaite:(NSString *)jinxopotamus; -(void)dumblethwaite:(NSString *)jinxopotamus; in one class doesn't work. 一堂课是行不通的。

So this is what I came up with. 这就是我想出的。 Thanks Josh. 谢谢乔希。

-(void) pauseWithFadeOut
{
NSNumber *incr = [[NSNumber alloc] initWithFloat:0.1];
while (_player.volume > 0.1) {
    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(fadeOut) userInfo:incr repeats:NO];
}
}
-(void)fadeOut: (NSTimer*) deleg
{   
int incr = (int) [deleg userInfo];
if (_player.volume > 0.1) {
    [self adjustVolume:-incr];
}

} 
-(void) pauseWithFadeOut:(NSString *)speed
{
NSNumber *incr = [[NSNumber alloc] initWithFloat:[speed floatValue]];
while (_player.volume > 0.1) {
    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(fadeOut) userInfo:incr repeats:NO];
}

[self pause];

}
-(void) stopWithFadeOut
{
NSNumber *incr = [[NSNumber alloc] initWithFloat:0.1];
while (_player.volume > 0.1) {
    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(fadeOut) userInfo:incr repeats:NO];
}
}
-(void) stopWithFadeOut:(NSString *)speed
{
NSNumber *incr = [[NSNumber alloc] initWithFloat:[speed floatValue]];
while (_player.volume > 0.1) {
    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(fadeOut) userInfo:incr repeats:NO];
}

[self stop];

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM