简体   繁体   中英

Multitouch with UIPanGestureRecognizer

I'm trying to implement UIPanGestureRecognizer for my view. How do I add multitouch? Below is the code from my view (a subclass of 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. 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

(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.

For example:

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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