使用touchesmoved 时Touchesbegan 总是会触发?

发布于 2024-10-20 13:32:00 字数 203 浏览 6 评论 0原文

我是 iPhone 编程新手。 我正在尝试制作一个简单的窗口,其中一只猫发出两种声音。当您单击猫图标时,它应该发出“miaau”,当您在窗口上拖动(描边)时,它应该发出“mrrrr”。 它有效,但总是当我尝试使 cat mrrrrr 函数 TouchesBegan 起火时,猫也会做“miaaau”。

如何使界面识别出我只想抚摸猫,而不是触摸它来执行第一个选项“miaau”?

I'm new to iPhone programing.
I'm trying to do a simple window with a cat doing two sounds. When you click at the cat icon it should do "miaau", and when wou drag over window (stroke) it should make "mrrrr".
It works, but always when I try to make cat mrrrrr function TouchesBegan fires and cat also do "miaaau".

What to do to make the interface recognize I want only stroke cat, not to touch it for doing the first option, "miaau"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

若沐 2024-10-27 13:32:00

我建议以较小的时间间隔(例如 0.1 秒)将 NSTimer 添加到 TouchBegan 方法:

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 的选项检查文档

I suggest to add a NSTimer to touchesBegan method with small time interval (say 0.1 sec):

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
}

Or as an option check docs for UIGestureRecognizers

深空失忆 2024-10-27 13:32:00

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

Max' solution is correct. It will definitely work. I am having an alternative, see this approach.

Make your player object in an .h file then allocate it in viewDidLoad and release in dealloc.

-(void) touchesBegan:... {

    //Play the miauu sound.
}

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

    //Play mrrrr.
}

Edit

.h file,

AVAudioPlayer *avPlayer1;
AVAudioPlayer *avPlayer2;

.m file

 -(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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文