简体   繁体   English

UIScrollView和UIImageView-水平移动

[英]UIScrollView and UIImageView - move horizontally

I have UIScrollView with UIImageView on it. 我有UIScrollView和UIImageView。 I set image in code and it can have different width (height is always the same). 我在代码中设置图像,它可以具有不同的宽度(高度始终相同)。 I want to do it scrollable (eg. see only part of it and drag it to left / right to see the rest). 我想使它可滚动(例如,仅查看其中一部分并将其向左/右拖动以查看其余部分)。

I have this code performed after image change: 更改图片后,我执行了以下代码:

float x = self.imageView.frame.origin.x;
float y = self.imageView.frame.origin.y;
float w = scaleImg.size.width;
float h = scaleImg.size.height;

self.imageView.frame = CGRectMake(x, y, w, h);
self.imageView.bounds = CGRectMake(0, 0, w, h);


w = self.frame.size.width;
h = self.scrollView.frame.size.height;

[self.scrollView setContentSize: CGSizeMake(w + 20, h)];

Problem is, that it didn't scroll correctly (I think it didn't scroll at all). 问题是,它没有正确滚动(我认为它根本没有滚动)。 And after I scroll, my image is resized, not respecting the frame I have set above. 滚动后,我的图像将调整大小,而不考虑上面设置的框架。

I am using iOS7, storyboard, autolayout. 我正在使用iOS7,情节提要,自动布局。

If you want the image to fit the scrollview and be scrollable left and right only you should create the image view with height same as the scrollview and width scaled accordingly to the image. 如果要使图像适合滚动视图并且只能左右滚动,则应创建高度与滚动视图相同且宽度根据图像缩放的图像视图。 Try something like this: 尝试这样的事情:

    UIImage *image;
    UIScrollView *scrollView;
    UIImageView *imageView;

    imageView = [[UIImageView alloc] initWithFrame:CGRectMake(.0f,
                                                              .0f,
                                                              scrollView.frame.size.height*image.size.width/image.size.height,
                                                              scrollView.frame.size.height)];//fixed height
    imageView.image = image;
    imageView.contentMode = UIViewContentModeScaleToFill;
    [scrollView addSubview:imageView];
    scrollView.contentSize = imageView.frame.size;
    scrollView.contentOffset = CGPointMake((scrollView.contentSize.width-scrollView.frame.size.width)*.5f, .0f);//scroll to center

Note I designed this only for images which have proportionally larger width then scrollview. 请注意,我仅针对宽度大于滚动视图的图像进行了此设计。 If that is not always the case you should create a specific logic for other images (either background is visible or it is scrollable in Y coordinate). 如果并非总是如此,则应为其他图像创建特定的逻辑(背景是可见的,或者在Y坐标中可滚动)。

I guess you should remove the following lines: 我想您应该删除以下几行:

w = self.frame.size.width;
h = self.scrollView.frame.size.height;

scrollView 's content size should reflect size of enclosed content, ie it should be scaleImg.size.width & scaleImg.size.height . scrollView的内容大小应反映包含内容的大小,即应为scaleImg.size.widthscaleImg.size.height

您将w作为滚动视图和imageview的宽度....内容大小应大于滚动视图。尝试将imageview宽度设置为大于scrollview。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM