我想向視圖添加手勢識別器。當我添加pan(移動)和pinch(縮放)時,我會遇到一些奇怪的行爲。手勢識別器,奇怪的行爲(乘數效應)iOS
在縮放視圖(恰好包含imageView)後,平移手勢識別器似乎將移動的效果倍增。或者它仍然採用原來的比例,所以這個舉動比它應該更快?
下面是方法的代碼移動和規模:
//ivars for gesture recognizers/transforms
CGFloat scale;
CGFloat rotation;
CGPoint translatedPoint;
CGFloat _lastScale;
CGFloat _lastRotation;
CGFloat _firstX;
CGFloat _firstY;
-(void)scale:(id)sender {
if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
_lastScale = 1.0;
}
scale = 1.0 - (_lastScale - [(UIPinchGestureRecognizer*)sender scale]);
CGAffineTransform currentTransform = gestureView.transform;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
[gestureView setTransform:newTransform];
_lastScale = [(UIPinchGestureRecognizer*)sender scale];
NSLog(@"gestureView origin x: %f, y: %f", gestureView.frame.origin.x, gestureView.frame.origin.y);
NSLog(@"gestureView center x: %f, y: %f",[gestureView center].x, [gestureView center].y);
NSLog(@"gestureView scale x: %f, scale y: %f", gestureView.frame.size.width, gestureView.frame.size.height);
}
-(void)move:(id)sender {
translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:gestureView];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
_firstX = [gestureView center].x;
_firstY = [gestureView center].y;
}
translatedPoint = CGPointMake(_firstX+translatedPoint.x, _firstY+translatedPoint.y);
[gestureView setCenter:translatedPoint];
NSLog(@"gestureView origin x: %f, y: %f", gestureView.frame.origin.x, gestureView.frame.origin.y);
NSLog(@"gestureView center x: %f, y: %f",[gestureView center].x, [gestureView center].y);
}
下面是該項目: owolf.net/uploads/StackOverflow/GestureRecognizerTest.zip
感謝您的閱讀!