【问题标题】:How to track multiple touches in Xcode如何在 Xcode 中跟踪多次触摸
【发布时间】:2017-09-12 02:17:53
【问题描述】:

最近我正在制作一个可以同时拖动多个对象的应用程序。我曾尝试使用UIPanGestureRecognizer 来获取手指触摸的坐标,但我不知道哪个触摸属于哪个手指。

我需要使用 Objective-C 支持四个手指同时平移而不会相互干扰。

我已经搜索了一段时间的解决方案,但他们显示的答案对我不起作用。任何帮助将不胜感激。

【问题讨论】:

    标签: ios objective-c xcode multi-touch


    【解决方案1】:

    我在同一个问题上苦苦挣扎了很长时间,终于解决了。以下是我的DrawView.m中的代码,它是UIView的子类,可以支持使用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 文件中即可。示例结果如下所示:

    希望这会有所帮助:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-12
      • 2013-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多