Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 缩小时将UIImageView居中显示在屏幕上_Iphone_Uiscrollview_Uiimageview_Zooming - Fatal编程技术网

Iphone 缩小时将UIImageView居中显示在屏幕上

Iphone 缩小时将UIImageView居中显示在屏幕上,iphone,uiscrollview,uiimageview,zooming,Iphone,Uiscrollview,Uiimageview,Zooming,我在UIScrollView中有一个UIImageView。我希望用户可以缩放和导航图像 这是我的工作代码: //img is the UIImageView //scroller is the UIScrollView - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView { return img; } - (void)viewDidLoad { [super viewDidLoad]; UIImag

我在UIScrollView中有一个UIImageView。我希望用户可以缩放和导航图像

这是我的工作代码:

//img is the UIImageView
//scroller is the UIScrollView

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return img; 
}
- (void)viewDidLoad {
   [super viewDidLoad];
   UIImage *image = [UIImage imageNamed:@"map_screen.png"];
   img = [[UIImageView alloc] initWithImage:image];
   scroller.delegate = self;
   scroller.autoresizesSubviews = YES;
   scroller.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
   scroller.contentSize = img.frame.size;
   scroller.scrollEnabled = YES;
   scroller.directionalLockEnabled = NO;
   scroller.userInteractionEnabled = YES;

   CGSize ivsize = img.frame.size;
   CGSize ssize = scroller.frame.size;

   float scalex = ssize.width / ivsize.width;
   float scaley = ssize.height / ivsize.height;
   scroller.minimumZoomScale = fmin(1.0, fmax(scaley, scalex));
   scroller.zoomScale = fmin(1.0, fmax(scalex, scaley));
   [scroller addSubview:img];
   img.userInteractionEnabled = YES;
}
所有的工作,但这发生了:最小的缩放是屏幕的高度。 我的图像的宽度大于高度,因此我希望最小缩放是宽度

如果我写

scroller.minimumZoomScale = fmin(1.0, scalex);
工作正常,但当用户缩小时,图像不在屏幕中央,而是在顶部

我试过这样的东西

CGPoint scrollCenter = [scroller center];
[img setCenter:CGPointMake(scrollCenter.x, scrollCenter.y)];

但是在这个解决方案中,图像不是完全可滚动的,如果我缩小,它会再次停留在屏幕顶部,但不是完全可见的


我能做些什么来修复它?

当正在使用scrollViewDidZoom代理功能进行缩放时,您必须手动执行此操作。。。差不多

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

    // center the image as it becomes smaller than the size of the screen
    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;

}
-(无效)scrollViewDidZoom:(UIScrollView*)scrollView
{
//当图像小于屏幕尺寸时,将图像居中
CGSize boundsSize=scrollView.bounds.size;
CGRect frameToCenter=imageView.frame;
//水平居中
if(frameToCenter.size.width
巴斯蒂安答案的Swift 3.0版,更简洁一点:

func scrollViewDidZoom(_ scrollView: UIScrollView) {
    // center the image as it becomes smaller than the size of the screen
    let boundsSize    = scrollView.bounds.size
    var frameToCenter = imageView.frame

    // center horizontally and vertically
    let widthDiff  = boundsSize.width  - frameToCenter.size.width
    let heightDiff = boundsSize.height - frameToCenter.size.height
    frameToCenter.origin.x = (widthDiff  > 0) ? widthDiff  / 2 : 0;
    frameToCenter.origin.y = (heightDiff > 0) ? heightDiff / 2 : 0;

    imageView.frame = frameToCenter;
}
func scrollViewDidZoom(_ scrollView: UIScrollView) {
    // center the image as it becomes smaller than the size of the screen
    let boundsSize    = scrollView.bounds.size
    var frameToCenter = imageView.frame

    // center horizontally and vertically
    let widthDiff  = boundsSize.width  - frameToCenter.size.width
    let heightDiff = boundsSize.height - frameToCenter.size.height
    frameToCenter.origin.x = (widthDiff  > 0) ? widthDiff  / 2 : 0;
    frameToCenter.origin.y = (heightDiff > 0) ? heightDiff / 2 : 0;

    imageView.frame = frameToCenter;
}