簡體   English   中英

如何在Xcode中跟蹤多次觸摸

[英]How to track multiple touches in Xcode

最近,我正在制作一個可以同時拖動多個對象的應用程序。 我曾嘗試使用UIPanGestureRecognizer來獲取手指觸摸的坐標,但是我不知道哪個觸摸屬於哪個手指。

我需要同時支持四個手指平移,而又不能使用Objective-C互相干擾。

我已經搜索了好一陣子的解決方案,但是他們給出的答案對我來說並不起作用。 任何幫助,將不勝感激。

我在同一個問題上苦苦掙扎了很長時間,終於解決了。 以下是我的DrawView.m的代碼,它是UIView的子類,該子類能夠使用drawRect:支持繪圖drawRect:

#import "DrawView.h"

#define MAX_TOUCHES 4

@interface DrawView() {

    bool touchInRect[MAX_TOUCHES];
    CGRect rects[MAX_TOUCHES];
    UITouch *savedTouches[MAX_TOUCHES];
}

@end

@implementation DrawView

-(id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        // Initialization code
        self.multipleTouchEnabled = YES;
        for (int i=0; i<MAX_TOUCHES; i++) {
            rects[i] = CGRectMake(200, 200, 50 ,50);
            savedTouches[i] = NULL;
            touchInRect[i] = false;
        }
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    // Drawing code
    [[UIColor blueColor] set];
    CGContextRef context = UIGraphicsGetCurrentContext();

    for (int i=0; i<MAX_TOUCHES; i++) {
        CGContextFillRect(context, rects[i]);
        CGContextStrokePath(context);
    }
}

#pragma mark - Handle Touches

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

    NSArray *allTouches = [touches allObjects];
    for (int i=0; i<[allTouches count]; i++) {
        UITouch *touch = allTouches[i];
        CGPoint newPoint = [touch locationInView:self];

        for (int j=0; j<MAX_TOUCHES; j++) {
            if (CGRectContainsPoint(rects[j], newPoint) && !touchInRect[j]) {
                touchInRect[j] = true;
                savedTouches[j] = touch;
                break;
            }
        }
    }
}

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

    NSArray *allTouches = [touches allObjects];
    for (int i=0; i<[allTouches count]; i++) {
        UITouch *touch = allTouches[i];
        CGPoint newPoint = [touch locationInView:self];

        for (int j=0; j<MAX_TOUCHES; j++) {
            if (touch == savedTouches[j]) {
                rects[j] = [self rectWithSize:rects[j].size andCenter:newPoint];
                [self setNeedsDisplay];
                break;
            }
        }
    }
}

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

    NSArray *allTouches = [touches allObjects];
    for (int i=0; i<[allTouches count]; i++) {
        UITouch *touch = allTouches[i];

        for (int j=0; j<MAX_TOUCHES; j++) {
            if (touch == savedTouches[j]) {
                touchInRect[j] = false;
                savedTouches[j] = NULL;
                break;
            }
        }
    }
}


- (CGRect)rectWithSize:(CGSize)size andCenter:(CGPoint)point {
    return CGRectMake(point.x - size.width/2, point.y - size.height/2, size.width, size.height);
}

@end

我將MAX_TOUCHES設置為4,因此屏幕上將有四個對象。 其基本概念是在調用touchesBegan::時將每個UITouch ID存儲在savedTouches數組中,然后在調用touchesMoved::時將每個ID與屏幕上的觸摸進行比較。

只要將代碼粘貼到您的.m文件中,它就可以工作。 示例結果如下所示:

在此處輸入圖片說明

希望這可以幫助 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM