Można rozwiązać ten problem poprzez wdrożenie niestandardowy PanGestureRecognizer
, który zapisuje oryginalny punkt dotyku i udostępnia go rozmówcy.
Poszedłem na tę trasę, ponieważ chciałem również kontrolować odległość przesuniętą, aby wywołać gest panoramy .
To może być przesada dla twojej sytuacji, ale działa idealnie i jest trudniejsze niż się wydaje.
Aby uzyskać styczności współrzędnych, wystarczy zadzwonić:
[sender touchPointInView:yourView]
zamiast:
[sender locationInView:yourView]
Oto kod dla PanGestureRecognizer.m
:
#import "PanGestureRecognizer.h"
#import <UIKit/UIGestureRecognizerSubclass.h>
@implementation PanGestureRecognizer
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
UITouch *touch = [touches anyObject];
// touchPoint is defined as: @property (assign,nonatomic) CGPoint touchPoint;
self.touchPoint = [touch locationInView:nil];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesMoved:touches withEvent:event];
UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:nil];
// customize the pan distance threshold
CGFloat dx = fabs(p.x-self.touchPoint.x);
CGFloat dy = fabs(p.y-self.touchPoint.y);
if (dx > 2 || dy > 2) {
if (self.state == UIGestureRecognizerStatePossible) {
[self setState:UIGestureRecognizerStateBegan];
}
else {
[self setState:UIGestureRecognizerStateChanged];
}
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
if (self.state == UIGestureRecognizerStateChanged) {
[self setState:UIGestureRecognizerStateEnded];
}
else {
[self setState:UIGestureRecognizerStateCancelled];
}
}
- (void) reset
{
}
// this returns the original touch point
- (CGPoint) touchPointInView:(UIView *)view
{
CGPoint p = [view convertPoint:self.touchPoint fromView:nil];
return p;
}
@end
Ten link może pomóc - http://www.icodeblog.com/2010/10/14/working-with-uigesturerecognizers/ – Coffee