【问题标题】:Track touch points in Multitouch在多点触控中跟踪触摸点
【发布时间】:2014-01-12 17:50:06
【问题描述】:

我正在做一个绘图项目,我想支持多点触控,我已经浏览了在线文档,其中建议跟踪触摸点,我做到了,但我没有得到想要的行为。我没有得到直线。

下面是我的代码

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    red = 0.0/255.0;
    green = 0.0/255.0;
    blue = 0.0/255.0;
    brush = 10.0;
    opacity = 1.0;

     self.view.multipleTouchEnabled = YES;

     touchPaths = [NSMutableDictionary dictionary];
}

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


    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
{

    for (UITouch *touch in touches)
    {
        NSString *key = [NSString stringWithFormat:@"%d", (int) touch];

        lastPoint = [[touchPaths objectForKey:key] CGPointValue];


        CGPoint currentPoint1 = [touch locationInView:self.view];

        UIGraphicsBeginImageContext(self.view.frame.size);
        [self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint1.x, currentPoint1.y);
        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush );
        CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);
        CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);

        CGContextStrokePath(UIGraphicsGetCurrentContext());
        self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext();
        [self.tempDrawImage setAlpha:opacity];
        UIGraphicsEndImageContext();

        lastPoint = currentPoint1;

    }
}

但是当我使用这段代码绘图时,我明白了。

所以朋友们,请帮帮我,我做错了什么。

问候 兰吉特

【问题讨论】:

    标签: ios uikit core-graphics multi-touch cgcontext


    【解决方案1】:

    我总是尽可能地尝试使用手势识别器。这里我使用UIPanGestureRecognizer

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        [self.view addGestureRecognizer:[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(didPan:)]];
    }
    
    - (void)didPan:(UIPanGestureRecognizer*)panGesture {
        for (NSUInteger touchIndex = 0; touchIndex < panGesture.numberOfTouches; touchIndex++) {
            // touchIndex is basically the "source" (the finger) from which the point comes from
            CGPoint p = [panGesture locationOfTouch:touchIndex inView:self.view];
            [self drawAtPoint:p withIndex:touchIndex];
        }
    }
    
    
    - (void)drawAtPoint:(CGPoint)point withIndex:(NSUInteger)index{
        UIView *smallPoint = [[UIView alloc] initWithFrame:CGRectMake(point.x, point.y, 3, 3)];
        [smallPoint setBackgroundColor:[self colorForIndex:index]];
        [self.view addSubview:smallPoint];
    }
    
    - (UIColor*)colorForIndex:(NSUInteger)index {
        switch (index) {
            case 0: return [UIColor redColor];
            case 1: return [UIColor orangeColor];
            case 2: return [UIColor yellowColor];
            case 3: return [UIColor blueColor];
            case 4: return [UIColor greenColor];
        }
        return [UIColor clearColor];
    }
    

    我没有画贝塞尔路径,但是如果你把它放到一个空的 ViewController 中并运行它,你会看到当多点触摸屏幕时,每根手指都会绘制不同的颜色。

    因此,如果您考虑到 touchIndex,基本上您可以跟踪不同手指的不同路径。

    假设您使用两根手指进行绘图:您将拥有panGesture.numberOfTouches == 2touchIndex 0 将代表第一根手指,touchIndex 1 将代表第二根手指。您可以将不同数组中的点累积起来,并将这些点添加到它们对应的路径中。

    【讨论】:

    • 不错,这很聪明...(除了为懒惰的绘图分配一个新的 uiview :D )非常聪明
    【解决方案2】:

    您没有正确填充touchPaths。尝试在每次绘图后设置它,如下所示:

    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {
    
        for (UITouch *touch in touches)
        {
            NSString *key = [NSString stringWithFormat:@"%d", (int) touch];
    
            CGPoint lastPoint = [[touchPaths objectForKey:key] CGPointValue];
    
    
            CGPoint currentPoint1 = [touch locationInView:self.view];
    
            UIGraphicsBeginImageContext(self.view.frame.size);
            [self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
            CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
            CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint1.x, currentPoint1.y);
            CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
            CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush );
            CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);
            CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);
    
            CGContextStrokePath(UIGraphicsGetCurrentContext());
            self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext();
            [self.tempDrawImage setAlpha:opacity];
            UIGraphicsEndImageContext();
            // I changed your code here
            [touchPaths setObject:[NSValue valueWithCGPoint:currentPoint1] forKey:key];
    
        }
    }
    

    您当前在此处设置lastPoint,但您似乎没有使用它(而且它只能通过一键式操作)。也不需要将lastPoint 作为字段。

    【讨论】:

    • 谢谢,它起作用了,我这样做是因为,我希望每当我把手放在 ipad 屏幕上时都能写字。可以指导我如何拒绝手触摸,只用手指/手写笔绘制,用手腕触摸绘制的任何东西都应该被取消/拒绝
    • 另外,这只是一个带有两个点的示例示例,即最后一个点和当前点,现在在我的项目中,要绘制平滑的线条和曲线,我有两个最后一个点和一个当前点开始接触, 那么我应该将它们全部存储在单独的字典中吗?
    • 有关如何绘制更平滑路径的提示,请参阅此页面:mobile.tutsplus.com/tutorials/iphone/ios-sdk_freehand-drawing
    • 关于手指/手部检测,已经有很多问题:stackoverflow.com/questions/11792510/…stackoverflow.com/questions/4302515/…
    • 嘿,谢谢@Krumelur,首先,您建议的平滑绘图链接使用了UIBezeirPath。其次我的问题不是关于平滑绘图,我的问题是我是否需要将所有接触点添加到单独的 NSDictionaries。
    猜你喜欢
    • 1970-01-01
    • 2014-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多