ios – 如何在多点触控序列中忽略某些UITouch点

ios – 如何在多点触控序列中忽略某些UITouch点,第1张

概述我正在为ipad绘图应用程序,我正在为绘图提供全屏.因此,就像我们现在一样,用户可能会用手腕支撑或将手放在屏幕上.所以我的目标是让用户用他的手腕/手支撑自由地书写. 但该应用程序应该只检测手指绘图,或忽略/拒绝手腕和手触摸并删除它们 我开始研究它,我创建了一个启用了多点触控的示例项目. 以下是我的代码 - (void)touchesBegan:(NSSet *)touches withEvent: 我正在为ipad绘图应用程序,我正在为绘图提供全屏.因此,就像我们现在一样,用户可能会用手腕支撑或将手放在屏幕上.所以我的目标是让用户用他的手腕/手支撑自由地书写.

但该应用程序应该只检测手指绘图,或忽略/拒绝手腕和手触摸并删除它们

我开始研究它,我创建了一个启用了多点触控的示例项目.

以下是我的代码

- (voID)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {    mouseSwiped = NO;    for (UItouch *touch in touches)    {        Nsstring *key = [Nsstring stringWithFormat:@"%d",(int) touch];        lastPoint = [touch locationInVIEw:self.vIEw];        [touchPaths setobject:[NSValue valueWithCGPoint:lastPoint] forKey:key];    }}- (voID)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{        mouseSwiped = YES;    CGPoint lastPoint3;    for (UItouch *touch in touches)    {        Nsstring *key = [Nsstring stringWithFormat:@"%d",(int) touch];        lastPoint = [[touchPaths objectForKey:key] CGPointValue];        currentPoint1 = [touch locationInVIEw:self.vIEw];        NSLog(@"Y:%f",currentPoint1.y);        UIGraphicsBeginImageContext(self.vIEw.frame.size);        [self.tempDrawImage.image drawInRect:CGRectMake(0,self.vIEw.frame.size.wIDth,self.vIEw.frame.size.height)];        CGContextSetlineCap(UIGraphicsGetCurrentContext(),kCGlineCapRound);        CGContextSetlinewidth(UIGraphicsGetCurrentContext(),brush );        CGContextSetRGBstrokecolor(UIGraphicsGetCurrentContext(),red,green,blue,1.0);        CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModenormal);        CGContextBeginPath(UIGraphicsGetCurrentContext());        CGContextMovetoPoint(UIGraphicsGetCurrentContext(),lastPoint.x,lastPoint.y);        CGContextAddlinetoPoint(UIGraphicsGetCurrentContext(),currentPoint1.x,currentPoint1.y);        CGContextstrokePath(UIGraphicsGetCurrentContext());        self.tempDrawImage.image = UIGraphicsGetimageFromCurrentimageContext();        [self.tempDrawImage setAlpha:opacity];        UIGraphicsEndImageContext();       [touchPaths setobject:[NSValue valueWithCGPoint:currentPoint1] forKey:key];    }}

所以这适用于任何数量的触摸,但我不明白如何在绘制时拒绝那些手掌/手部触摸并且仅绘制,用户用他的手指/触笔绘制的内容.

现在,如果我画画,我得到这个东西,下面是图像

在这里,我用我的手支持绘制,你可以看到下面的“你好”,他们发现了一些奇怪的绘图.我怎么能拒绝那些接触并删除它们而只是打招呼

谢谢

兰吉特

解决方法 一种解决方案是将最顶部的水龙头存储在touchesBegan中,并仅绘制这一个.

正如您所指出的,您不应该保留UItouch实例,因此我建议使用弱引用.

这只会吸引一下.如果你想绘制多个手指的触摸,你需要另一种过滤手的方法(例如,许多绘图应用程序具有用于告诉应用程序手的姿势的用户设置,但这当然更复杂).

以下是如何做到这一点的想法:

#import <QuartzCore/QuartzCore.h>@interface TVIEwController () {    // We store a weak reference to the current touch that is tracked    // for drawing.    __weak UItouch* drawingtouch;    // This is the prevIoUs point we drawed to,or the first point the user tapped.    CGPoint touchstartPoint;}@end@interface _TDrawVIEw : UIVIEw {@public    CGLayerRef persistentLayer,tempLayer;}-(voID)commitDrawing;-(voID)discardDrawing;@end@implementation TVIEwController- (voID) loadVIEw{    self.vIEw = [[_TDrawVIEw alloc] initWithFrame:[UIScreen mainScreen].bounds];    self.vIEw.opaque = YES;    self.vIEw.multipletouchEnabled = YES;    self.vIEw.backgroundcolor = [UIcolor whitecolor];}- (voID)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {    // Start with what we currently have    UItouch* topmosttouch = self->drawingtouch;    // Find the top-most touch    for (UItouch *touch in touches) {        CGPoint lastPoint = [touch locationInVIEw:self.vIEw];        if(!topmosttouch || [topmosttouch locationInVIEw:self.vIEw].y > lastPoint.y) {            topmosttouch = touch;            touchstartPoint = lastPoint;        }    }    // A new finger became the drawing finger,discard any prevIoUs     // strokes since last touchesEnded    if(self->drawingtouch != nil && self->drawingtouch != topmosttouch) {        [(_TDrawVIEw*)self.vIEw discardDrawing];    }    self->drawingtouch = topmosttouch;}- (voID) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {    // Always commit the current stroke to the persistent layer if the user    // releases a finger. This Could need some tweaking for optimal user experIEnce.    self->drawingtouch = nil;    [(_TDrawVIEw*)self.vIEw commitDrawing];    [self.vIEw setNeedsdisplay];}- (voID)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{    const CGfloat red=0,green=0,blue=0,brush=1;    for (UItouch *touch in touches) {        // Find the touch that we track for drawing        if(touch == self->drawingtouch) {            CGPoint currentPoint = [touch locationInVIEw:self.vIEw];            // Draw stroke first in temporary layer            CGContextRef ctx = CGLayerGetContext(((_TDrawVIEw*)self.vIEw)->tempLayer);            CGContextSetlineCap(ctx,kCGlineCapRound);            CGContextSetlinewidth(ctx,brush );            CGContextSetRGBstrokecolor(ctx,1.0);            CGContextSetBlendMode(ctx,kCGBlendModenormal);            CGContextBeginPath(ctx);            CGContextMovetoPoint(ctx,touchstartPoint.x,touchstartPoint.y);            CGContextAddlinetoPoint(ctx,currentPoint.x,currentPoint.y);            CGContextstrokePath(ctx);            // Update the points so that the next line segment is drawn from where            // we left off            touchstartPoint = currentPoint;            // repaint the layer            [self.vIEw setNeedsdisplay];        }    }}@end@implementation _TDrawVIEw- (voID) finalize {    if(persistentLayer) CGLayerRelease(persistentLayer);    if(tempLayer) CGLayerRelease(tempLayer);}- (voID) drawRect:(CGRect)rect {    CGContextRef ctx = UIGraphicsGetCurrentContext();    if(!persistentLayer) persistentLayer = CGLayerCreateWithContext(ctx,self.bounds.size,nil);    if(!tempLayer) tempLayer = CGLayerCreateWithContext(ctx,nil);    // Draw the persistant drawing    CGContextDrawLayerAtPoint(ctx,CGPointMake(0,0),persistentLayer);    // Overlay with the temporary drawing    CGContextDrawLayerAtPoint(ctx,tempLayer);}- (voID)commitDrawing {    // Persist the temporary drawing    CGContextRef ctx = CGLayerGetContext(persistentLayer);    CGContextDrawLayerAtPoint(ctx,tempLayer);    [self discardDrawing];}- (voID)discardDrawing {    // Clears the temporary layer    CGContextRef ctx = CGLayerGetContext(tempLayer);    CGContextClearRect(ctx,self.bounds);    CGContextFlush(ctx);}@end

编辑:我添加了一个逻辑,如果检测到新的触摸,如果当前正在绘制任何具有更高y值的笔划,它将被删除,正如我们在评论中所讨论的那样.

覆盖是通过绘制两个CGLayers来完成的.这段代码可以针对性能进行大量优化,应该将其视为一个插图而不是生产就绪代码.

总结

以上是内存溢出为你收集整理的ios – 如何在多点触控序列中忽略某些UITouch点全部内容,希望文章能够帮你解决ios – 如何在多点触控序列中忽略某些UITouch点所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1065802.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存