gpt4 book ai didi

objective-c - 从捏合位置缩放在 CGContext 上绘制的图像

转载 作者:搜寻专家 更新时间:2023-10-30 19:46:25 24 4
gpt4 key购买 nike

这个问题是前一段时间另一个 SO 问题的跟进:zooming using pinch but not using transform methods。我的缩放功能几乎完美地工作,但它仍然会使绘制的图像像我一直所说的那样“跳来跳去”。代码中最重要的部分如下:

当观察到捏合事件时调用此方法:

[代码片段 1]

- (IBAction)handlePinchGesture:(UIGestureRecognizer *)sender {
float previousZoomLevel = zoomLevel;
UIPinchGestureRecognizer *pinch = (UIPinchGestureRecognizer *)sender;

float zoomIncreaseFactor = [self getZoomFactor:[pinch scale]]; //= 13 or 7 or 10
float estimatedDelta = (previousZoomLevel * zoomIncreaseFactor) / 150;

if ([pinch scale] > previousScale) {
zoomLevel += estimatedDelta;
}

if ([pinch scale] < previousScale) {
zoomLevel -= estimatedDelta;
}

previousScale = [pinch scale];

if (zoomLevel < MIN_ZOOM_LEVEL) {
zoomLevel = MIN_ZOOM_LEVEL;
}

if (zoomLevel > MAX_ZOOM_LEVEL) {
zoomLevel = MAX_ZOOM_LEVEL;
}

CGPoint loc = [pinch locationInView:self];

NSLog(@"loc: {%.1f,%.1f}, lastPinchLoc: {%.1f,%.1f}", loc.x,loc.y,lastPinchLocation.x,lastPinchLocation.y);

//uncomment the below part to always zoom from the first zoom location.
//if (lastPinchLocation.x != 0 && lastPinchLocation.y != 0) {
//loc.x += lastPinchLocation.x - loc.x;
//loc.y += lastPinchLocation.y - loc.y;
//}

//NSLog(@"newLastPinchLocation: {%.1f,%.1f}", loc.x,loc.y);

lastPinchLocation = loc;

hasBeenMoved = YES;

if (zoomLevel != previousZoomLevel) {
[self setNeedsDisplay];
}
}

当我在整个屏幕上捏合时,上面的 NSLog 给出了以下打印:

2012-10-16 10:15:19.357 App[2609:907] loc: {147.0,232.0}, lastPinchLoc: {0.0,0.0}
2012-10-16 10:15:19.391 App[2609:907] loc: {147.0,231.0}, lastPinchLoc: {147.0,232.0}
2012-10-16 10:15:19.436 App[2609:907] loc: {148.0,232.0}, lastPinchLoc: {147.0,231.0}
2012-10-16 10:15:19.457 App[2609:907] loc: {148.0,232.0}, lastPinchLoc: {148.0,232.0}
2012-10-16 10:15:19.474 App[2609:907] loc: {148.0,233.0}, lastPinchLoc: {148.0,232.0}
2012-10-16 10:15:19.507 App[2609:907] loc: {147.0,233.0}, lastPinchLoc: {148.0,233.0}
2012-10-16 10:15:19.573 App[2609:907] loc: {103.0,355.0}, lastPinchLoc: {147.0,233.0}
2012-10-16 10:15:20.324 App[2609:907] loc: {585.0,710.0}, lastPinchLoc: {103.0,355.0}
2012-10-16 10:15:20.340 App[2609:907] loc: {584.0,709.0}, lastPinchLoc: {585.0,710.0}
2012-10-16 10:15:20.356 App[2609:907] loc: {584.0,710.0}, lastPinchLoc: {584.0,709.0}
2012-10-16 10:15:20.374 App[2609:907] loc: {584.0,710.0}, lastPinchLoc: {584.0,710.0}
2012-10-16 10:15:20.407 App[2609:907] loc: {583.0,710.0}, lastPinchLoc: {584.0,710.0}
2012-10-16 10:15:20.456 App[2609:907] loc: {512.0,811.0}, lastPinchLoc: {583.0,710.0}
2012-10-16 10:15:21.056 App[2609:907] loc: {645.0,163.0}, lastPinchLoc: {512.0,811.0}
2012-10-16 10:15:21.090 App[2609:907] loc: {647.0,164.0}, lastPinchLoc: {645.0,163.0}
2012-10-16 10:15:21.107 App[2609:907] loc: {647.0,165.0}, lastPinchLoc: {647.0,164.0}
2012-10-16 10:15:21.156 App[2609:907] loc: {602.0,249.0}, lastPinchLoc: {647.0,165.0}
2012-10-16 10:15:21.839 App[2609:907] loc: {153.0,702.0}, lastPinchLoc: {602.0,249.0}
2012-10-16 10:15:21.856 App[2609:907] loc: {157.0,703.0}, lastPinchLoc: {153.0,702.0}
2012-10-16 10:15:21.874 App[2609:907] loc: {159.0,704.0}, lastPinchLoc: {157.0,703.0}
2012-10-16 10:15:21.890 App[2609:907] loc: {160.0,704.0}, lastPinchLoc: {159.0,704.0}
2012-10-16 10:15:21.906 App[2609:907] loc: {123.0,796.0}, lastPinchLoc: {160.0,704.0}
2012-10-16 10:15:21.923 App[2609:907] loc: {125.0,794.0}, lastPinchLoc: {123.0,796.0}
2012-10-16 10:15:22.706 App[2609:907] loc: {368.0,303.0}, lastPinchLoc: {125.0,794.0}
2012-10-16 10:15:22.723 App[2609:907] loc: {369.0,307.0}, lastPinchLoc: {368.0,303.0}
2012-10-16 10:15:22.725 App[2609:907] loc: {369.0,307.0}, lastPinchLoc: {369.0,307.0}
2012-10-16 10:15:22.739 App[2609:907] loc: {371.0,312.0}, lastPinchLoc: {369.0,307.0}
2012-10-16 10:15:22.756 App[2609:907] loc: {374.0,312.0}, lastPinchLoc: {371.0,312.0}
2012-10-16 10:15:22.773 App[2609:907] loc: {338.0,392.0}, lastPinchLoc: {374.0,312.0}

就在 CGContext 上绘制项目之前,x 和 y 被放入此方法中,我在返回的位置绘制项目。类变量“zoomTouchLocation”与上述代码段中的“lastPinchLocation”具有相同的值。

[代码片段 2]

- (CGPoint)handleCoordinatesX:(float)x y:(float)y {
x -= zoomTouchLocation.x;
y -= zoomTouchLocation.y;

x = x * zoomLevel;
y = y * zoomLevel;

x += zoomTouchLocation.x;
y += zoomTouchLocation.y;

return CGPointMake(x, y);
}

如果我每次都在大致相同的位置捏合,它会按预期工作,但当我捏合整个屏幕时,图像会疯狂地跳来跳去。

现在基本上我的问题是,是否有人可以帮助我解决这个“跳来跳去”的问题,并让图像从我放大的位置缩放而不跳来跳去。如果您需要更多信息,请在下面的评论中提问。

(我确实知道我的代码按原样发生跳跃是很正常的,我只是找不到阻止它这样做的方法)

最佳答案

没有完整的代码很难提供帮助,但您应该尝试将 view.layer.anchorPoint 设置为您的 pinchLocation(如果尚未完成)。像这样,变换完成后,这个点不动。对于旋转或缩放非常有用。

关于objective-c - 从捏合位置缩放在 CGContext 上绘制的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12910465/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com