iphone - 带有 UIPanGestureRecognizer 的多点触控

标签 iphone ios ipad touch multi-touch

我正在尝试为我的 View 实现 UIPanGestureRecognizer。如何添加多点触控?下面是我 View 中的代码(UIView 的子类)。我希望能够同时知道所有触摸的位置和速度。当前代码仅打印出一次触摸的位置和速度。更改属性 minimumNumberOfTouches 和 maximumNumberOfTouches 不起作用。非常感谢您的帮助。

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));

}

最佳答案

来自 UIGestureRecogniser 的苹果文档

(CGPoint)locationOfTouch:(NSUInteger)touchIndex inView:(UIView *)view Parameters touchIndex The index of a UITouch object in a private array maintained by the receiver. This touch object represents a touch of the current gesture. view A UIView object on which the gesture took place. Specify nil to indicate the window. 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.

(NSUInteger)numberOfTouches Return Value The number of UITouch objects in a private array maintained by the receiver. 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.

例如:

( objective-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)
    }

至于速度,我认为它只有一个值,如果不编写一个自定义方法来计算一系列调用中每次触摸之间的差异,就无法获得每次触摸的速度。但是,不能保证每次触摸都在相同的索引处。

注意:至于最小和最大触摸次数,需要相应设置才能获得多点触摸。

关于iphone - 带有 UIPanGestureRecognizer 的多点触控,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10592677/

相关文章:

ios - 如何使用 CoreData 和 CloudKit 处理离线模式?

objective-c - UI切换自定义

iphone - IOS。 viewControllers 数组的错误?

ios - 将 float 四舍五入到最接近的 .5

iphone - NSURL 异常

Objective-C (iOS/ cocoa ) : expand first letter into day of week

iphone - 2 个相似的 iOS 应用程序 - 管理它们的最佳方式是什么?

iphone - 自动释放对象两次

ios - 上传前在 XCode 中构建应用程序时测试架构是否正确

iphone - NSMutableArray 越界