如何在多点触控序列中有效地处理 UITouch

Posted

技术标签:

【中文标题】如何在多点触控序列中有效地处理 UITouch【英文标题】:How to effectively process UITouches in a multitouch sequence 【发布时间】:2014-02-22 09:18:14 【问题描述】:

我在写作时使用多点触控,所以基本上我正在做的是,我正在用手支持写作,因为通常,它是用户写作的方式,我点击了这个链接How to ignore certain UITouch Points in multitouch sequence

单点触控一切正常,但当我用手触摸屏幕(即多个 UItouches)书写时,它们会出现问题 下面是我的代码

在触摸开始时,我遍历所有触摸,找到最高 y 位置的触摸,下面是我的代码

下面是我的代码

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

    UITouch* topmostTouch = self.trackingTouch;
    for (UITouch *touch in touches)
    
        ctr = 0;

        touchStartPoint1 = [touch locationInView:self];


        if(!topmostTouch || [topmostTouch locationInView:self].y > touchStartPoint1.y)
        
            topmostTouch = touch;
            pts[0] = touchStartPoint1;
        
       

    self.trackingTouch = topmostTouch;

在touches move.中,我只会拿self.trackingTouch,这是我在touches Began中找到的

我的触摸移动了下面的代码

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
 
    if(self.trackingTouch== nil)
    
        return;
    

    CGPoint p = [self.trackingTouch locationInView:self];
    ctr++;
    pts[ctr] = p;

    if (ctr == 4)
    
        pts[3] = midPoint(pts[2], pts[4]);

        self.currentPath = [[DrawingPath alloc] init];

        [self.currentPath setPathColor:self.lineColor];
        self.currentPath.pathWidth = [NSString stringWithFormat:@"%f",self.lineWidth];


        [self.currentPath.path moveToPoint:pts[0]];
        [self.currentPath.path addCurveToPoint:pts[3] controlPoint1:pts[1] controlPoint2:pts[2]];

        [self setNeedsDisplay];


        pts[0] = pts[3];
        pts[1] = pts[4];
        ctr = 1;
    

这里分别是单点触控和多点触控的书写图像供您参考

您可以看到,当我单点书写时,我的书写是平滑的,曲线是平滑的,但是当我用手搁置书写时,我的曲线会出现锯齿状,正如您在第二张图片中看到的那样。

朋友们,请帮帮我

【问题讨论】:

我认为,跟踪触摸出了点问题,这就是它搞砸的原因 也许您可以尝试检测用户静止手的位置,并禁用该区域的触摸?一旦用户开始绘图,为什么不直接忽略touchesEnded:touchesCancelled: 之前不在该触摸附近的任何触摸? 您好@AaronBrager,感谢您的回复,如您所见,我已经找到了跟踪触摸并忽略了基于更高值 y 的其余触摸,触摸开始。在 touchesEnded: 和 touchesCancelled: 之前忽略近距离触摸是不是同样的意思,如果我错了,请纠正我。 【参考方案1】:

假设绘图代码都是相同的,区别只是处理额外触摸的额外开销。循环处理这些,循环绘制,一切至少翻了一番。所以当你处理触摸然后在手指 #2 上绘图时,在现实世界中手指 #1 等仍在/仍在移动......请注意UIKit 只能在每个 runLoop 结束时绘制。我不认为有什么办法可以解决这个问题,正如我之前告诉过你的,我怀疑有很多人会重视多点触控的能力,尽管这可能是我有限的、英语、澳大利亚的观点它。祝你好运

【讨论】:

您好@Jef,感谢您的回复,您说得对,我们需要智能处理这些额外的触摸,用户将手放在屏幕上书写,我们不能指望用户在上面书写屏幕上没有休息他的手,因为他/她通常会记笔记,所以在我看来,这是一个需要解决的重要问题。 你好@Jef,我认为问题出在 touchesBegan: 函数中。怎么说? 你好@Jef,请帮我解决这个问题***.com/questions/22033228/eraser-the-drawing-in-ios【参考方案2】:

我终于解决了我的问题,

这里是代码

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

    UITouch* topmostTouch = self.trackingTouch;
    for (UITouch *touch in touches)
    


        touchStartPoint1 = [touch locationInView:self];


        if(!topmostTouch || [topmostTouch locationInView:self].y > touchStartPoint1.y)
        
            ctr = 0;
            topmostTouch = touch;
            pts[0] = touchStartPoint1;
        
       

    self.trackingTouch = topmostTouch;

问题很简单,设置 ctr = 0 ,在check里面,像这样..

【讨论】:

以上是关于如何在多点触控序列中有效地处理 UITouch的主要内容,如果未能解决你的问题,请参考以下文章

Android多点触控技术实战,自由地对图片进行缩放和移动

U3D多点触控框架实例(下)

Linux 中的多点触控

多点触控互动软件-一键无线飞屏展示系统-飞屏甩屏互动软件

如何在 UIPangestureRecognizer 上禁用多点触控?

多点触控与多鼠标支持