简体   繁体   English

使用UIPanGestureRecognizer进行多点触控

[英]Multitouch with UIPanGestureRecognizer

I'm trying to implement UIPanGestureRecognizer for my view. 我正在尝试为我的视图实现UIPanGestureRecognizer。 How do I add multitouch? 如何添加多点触控? Below is the code from my view (a subclass of UIView). 下面是我视图中的代码(UIView的子类)。 I would like to be able to know the location and velocity for all of the touches at the same time. 我希望能够同时知道所有触摸的位置和速度。 The current code only prints out the location and velocity for one touch. 当前代码仅打印一次触摸的位置和速度。 Changing the properties minimumNumberOfTouches and maximumNumberOfTouches doesn't work. 更改属性minimumNumberOfTouches和maximumNumberOfTouches不起作用。 Thank you very much for your help. 非常感谢您的帮助。

UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
panGestureRecognizer.cancelsTouchesInView = NO;
[self addGestureRecognizer:panGestureRecognizer];


- (void)handlePanGesture:(UIPanGestureRecognizer *)panGestureRecognizer
{
    CGPoint location = [panGestureRecognizer locationInView:panGestureRecognizer.view];
    CGPoint velocity = [panGestureRecognizer velocityInView:panGestureRecognizer.view];

    NSLog(@"Location: %@",  NSStringFromCGPoint(location));
    NSLog(@"Velocity: %@",  NSStringFromCGPoint(velocity));

}

From the apple documentation for UIGestureRecogniser 来自UIGestureRecogniser的苹果文档

(CGPoint)locationOfTouch:(NSUInteger)touchIndex inView:(UIView *)view Parameters touchIndex The index of a UITouch object in a private array maintained by the receiver. (CGPoint)locationOfTouch:(NSUInteger)touchIndex inView:(UIView *)视图参数touchIndex接收方维护的专用数组中UITouch对象的索引。 This touch object represents a touch of the current gesture. 该触摸对象表示当前手势的触摸。 view A UIView object on which the gesture took place. view发生手势的UIView对象。 Specify nil to indicate the window. 指定nil以指示窗口。 Return Value A point in the local coordinate system of view that identifies the location of the touch. 返回值视图的局部坐标系中的一个点,用于标识触摸的位置。 If nil is specified for view, the method returns the touch location in the window's base coordinate system. 如果为视图指定了nil,则该方法返回窗口基本坐标系中的触摸位置。

(NSUInteger)numberOfTouches Return Value The number of UITouch objects in a private array maintained by the receiver. (NSUInteger)numberOfTouches返回值接收方维护的专用数组中的UITouch对象数。 Each of these objects represents a touch in the current gesture. 这些对象中的每一个表示当前手势中的触摸。

Discussion Using the value returned by this method in a loop, you can ask for the location of individual touches using the locationOfTouch:inView: method. 讨论在循环中使用此方法返回的值,您可以使用locationOfTouch:inView:方法询问各个触摸的位置。

For example: 例如:

(Objective-C) (目标C)

    for(int i=0; i<[panGestureRecogniser numberOfTouches]; i++)
    {
         CGPoint pt = [panGestureRecogniser locationOfTouch:i inView:self];
    }

(Swift) (迅速)

    for i in 0..<panGestureRecogniser.numberOfTouches {
         let pt = recognizer.location(ofTouch: i, in: panGestureRecognizer.view)
    }

As for velocity I believe there is only one value for it and there is no way to get the velocity of each touch without writing a custom method that calculates the difference between each touch over a series of calls. 至于速度,我相信它只有一个值,并且没有办法在不编写自定义方法的情况下获得每次触摸的速度,该方法计算一系列调用中每次触摸之间的差异。 There is no guarantee that the touches will be at the same index each time however. 然而,无法保证每次触摸都在相同的索引处。

Note: As for the minimum and maximum number of touches, these will need to be set accordingly to get multiple touches. 注意:对于最小和最大触摸次数,需要相应地设置这些触摸以获得多次触摸。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM