English 中文(简体)
使用NSTimer,调用两个功能,名称相同,参数不同,都使用定期时间时间时间间隔
原标题:Using NSTimer, calling two functions with the same name, different parameters, both use scheduledTimerWithTimeInterval

我试图用AVAudioPlayer作为帮手 来写我自己的淡入和淡出

我的问题是:我有两个方法定义,名称相同,但一个用一字,另一个不使用参数。我能否告诉NSTimer该给谁打电话?不能真正理解文件:

https://developer.apill.com/library/mac/#document/Cocoa/Reference/Foundation/Classeses/nstimer_Class/Reference/NSTimerr.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];
}
}

-(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];
}
}
最佳回答

这些名称实际上有distic 名称。 冒号很重要, 所以第二个方法的名称( 因此对 的参数) 是 < code> stop withoutFadeOut: .*。

要创建计时器, 您想要 :

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

然而,这一方法是不正确的, 因为 NSTimerr itself 传到其操作方法; 您不可能在 < / em > 中 < em > pass in a 任意对象。 这是 < code > userInfo: 参数的目的。 您可以对计时器排序 < em>attach some 对象, 并在动作方法中使用 < code>userInfo 方法检索它, 例如 :

- (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];
    }

(请注意,这意味着你的第一个方法并不真正正确 NSTimer 动作方法,因为它没有 NSTimer 参数。 )


* 编译者不会允许您在一个类别中声明或定义两个名称相同的方法,而参数类型并不算作选择器的一部分,因此试图创建 -( 避免) dumbulthwaite: (int) crencestance; -( eve) dumbulthwaite: (NSString *) Jinxopotamus; 在一个类别中无效 。

问题回答

这就是我想出来的 谢谢乔希

-(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];

}




相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...