简体   繁体   English

如何使用自动布局在scrollview中创建图像的幻灯片显示?

[英]How to create slideshow of images within scrollview with autolayout?

I have a collectionview that loads a series of cells. 我有一个加载一系列单元格的collectionview。 Some of the cells will have static images (which works well), but some might have multiple images that will need to be swipeable. 一些单元格将具有静态图像(效果很好),但是某些单元格可能具有多个图像,需要将其滑动。

在此处输入图片说明

The UICollectionViewCell is the base view, the UIScrollView is within it (pinned to the top, left, bottom, and right of its superview). UICollectionViewCell是基础视图,UIScrollView在其中(固定在其父视图的顶部,左侧,底部和右侧)。

Adding a UIImageView to the scrollview, I want to pin it to the top and left of the scrollview, but have its width and height be the same as the collection view cell, not the content size of the scrollview. 将UIImageView添加到scrollview,我想将其固定在scrollview的顶部和左侧,但使其宽度和高度与集合视图单元格相同,而不是scrollview的内容大小。

Then I'd like two additional images to be pinned to the top of the collection view cell, and the left closest view (in this case, the prior UIImageView). 然后,我想将另外两个图像固定在集合视图单元格的顶部,并固定在最左边的视图(在本例中为先前的UIImageView)。

The Storyboard would look something like this: 故事板看起来像这样:

在此处输入图片说明

(with the other images off-screen and not displayed). (其他图像不在屏幕上,并且不显示)。

I decided to add the images via code: 我决定通过代码添加图像:

There's a parent view, and a scrollview (attached to an IBOutlet). 有一个父视图和一个滚动视图(附加到IBOutlet)。

The CollectionViewCell subclass contains: CollectionViewCell子类包含:

@synthesize lbl, sv, pageControl;

- (void) awakeFromNib {
    sv.userInteractionEnabled = NO;
    sv.delegate = self;
    pageControl.userInteractionEnabled = NO;
    [self.contentView addGestureRecognizer:sv.panGestureRecognizer];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat pageWidth = sv.frame.size.width;
    float fractionalPage = sv.contentOffset.x / pageWidth;
    NSInteger page = lround(fractionalPage);
    self.pageControl.currentPage = page;
}

Then, within the View Controller's collectionView:cellForItemAtIndexPath method, I manually add images like so: 然后,在视图控制器的collectionView:cellForItemAtIndexPath方法中,我手动添加图像,如下所示:

        UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[myArray objectAtIndex:indexPath.row]]];
        img.frame = (CGRect) { 0, 0, cell.frame.size.width, cell.frame.size.height};
        [cell.sv addSubview:img];

        img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[myArray objectAtIndex:indexPath.row + 1]]];
        img.frame = (CGRect) { cell.frame.size.width, 0, cell.frame.size.width, cell.frame.size.height };
        [cell.sv addSubview:img];

        img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[myArray objectAtIndex:indexPath.row + 2]]];
        img.frame = (CGRect) { cell.frame.size.width * 2, 0, cell.frame.size.width, cell.frame.size.height };
        [cell.sv addSubview:img];

        [cell.sv setContentSize:CGSizeMake(cell.frame.size.width * 3.0, cell.frame.size.height)];

I would have preferred a StoryBoard/Interface Builder way of doing this, but for now, this will suffice. 我本来希望使用StoryBoard / Interface Builder的方式来执行此操作,但就目前而言,就足够了。

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

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