gpt4 book ai didi

iphone - iOS 失去联系?

转载 作者:可可西里 更新时间:2023-11-01 03:09:40 25 4
gpt4 key购买 nike

我找不到任何内容来解释丢失的 UITouch 事件。如果您用整只手在屏幕上敲打的次数足够多,touchesBegan 的数量将不同于 touchesEnded 的数量!我认为真正了解这些孤立触摸的唯一方法是自己引用它们并跟踪它们多久没有移动。

示例代码:

int touchesStarted = 0;
int touchesFinished = 0;

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
touchesStarted += touches.count;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
touchesFinished += touches.count;
NSLog(@"%d / %d", touchesStarted, touchesFinished);
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchesEnded:touches withEvent:event];
}

最佳答案

不要忘记touchesCancelled:UIResponder reference

根据海报的更新进行编辑:

每个触摸对象都提供了它所处的阶段:

typedef enum {
UITouchPhaseBegan,
UITouchPhaseMoved,
UITouchPhaseStationary,
UITouchPhaseEnded,
UITouchPhaseCancelled,
} UITouchPhase;

我相信如果触摸在同一触摸事件集中开始和结束,-touchesBegan:withEvent: 将被调用但将包含已结束或取消的触摸 .

然后,您应该更改计数代码,使其看起来像这样:

int touchesStarted = 0;
int touchesFinished = 0;

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self customTouchHandler:touches];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[self customTouchHandler:touches];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[self customTouchHandler:touches];
}
- (void)customTouchHandler:(NSSet *)touches
{
for(UITouch* touch in touches){
if(touch.phase == UITouchPhaseBegan)
touchesStarted++;
if(touch.phase == UITouchPhaseEnded || touch.phase == UITouchPhaseCancelled)
touchesFinished++;
}
NSLog(@"%d / %d", touchesStarted, touchesFinished);
}

每个触摸事件都会经历开始和完成/取消这两个阶段,因此一旦您的手指离开屏幕,您的计数就会匹配。

关于iphone - iOS 失去联系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9320963/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com