UIImageView在PageControl中缩放后在右侧被切断



我的图像在放大后在右侧被切断。如果我没有用正确的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];
}

编辑:这是我的视图层次

  • 视图
    • mainScrollView
      • innerScrollFrame
        • pageScrollView
          • imageForZooming
      • innerScrollFrame
        • pageScrollView
          • imageForZooming

对于将来有同样问题的任何人,我所做的是添加以下UIScrollViewDelegate协议:

- (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);
}

我在这里找到了答案:

使图像在缩放后居中,而不是应用最初应用的偏移量。效果很好。现在,当我缩放时,我的图像不会在任何页面控件上被切断,并且我可以将图像居中于视图的开始。

相关内容

  • 没有找到相关文章

最新更新