如何使用scrollRectToVisible滚动到图像的中心?

发布于 2024-08-20 10:23:42 字数 141 浏览 2 评论 0原文

我有一个带有缩放和平移功能的 UIScrollView 。我希望图像在用户命令后滚动到中心。我的问题是计算图像中心的框架的大小和位置。

有谁知道如何计算图像中心的正确框架?问题是,如果 ZoomScale 不同,框架会发生变化。

谢谢!

I have a UIScrollView with zooming and panning. I want the image to scroll to the center after a user command. My problem is in calculating the size and location of a frame that is in the center of the image.

Does anyone know how to calculate the correct frame for the center of my image? The problem is that if the zoomScale is different the frame changes.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

帅气尐潴 2024-08-27 10:23:42

如果有人需要的话,这里可能有更好的代码;-)

UIScrollView+CenteredScroll.h:

@interface UIScrollView (CenteredScroll)

-(void)scrollRectToVisibleCenteredOn:(CGRect)visibleRect
                            animated:(BOOL)animated;

@end

UIScrollView+CenteredScroll.m:

@implementation UIScrollView (CenteredScroll)

-(void)scrollRectToVisibleCenteredOn:(CGRect)visibleRect
                            animated:(BOOL)animated
{
  CGRect centeredRect = CGRectMake(visibleRect.origin.x + visibleRect.size.width/2.0 - self.frame.size.width/2.0,
                                   visibleRect.origin.y + visibleRect.size.height/2.0 - self.frame.size.height/2.0,
                                   self.frame.size.width,
                                   self.frame.size.height);
  [self scrollRectToVisible:centeredRect
                   animated:animated];
}

@end

Here's maybe a bit better code in case anyone is in need ;-)

UIScrollView+CenteredScroll.h:

@interface UIScrollView (CenteredScroll)

-(void)scrollRectToVisibleCenteredOn:(CGRect)visibleRect
                            animated:(BOOL)animated;

@end

UIScrollView+CenteredScroll.m:

@implementation UIScrollView (CenteredScroll)

-(void)scrollRectToVisibleCenteredOn:(CGRect)visibleRect
                            animated:(BOOL)animated
{
  CGRect centeredRect = CGRectMake(visibleRect.origin.x + visibleRect.size.width/2.0 - self.frame.size.width/2.0,
                                   visibleRect.origin.y + visibleRect.size.height/2.0 - self.frame.size.height/2.0,
                                   self.frame.size.width,
                                   self.frame.size.height);
  [self scrollRectToVisible:centeredRect
                   animated:animated];
}

@end
温柔戏命师 2024-08-27 10:23:42

根据 Daniel Bauke 的回答,我更新了他的代码以包括缩放比例:

@implementation UIScrollView (jsCenteredScroll)

-(void)jsScrollRectToVisibleCenteredOn:(CGRect)visibleRect
                            animated:(BOOL)animated
{

    CGPoint center = visibleRect.origin;
    center.x += visibleRect.size.width/2;
    center.y += visibleRect.size.height/2;

    center.x *= self.zoomScale;
    center.y *= self.zoomScale;


    CGRect centeredRect = CGRectMake(center.x - self.frame.size.width/2.0,
                                     center.y - self.frame.size.height/2.0,
                                     self.frame.size.width,
                                     self.frame.size.height);
    [self scrollRectToVisible:centeredRect
                     animated:animated];
}

@end

Based on Daniel Bauke answer, I updated his code to include zoom scale :

@implementation UIScrollView (jsCenteredScroll)

-(void)jsScrollRectToVisibleCenteredOn:(CGRect)visibleRect
                            animated:(BOOL)animated
{

    CGPoint center = visibleRect.origin;
    center.x += visibleRect.size.width/2;
    center.y += visibleRect.size.height/2;

    center.x *= self.zoomScale;
    center.y *= self.zoomScale;


    CGRect centeredRect = CGRectMake(center.x - self.frame.size.width/2.0,
                                     center.y - self.frame.size.height/2.0,
                                     self.frame.size.width,
                                     self.frame.size.height);
    [self scrollRectToVisible:centeredRect
                     animated:animated];
}

@end
似最初 2024-08-27 10:23:42
private func centerScrollContent() {
    let x = (imageView.image!.size.width * scrollView.zoomScale / 2) - ((scrollView.bounds.width) / 2)
    let y = (imageView.image!.size.height * scrollView.zoomScale / 2) - ((scrollView.bounds.height) / 2)
    scrollView.contentOffset = CGPointMake(x, y)
}
private func centerScrollContent() {
    let x = (imageView.image!.size.width * scrollView.zoomScale / 2) - ((scrollView.bounds.width) / 2)
    let y = (imageView.image!.size.height * scrollView.zoomScale / 2) - ((scrollView.bounds.height) / 2)
    scrollView.contentOffset = CGPointMake(x, y)
}
莫言歌 2024-08-27 10:23:42

好的,已经开始工作了。这是代码,以防有人需要:

CGFloat tempy = imageView.frame.size.height;
CGFloat tempx = imageView.frame.size.width;
CGRect zoomRect = CGRectMake((tempx/2)-160, (tempy/2)-240, myScrollView.frame.size.width, myScrollView.frame.size.height);
[myScrollView scrollRectToVisible:zoomRect animated:YES];

Okay, got it working. Here's the code incase anyone is in need:

CGFloat tempy = imageView.frame.size.height;
CGFloat tempx = imageView.frame.size.width;
CGRect zoomRect = CGRectMake((tempx/2)-160, (tempy/2)-240, myScrollView.frame.size.width, myScrollView.frame.size.height);
[myScrollView scrollRectToVisible:zoomRect animated:YES];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文