我有一个调用UIView类的UIViewController,我正在使用CGAffine Transform使用以下代码放大我的UIView类实例
CGAffineTransform transform = CGAffineTransformMakeScale(recognizer.scale, recognizer.scale);
NSLog(@"Pinch scale: %f", recognizer.scale);
float scale = recognizer.scale;
float SCALE_MIN = 1.0f;
float SCALE_MAX = 3.0f;
. . .
这个基本代码,有效! (某些代码已被省略)
现在的问题是无论屏幕上的哪个位置“捏”,文件总是从文件的中间放大,而不是在我捏的位置。 。
有关如何让它缩放到精确位置的任何建议吗?
答案 0 :(得分:1)
在locationInView:
的实例上调用时,UIGestureRecognizers
方法可用于所有UIPinchGestureRecognizer
,它将为您提供构成捏合的两个触摸之间的中点。这是我认为你会得到的最接近的东西“确切的位置。”
首次识别手势时,保存locationInView
以获取您想要缩放的点。我不会尝试在夹点过程中更新缩放中心,因为我认为你最终会得到一个不直观的复杂实现。您需要缩放以保持中心点的固定,并且您应该可以通过设置要转换的视图的anchorPoint
的{{1}}来实现此目的。请注意,layer
坐标已标准化 - 默认为anchorPoint
,即视图的中心。
答案 1 :(得分:0)
- (void)pinchDetected:(UIPinchGestureRecognizer *)gestureRecognizer {
if([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
// Reset the last scale, necessary if there are multiple objects with different scales
lastScale = [gestureRecognizer scale];
}
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan ||
[gestureRecognizer state] == UIGestureRecognizerStateChanged) {
CGFloat currentScale = [[[gestureRecognizer view].layer valueForKeyPath:@"transform.scale"] floatValue];
// Constants to adjust the max/min values of zoom
const CGFloat kMaxScale = 2.2;
const CGFloat kMinScale = 0.64;
CGFloat newScale = 1 - (lastScale - [gestureRecognizer scale]);
newScale = MIN(newScale, kMaxScale / currentScale);
newScale = MAX(newScale, kMinScale / currentScale);
CGAffineTransform transform = CGAffineTransformScale([[gestureRecognizer view] transform], newScale, newScale);
[gestureRecognizer view].transform = transform;
[gestureRecognizer setScale:1.0];
lastScale = [gestureRecognizer scale]; // Store the previous scale factor for the next pinch gesture call
}
}
答案 2 :(得分:-1)
希望这对你有所帮助,这对我来说就像魅力一样......
- (void)pinchDetected:(UIPinchGestureRecognizer *)pinchRecognizer
{
CGAffineTransform transform = CGAffineTransformScale(pinchRecognizer.view.transform, pinchRecognizer.scale,pinchRecognizer.scale);
_designImage.transform = transform;
_Zoomscale = pinchRecognizer.scale;
NSLog(@"image Pinch scale: %2ff", _Zoomscale);
}