如何将UIScrollView缩放到固定点?

4

我正在创建一个用于显示时间轴的控件。最初,应该显示年份。当缩放至zoomScale到达特定值时,它将显示每个月份的值,在下一个缩放级别中,它应该显示每个独立日的实际值。

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

我的方式正确吗?否则,请帮我开始这个项目。


你的意思是scrollView在特定大小进行缩放吗? - iPatel
为什么不尝试使用-zoomToRect:animated:方法呢? - holex
5个回答

3
请使用以下方法来满足您的需求:
- (void)zoomToRect:(CGRect)rect animated:(BOOL)animated

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

如需更多信息,请参见此链接:UIScrollView_Class

希望这对您有所帮助...


我没有使用默认的UIScrollview缩放,而是在捏合手势中调整层大小以保持文本清晰度。请参见以下代码进行缩放。[CATransaction begin]; [CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions]; tmpLayer.frame = 'CGRectMake(ilayerWidthOnPinchrecognizer.scale,tmpLayer.frame.origin.y,layerWidthOnPinch*recognizer.scale, tmpLayer.frame.size.height); [tmpLayer setNeedsDisplay]; [CATransaction commit]; eventScroll.contentOffset = CGPointMake(500, 0); - user2039257
尝试一下这个,也许你可以从这里得到一些想法:http://stackoverflow.com/questions/11536555/i-would-like-to-implement-only-zoom-in-feature-on-uiscrollview-in-iphone-sdk-is - Paras Joshi
这个也是关于编程的内容...https://dev59.com/4G435IYBdhLWcg3w4Uas 我曾经从这里获取了一些有关PichGesture的想法。https://dev59.com/4G435IYBdhLWcg3w4Uas - Paras Joshi

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/


我没有使用默认的UIScrollview缩放。我正在调整图层大小以保留文本清晰度。 - user2039257
我正在处理这段代码。它能够运行,但是有些地方出现了错误。我使用的是Scale 3.0版本,当我尝试进行zoomToRect操作时,内容矩形的中心位置出现了偏差。 - Arthur
哇!那是我的博客文章!非常感谢给我信用!不过,Nicolas 是正确的。当我试图使用更复杂的缩放时,我发现如果你不从最小比例值进行缩放,原点计算会出错。因此,我刚刚完成了一个新版本的实现。看看这个新版本是否有所帮助! :) https://gist.github.com/TimOliver/71be0a8048af4bd86ede - TiM

1
我遇到了同样的问题, 我有一张世界地图图片,我想将地图缩放到印度这个位置。 这是我解决问题的方法。 我找到了印度的位置,用CGRect(X,Y) [CGRect(500,300)]表示。 在DidLoad中添加以下代码。
    [self.scrollViewForMap setZoomScale:2.0 animated:YES]; //for zooming
    _scrollViewForMap.contentOffset = CGPointMake(500,300); //for location

这解决了我的问题。 :)

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
}
}

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将导致UIScrollViewmaximumZoomScale下缩放到矩形的origin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接