在iPhone上不使用事件查询当前屏幕上的触摸次数

发布于 2024-08-29 19:26:31 字数 339 浏览 4 评论 0原文

我有一个应用程序,当用户触摸 uiview 时开始播放声音,并在用户在屏幕上滑动手指时更改为不同的音调。当用户抬起手指时声音停止。

我为此使用touchesBegan、Moved 和Ended 事件。

我的问题是触摸结束(和/或取消)有时无法正确触发,即使手指从屏幕上抬起后声音也会继续播放。

因此,作为一种解决方法,我想实现一个计时器来检查屏幕上的触摸次数,如果为零,它将检查并停止音频播放器(如果正在播放)。

我一直在寻找一些可以获取触摸次数的代码,例如

UITouch *touches=[self getAllTouchesonScreen];

或其他什么:)

I have an application that starts playing a sound when user touches the uiview and changing to different tones as the user slides the finger on the screen. The sound stops when the user lifts the finger.

I am using the touchesBegan, Moved and Ended Events for this.

My problem is touches Ended (and/or cancelled) is sometimes not fired properly and the sound keeps playing even after the finger is lifted from screen.

So as a workaround I would like to implement a timer that would check for the number of touches on the screen and if it is zero it will check and stop the audioplayer if playing.

I have been searching for some code that could get me the number of touches like

UITouch *touches=[self getAllTouchesonScreen];

or something :)

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

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

发布评论

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

评论(3

沫离伤花 2024-09-05 19:26:31
NSSet *allTouches = [event allTouches];
for(GLuint index = 0; index < [allTouches count]; ++index)
{
    UITouch *touch = [[allTouches allObjects] objectAtIndex:index];
    if([touch phase] != UITouchPhaseCancelled)
    {
        CGPoint touchPoint = [touch locationInView:self];
        //do something with touchPoint that has state [touch phase]
    }
}

您可以在所有触摸事件函数(touchesBegan、touchesEnded、touchesMoved)中使用此代码,并且可以对触摸进行计数并了解其状态。

NSSet *allTouches = [event allTouches];
for(GLuint index = 0; index < [allTouches count]; ++index)
{
    UITouch *touch = [[allTouches allObjects] objectAtIndex:index];
    if([touch phase] != UITouchPhaseCancelled)
    {
        CGPoint touchPoint = [touch locationInView:self];
        //do something with touchPoint that has state [touch phase]
    }
}

You can use this code in all touch event functions(touchesBegan, touchesEnded, touchesMoved) and you can count touches and know their states.

别想她 2024-09-05 19:26:31

触摸结束事件有时不会被触发。

我尝试过为触摸取消和触摸结束事件设置断点,但有时不会命中。

尝试 Apple 网站上的 GLPaint 示例程序,并尝试在结束时使用 NSLog,在屏幕上快速绘图并快速抬起手指,就像将手指扔离屏幕一样。

你会明白我的意思的。我当前的解决方案涉及加速度计:)

提示:我用它来查找所有事件:(void)sendEvent:(UIEvent *)event

Touches Ended Event sometimes does not get fired.

I have tried setting break points for touches cancelled and touches ended events and it sometimes doesnt hit.

try the GLPaint sample program from Apple website and try an NSLog in toches ended and do some fast drawings on screen and lift the finger fast, like throwing the finger off screen.

You will know what I mean. My current solution for this involves accelerometer :)

Hint : I use this to find all events : (void)sendEvent:(UIEvent *)event

黑凤梨 2024-09-05 19:26:31

不要忘记touchesCanceled。添加此函数/方法并在触摸结束时对其进行 NSLog 我认为您会发现一些丢失的触摸。

如果您正在寻找点击计数 - 多次点击同一位置而无需过多移动手指 - 您可以通过以下方式获得:

-(void)iPhoneTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

UITouch *touch = [touches anyObject];
CGPoint touchPosition = [touch locationInView:self];
lastTouchTime = [touch timestamp];
myTouchCount = [touch tapCount];

但我也使用 TouchMoved 手动执行此操作,以查看其移动的距离并取消加倍的能力如果任何点击移动得太远,则 /三次点击,只要处于有效的点击状态,就会计算点击次数。

Don't forget about touchesCanceled. Add this function/method and NSLog it when a touch ends up there I think you'll find some missing touches.

If you're looking for a tap count - tapping in the same place more than once without moving the finger much - you can get it via:

-(void)iPhoneTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

UITouch *touch = [touches anyObject];
CGPoint touchPosition = [touch locationInView:self];
lastTouchTime = [touch timestamp];
myTouchCount = [touch tapCount];

But I also do it manually using touchesMoved to see how FAR it moved and canceling the ability to double/triple tap if any of the taps moved too far, and counting taps as long as you're in a valid tap state.

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