4

我正在创建一个用于显示时间线的控件。最初,它应该显示年份。当放大并zoomScale达到特定值时,它将显示各个月份的值,并且在下一个缩放级别中,它应该显示各个天的实际值。

我已经创建了一个scrollview并将layers(用于显示月/年值)添加到此。覆盖捏手势后,我可以进行缩放(正常 UIScrollview 缩放)。但我需要将其缩放到特定点。即,假设我正在缩放一月,我需要将其保持在相同的位置(以创建像在一月内部移动的效果)。有什么建议可以实现这一目标吗?

我的方法正确吗?否则请帮助我开始这个。

4

5 回答 5

3

根据您的要求使用以下方法,

- (void)zoomToRect:(CGRect)rect animated:(BOOL)animated

只需使用您选择的框架或点调用上述方法,例如如果选择一月然后只需获取一月按钮或视图的点(位置),然后使用该框架调用上述方法。

有关更多信息,请参阅此链接UIScrollView_Class

我希望这对你有帮助......

于 2013-02-04T11:50:23.117 回答
2

这是它的样子:

@implementation UIScrollView (ZoomToPoint)

- (void)zoomToPoint:(CGPoint)zoomPoint withScale: (CGFloat)scale animated: (BOOL)animated
{
    //Normalize current content size back to content scale of 1.0f
    CGSize contentSize;
    contentSize.width = (self.contentSize.width / self.zoomScale);
    contentSize.height = (self.contentSize.height / self.zoomScale);

    //translate the zoom point to relative to the content rect
    zoomPoint.x = (zoomPoint.x / self.bounds.size.width) * contentSize.width;
    zoomPoint.y = (zoomPoint.y / self.bounds.size.height) * contentSize.height;

    //derive the size of the region to zoom to
    CGSize zoomSize;
    zoomSize.width = self.bounds.size.width / scale;
    zoomSize.height = self.bounds.size.height / scale;

    //offset the zoom rect so the actual zoom point is in the middle of the rectangle
    CGRect zoomRect;
    zoomRect.origin.x = zoomPoint.x - zoomSize.width / 2.0f;
    zoomRect.origin.y = zoomPoint.y - zoomSize.height / 2.0f;
    zoomRect.size.width = zoomSize.width;
    zoomRect.size.height = zoomSize.height;

    //apply the resize
    [self zoomToRect: zoomRect animated: animated];
}

@end

看着它,应该很容易弄清楚它是如何工作的。如果您发现它有任何问题,请在评论中告诉我。
谢谢 :)

来源:http ://www.tim-oliver.com/2012/01/14/zooming-to-a-point-in-uiscrollview/

于 2013-02-04T10:53:22.310 回答
1

我有同样的问题,
我有一张世界地图图像,我想将地图缩放到某个位置,印度
这就是我修复它的方法。
我在 CGRect(X,Y) [CGRect(500,300)]
和 DidLoad 中的以下代码方面发现了印度的位置

    [self.scrollViewForMap setZoomScale:2.0 animated:YES]; //for zooming
    _scrollViewForMap.contentOffset = CGPointMake(500,300); //for location

这解决了我的问题。:)

于 2014-12-10T09:47:28.647 回答
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
}
}
于 2015-02-18T07:58:19.133 回答
0

什么对我有用(Swift 4):

func zoomIn(with gestureRecognizer: UIGestureRecognizer?) {
    guard let location = gestureRecognizer?.location(in: gestureRecognizer?.view)

    let rect = CGRect(x: location.x, y: location.y, width: 0, height: 0)
    scrollView.zoom(to: rect, animated: true)
}

或者换句话说,创建一个CGRect宽度和高度为零的将导致缩放UIScrollView到矩形originmaximumZoomScale

于 2018-05-16T12:24:23.267 回答