我正在尝试根据收缩和收缩在上下文中缩放绘图。这是完美的工作,但它总是会从原点(左上角)缩放。这不会给您在其他应用程序中获得的良好效果,而且真的很烦人。旧的缩放方法如下:
- (IBAction)handlePinchGesture:(UIGestureRecognizer *)sender {
UIPinchGestureRecognizer *pinch = (UIPinchGestureRecognizer *)sender;
if ([pinch scale] > zoomLevel + MAX_ZOOM_PER_CALL) {
[pinch setScale: zoomLevel + MAX_ZOOM_PER_CALL];
}
if ([pinch scale] < zoomLevel - MAX_ZOOM_PER_CALL) {
[pinch setScale:zoomLevel - MAX_ZOOM_PER_CALL];
}
float prevZoomLevel = zoomLevel;
CGFloat factor = [pinch scale];
zoomLevel = factor;
if (zoomLevel < MIN_ZOOM_LEVEL) {
zoomLevel = MIN_ZOOM_LEVEL;
}
if (zoomLevel > MAX_ZOOM_LEVEL) {
zoomLevel = MAX_ZOOM_LEVEL;
}
//The next piece of code is added here
[self setNeedsDisplay];
}
实际缩放是在上下文中显示的零件的绘制方法中完成的。我正在考虑更改屏幕的原点(即要绘制的项目的相对原点),以便模拟这一点。但它根本不起作用,这就是我现在所拥有的:
float dZoomLevel = zoomLevel - prevZoomLevel;
if (dZoomLevel != 0) {
CGPoint touchLocation = [pinch locationInView:self];
CGPoint zoomedTouchLocation = CGPointMake(touchLocation.x * prevZoomLevel, touchLocation.y * prevZoomLevel);
CGPoint newLocation = CGPointMake(touchLocation.x * zoomLevel, touchLocation.y * zoomLevel);
float dX = ( newLocation.x - zoomedTouchLocation.x );
float dY = ( newLocation.y - zoomedTouchLocation.y );
_originX += roundf(dX);
_originY += roundf(dY);
}
原点取自 xml(所有元素中最小的 x 和 y)。它通常在 12000 到 20000 之间,但理论上可能在 0 到 100000 之间。
我不确定我是否已经说清楚了,但是如果您需要任何信息或不明白的地方,请问我,我会尽快回复。提前致谢!