我正在尝试将手势识别器添加到视图中。当我同时添加平移(移动)和捏(缩放)时,我得到了一些奇怪的行为。
在视图(恰好包含一个 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
谢谢阅读!