【问题标题】:iPhone UIImageView in UIScrollView not zooming to centerUIScrollView中的iPhone UIImageView没有缩放到中心
【发布时间】:2011-09-20 16:24:21
【问题描述】:

我在UIScrollView 中有一个UIImageView,所以我可以允许缩放,缩放有效,但它不会使UIImageView 居中,最终会切断图像的底部并在顶部留下黑色空间.

这就是我所拥有的:

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

    scroller.bouncesZoom   = YES;
    scroller.clipsToBounds = YES;

    float scale = image.size.width/320;
    UIImage *scaled = [UIImage imageWithCGImage:[image CGImage] scale:scale orientation:UIImageOrientationUp];

    imageView = [[UIImageView alloc] initWithImage:scaled];
    imageView.userInteractionEnabled = YES;
    [imageView setCenter:CGPointMake(160,240)];

    [scroller addSubview:imageView];
    scroller.contentSize = [imageView frame].size;

    scroller.maximumZoomScale = 4.0;
    scroller.minimumZoomScale = 1.0;
    scroller.zoomScale        = 1.0;
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return imageView;
} 

有什么建议吗?

【问题讨论】:

  • 在去年的 WWDC 上有一个关于设计 Scroll Views 的演讲。看看它,它真的对我有很大帮助:)

标签: iphone uiscrollview uiimageview center zooming


【解决方案1】:

我认为this 是你想要的。

【讨论】:

  • 这就是我想要的,但我想没有覆盖就没有办法了
【解决方案2】:

如果不需要分页,则禁用分页它可以正常工作

scrollview.pagingEnabled = NO;

【讨论】:

    【解决方案3】:

    下面是我在我的应用程序中使用的代码(改编自 WWDC 示例)也许你可以从中得到一些东西:)

        CGSize imageSize =[image size];
        CGSize boundsSize = scrollview.bounds.size;
        CGRect frameToCenter = imageview.frame;
    
        // center horizontally
        if (frameToCenter.size.width < boundsSize.width)
            frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2;
        else
            frameToCenter.origin.x = 0;
    
        // center vertically
        if (frameToCenter.size.height < boundsSize.height)
            frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2;
        else
            frameToCenter.origin.y = 0;
    
        imageview.frame = frameToCenter;
    
        CGFloat xScale = boundsSize.width / imageSize.width;    // the scale needed to perfectly fit the image width-wise
        CGFloat yScale = boundsSize.height / imageSize.height;  // the scale needed to perfectly fit the image height-wise
        CGFloat minScale = MIN(xScale, yScale);                 // use minimum of these to allow the image to become fully visible
    
        // on high resolution screens we have double the pixel density, so we will be seeing every pixel if we limit the
        // maximum zoom scale to 0.5.
        CGFloat maxScale = 1.0 / [[UIScreen mainScreen] scale];
    
        // don't let minScale exceed maxScale. (If the image is smaller than the screen, we don't want to force it to be zoomed.) 
        if (minScale > maxScale) {
            minScale = maxScale;
        }
    
        scrollview.contentSize=imageSize;
        scrollview.maximumZoomScale=maxScale;
        scrollview.minimumZoomScale=minScale;
        scrollview.zoomScale=1;
    

    【讨论】:

      猜你喜欢
      • 2018-08-24
      • 2012-12-28
      • 2012-11-24
      • 1970-01-01
      • 2012-02-12
      • 2015-10-08
      • 2019-05-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多