gpt4 book ai didi

ios - 缩放后 UIImageView 在 PageControl 的右侧被截断

转载 作者:行者123 更新时间:2023-11-29 10:59:45 25 4
gpt4 key购买 nike

放大后我的图像在右侧被切断。如果我不使用 cgrect make 设置偏移量,那么它不会被切断,但我希望我的图像在屏幕上居中。我怎样才能让我的图像居中并且在缩放后不切断正确的部分?

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

self.view.backgroundColor = [UIColor grayColor];

UIScrollView *mainScrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
mainScrollView.pagingEnabled = YES;
mainScrollView.showsHorizontalScrollIndicator = NO;
mainScrollView.showsVerticalScrollIndicator = NO;

CGRect innerScrollFrame = mainScrollView.bounds;

for (NSInteger i = 0; i < 2; i++) {
UIImageView *imageForZooming = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"page%d", i + 1]]];
imageForZooming.tag = VIEW_FOR_ZOOM_TAG;
imageForZooming.frame = CGRectMake(50, 0, imageForZooming.bounds.size.width, imageForZooming.bounds.size.height);

UIScrollView *pageScrollView = [[UIScrollView alloc] initWithFrame:innerScrollFrame];
pageScrollView.minimumZoomScale = 0.5f;
pageScrollView.maximumZoomScale = 1.0f;
pageScrollView.contentSize = imageForZooming.bounds.size;
pageScrollView.delegate = self;
[pageScrollView addSubview:imageForZooming];

[mainScrollView addSubview:pageScrollView];

if (i < 1) {
innerScrollFrame.origin.x += innerScrollFrame.size.width;
}

pageScrollView.zoomScale = 0.5f;
}

mainScrollView.contentSize = CGSizeMake(innerScrollFrame.origin.x + innerScrollFrame.size.width, mainScrollView.bounds.size.height);

[self.view addSubview:mainScrollView];
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return [scrollView viewWithTag:VIEW_FOR_ZOOM_TAG];
}

编辑:这是我的 View 层次结构

  • 查看
    • 主 ScrollView
      • innerScrollFrame
        • 页面 ScrollView
          • 图像缩放
      • innerScrollFrame
        • 页面 ScrollView
          • 图像缩放

最佳答案

对于以后遇到同样问题的其他人,我所做的是添加以下 UIScrollViewDelegate 协议(protocol):

- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
UIView *subView = [scrollView.subviews objectAtIndex:0];
CGFloat offsetX = (scrollView.bounds.size.width > scrollView.contentSize.width) ? (scrollView.bounds.size.width - scrollView.contentSize.width) * 0.5 : 0.0;
CGFloat offsetY = (scrollView.bounds.size.height > scrollView.contentSize.height) ? (scrollView.bounds.size.height - scrollView.contentSize.height) * 0.5 : 0.0;
subView.center = CGPointMake(scrollView.contentSize.width * 0.5 + offsetX,
scrollView.contentSize.height * 0.5 + offsetY);
}

我在这里找到了答案:Center content of UIScrollView when smaller

这样图像在缩放后会居中,而不是应用最初应用的偏移量。工作起来很有魅力。现在,当我缩放时,我的图像在任一页面控件上都不会被截断,而且我可以将图像居中显示在 View 的开头。

关于ios - 缩放后 UIImageView 在 PageControl 的右侧被截断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16468940/

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