使用touchesmoved时,Touchesbegan总会开火吗?

我是iPhone编程的新手。 我正试图做一个简单的窗口,一只猫做两声。当你点击猫图标时它应该做“miaau”,当你拖过窗口(笔划)时它应该是“mrrrr”。 它工作,但总是当我尝试使猫mrrrrr功能TouchesBegan火和猫也做“miaaau”。 怎么做才能使界面识别我只想要中风猫,而不是触摸它做第一个选项,“miaau”?     
已邀请:
我建议在touchesBegan方法中添加一个NSTimer,时间间隔很短(比如0.1秒):
BOOL tap_event = NO; //ivar declared in header

-(void) touchesBegan:... {
    tap_event = YES;
    [NSTimer scheduledTimerWithTimeInterval: 0.1 target: self selector: @selector(checkTap:) userInfo: nil repeats: NO];
} 

-(void) checkTap:(NSTimer*) t {
     if( tap_event ) //miauu here
     tap_event = NO;
}

-(void) touchesMoved:... {
    tap_event = NO;
     //mrrrr here
}
或者作为选项检查UIGestureRecognizers的文档     
这可能对你有所帮助 touchesBegan什么时候成为touchesMoved? touchesBegin& touchesMove Xcode Obj C问题     
Max的解决方案是正确的。它肯定会奏效。我有另一种选择,看看这种方法。 将您的播放器对象放在.h文件中,然后在viewDidLoad中分配它并在dealloc中释放。
-(void) touchesBegan:... {

    //Play the miauu sound.
}

-(void)  touchesMoved:... {
[self.yourPlayer stop];

    //Play mrrrr.
}
编辑 .h文件,
AVAudioPlayer *avPlayer1;
AVAudioPlayer *avPlayer2;
.m文件
 -(void) touchesBegan:... {
     NSString *path = [[NSBundle mainBundle] pathForResource:@"cat" ofType:@"wav"];
     avPlayer1 = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
     [avPlayer1 play];
 }

-(void) touchesMoved:... {
    [avPlayer1 stop];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"cat" ofType:@"wav"];
    avPlayer2 = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
    [avPlayer2 play];
}

-(void)dealloc
{
    [avPlayer1 release];
    [avPlayer2 release];
    [super dealloc];
}
    

要回复问题请先登录注册