gpt4 book ai didi

ios - UIScrollviews zoomToRect 仅设置 zoomScale 但不设置 contentOffset

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:48:58 47 4
gpt4 key购买 nike

我的 UIScrollView 在使用 zoomToRect 时没有设置它的 contentOffset

我有一个 UIScrollView,里面有一个 UIImageView。到目前为止,滚动和缩放本身是有效的。现在我想在应用程序启动时为 ScrollView 提供 ImageView 的某个缩放矩形。为此,我实现了 zoomToRect: 并正确设置了 zoomsScale,但没有设置 contentOffset

使用 zoomToRect 时的预期结果是 UIScrollView 根据选定的矩形放大或缩小,并根据原点设置其 contentOffsetzoomToRect 方法的矩形的坐标。
实际行为是它缩放到正确的 zoomScale 但我的 UIImageView 始终位于原点 0,0 而不是 x (475) 和 y ( 520) 我在 zoomToRect 中指定的矩形的坐标。
我的图片大小是 1473x1473。

代码如下

- (void)viewDidLoad {

CGRect bounds = self.view.frame;

_imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bgImage.png"]];

self.containerView = [[UIView alloc] initWithFrame:bounds];
_containerView.contentMode = UIViewContentModeCenter;

_scrollView = [[UIScrollView alloc] initWithFrame:bounds];
_scrollView.delegate = self;
_scrollView.contentSize = _imageView.bounds.size;
_scrollView.minimumZoomScale = 0.2;
[_scrollView addSubview:_containerView];

[_containerView addSubview:_imageView];

[self.view addSubview:_scrollView];
}

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[_scrollView zoomToRect:CGRectMake(475.0, 150.0, 520.0, 747.0) animated:NO];
}

#pragma mark UIScrollViewDelegate methods
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return _containerView;
}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView {

[self printVisibleRectToConsole:scrollView];

CGSize newImageViewSizeWithScale = CGSizeMake(_imageView.bounds.size.width * _scrollView.zoomScale,
_imageView.bounds.size.height * _scrollView.zoomScale);
_scrollView.contentSize = newImageViewSizeWithScale;
}

我的问题:

  • 为什么 zoomToRect 没有设置 contentOffset
  • 如何让 zoomToRect 按预期更改我的 contentOffset

最佳答案

问题是您正在缩放的​​ View (containerView) 没有它包含的 ImageView (以及您实际想要缩放的 ImageView )那么大。它的 frame 设置为 View Controller 的 frame。您看不到这一点,因为 UIView 默认情况下不会剪辑其 subview 。

您应该使用 ImageView 的边界来初始化 containerView

self.containerView = [[UIView alloc] initWithFrame:_imageView.bounds];

关于ios - UIScrollviews zoomToRect 仅设置 zoomScale 但不设置 contentOffset,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12366536/

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