简体   繁体   English

在ScrollView中显示图像

[英]Display images in ScrollView

I want to display some images in a scroll view, but I am facing some issues. 我想在滚动视图中显示一些图像,但是遇到一些问题。

Here is what I have: 这是我所拥有的:

- (void)viewDidLoad
{
    [super viewDidLoad];

    myObject = [[NSMutableArray alloc] init];

    [myObject addObject:@"tut_5.jpg"];
    [myObject addObject:@"tut_2.jpg"];
    [myObject addObject:@"tut_3.jpg"];
    [myObject addObject:@"tut_4.jpg"];

    pageControlBeingUsed = NO;


    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat screenWidth = screenRect.size.width;
    CGFloat screenHight = screenRect.size.height;

    for (int i = 0; i < [myObject count]; i++) {
        CGRect frame;
        frame.origin.x = self.takeTourScrollView.frame.size.width * i;
        frame.origin.y = 0;
        frame.size = self.takeTourScrollView.frame.size;

        NSString *val = [myObject objectAtIndex:i];


        UIImage* theImage = [UIImage imageNamed:val];


        UIImageView *img=[[UIImageView alloc] initWithFrame:CGRectMake(screenWidth*i,0,185 ,284)];


        img.image = theImage;

        [self.takeTourScrollView addSubview:img];

    }

The first first image seems to be ok. 第一张第一张图片似乎还不错。

在此处输入图片说明

Then when I swipe left I am getting a blank screen 然后,当我向左滑动时,我得到一个空白屏幕

在此处输入图片说明

I swipe left again and then I see my second picture. 我再次向左滑动,然后看到第二张照片。 And it goes like that for all the 4 images. 对于所有4张图像,它都是这样。

Any ideas what am I doing wrong ? 有什么想法我做错了吗?

I'd advise to use UITableView / UICollectionView for such behaviour. 我建议对这种行为使用UITableView / UICollectionView You can set the image in each cell and they'll be reused - it will be much better in terms of memory management. 您可以在每个单元格中设置图像,它们将被重用-在内存管理方面会更好。

The tableView will by the way place all elements in proper places (you just need to define the height of the cell or the layout). tableView将顺便将所有元素放置在适当的位置(您只需要定义单元格或布局的高度)即可。

It will also properly resize on events (eg phone rotation). 它还可以根据事件(例如电话旋转)正确调整大小。

Also, you should try to avoid hardcoding the sizes: 另外,您应该避免对尺寸进行硬编码:

CGRectMake(screenWidth*i,0,185 ,284)

You will fail in case of smaller / bigger devices. 如果设备较小/较大,您将失败。 It'll be a great pain to maintain the code after some time. 一段时间后维护代码会很痛苦。

-- edit -- -编辑-

After @Duncan C posted his answer, I've noticed you're looking for a paging system. @Duncan C发布答案后,我注意到您正在寻找分页系统。 You can go with building your own solution either on UIPageViewController or on UICollectionView . 您可以在UIPageViewControllerUICollectionView上构建自己的解决方案。 However you can also use third party libraries, I really enjoy this one: https://www.cocoacontrols.com/controls/bwwalkthrough . 但是您也可以使用第三方库,我真的很喜欢这一类库: https : //www.cocoacontrols.com/controls/bwwalkthrough It has a support of different animations and does a ton of stuff for you :). 它支持不同的动画,并为您做很多事情:)。

There are system components that will give you what you want with much less work and a more refined user experience. 有些系统组件将以更少的工作量和更精致的用户体验为您提供所需的内容。 Using a UICollectionView is one possibility, as mentioned by Vive in her answer. 正如Vive在回答中提到的,使用UICollectionView是一种可能性。 My suggestion would be a UIPageViewController . 我的建议是UIPageViewController There is a sample app called PhotoScroller from Apple that shows how to implement a UI very much like what you describe. 苹果有一个名为PhotoScroller的示例应用程序,该应用程序展示了如何非常像您所描述的那样实现UI。 Do a search in the Xcode docs for "PhotoScroller" to find it. 在Xcode文档中搜索“ PhotoScroller”以找到它。

you already set the x position of uiscrollview. 您已经设置了uiscrollview的x位置。 do not need to set the x postion of UIImageView.Because you used imageview as a subview of uiimagescrollview. 不需要设置UIImageView的x位置。因为您使用imageview作为uiimagescrollview的子视图。 change this line to 将此行更改为

UIImageView *img=[[UIImageView alloc] initWithFrame:CGRectMake(screenWidth*i,0,185 ,284)];

with

UIImageView *img=[[UIImageView alloc] initWithFrame:CGRectMake(0,0,185 ,284)];

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

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