在我看来,我添加了长按手势和平移手势,如下所示
UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(fitToView:)];
[longPressRecognizer setDelegate:self];
longPressRecognizer.allowableMovement = 5.0f;
longPressRecognizer.minimumPressDuration = 2.0;
[self addGestureRecognizer:longPressRecognizer];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
panRecognizer.delegate = self;
[self addGestureRecognizer:panRecognizer];
[panRecognizer requireGestureRecognizerToFail:longPressRecognizer];
他们的手势处理程序如下:
-(void)move:(UIPanGestureRecognizer*)recognizer {
CGPoint translatedPoint = [recognizer translationInView:self.imgView ];
if ([recognizer state] == UIGestureRecognizerStateBegan) {
_firstX = [self.imgView center].x;
_firstY = [self.imgView center].y;
}
CGAffineTransform newTransform = CGAffineTransformTranslate(CGAffineTransformIdentity, translatedPoint.x , translatedPoint.y );
self.imgView.transform = newTransform;
}
-(void)fitToView:(UILongPressGestureRecognizer*)recognizer {
if ([recognizer state] == UIGestureRecognizerStateBegan) {
self.imgView.transform = CGAffineTransformIdentity;
}
}
长按是为了恢复图像。但是当我移动图像时,长按手势代表也会调用并恢复我所做的所有更改。我曾经[panRecognizer requireGestureRecognizerToFail:longPressRecognizer];
长按识别器失败。但它不会发生。我还尝试了委托方法
gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:。但它没有工作