UIScrollView具有多个图像水平

时间:2015-12-03 04:36:05

标签: ios uiscrollview imageview

我需要在视图中水平滚动2张图像。我已经很好地实现了单个图像滚动,但没有在scrollview中显示多个图像。我附加了单滚动的代码。以下是我需要实现的附加图像。enter image description here

galleryScrollView = [UIScrollView new];
galleryScrollView.pagingEnabled = YES;
galleryScrollView.backgroundColor = [UIColor clearColor];
galleryScrollView.translatesAutoresizingMaskIntoConstraints = NO;
galleryScrollView.showsHorizontalScrollIndicator = NO;
galleryScrollView.showsVerticalScrollIndicator = NO;
galleryScrollView.delegate = self;
galleryScrollView.scrollEnabled = YES;
[backView addSubview:galleryScrollView];

for(int i=0;i<resultDict.count;i++) {

    CGRect frame;
    frame.origin.x= galleryScrollView.frame.size.width *i;
    frame.origin.y=0;
    frame.size=galleryScrollView.frame.size;

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    imageView.translatesAutoresizingMaskIntoConstraints = YES;
    imageView.clipsToBounds = YES;

    NSString *imageString = [[resultDict valueForKey:@"ImageURL"] objectAtIndex:i];
    [imageView setImageWithURL:[NSURL URLWithString:imageString]];
    [galleryScrollView  addSubview:imageView];
}
galleryScrollView.contentSize=CGSizeMake(galleryScrollView.frame.size.width * resultDict.count, galleryScrollView.frame.size.height);

3 个答案:

答案 0 :(得分:1)

问题是您想在一个屏幕上显示两个图像,但是您为每个图像创建的帧是屏幕的大小,而原点是关闭的。要更改原点替换

frame.origin.x= galleryScrollView.frame.size.width *i;

frame.origin.x= galleryScrollView.frame.size.width *i/2;

同样,您还需要更改frame.size

答案 1 :(得分:0)

我没有看到你为你的滚动视图设置框架和uiimageview的宽度不正确,它必须

frame.origin.x= galleryScrollView.frame.size.width/2 *i

galleryScrollView.contentSize=CGSizeMake(galleryScrollView.frame.size.width/2 * resultDict.count, galleryScrollView.frame.size.height);

但是,如果您使用UICollection来展示您的产品,我认为会更好。

答案 2 :(得分:0)

enter image description here

如您所见,上面我拍摄了两张背景色为蓝色和灰色的样本图像。在滚动视图中。和编码第一部分flot当前Scroll X-position of scrollview“currScrollXPosition”

 [self.scrollView setScrollEnabled:YES];
[self.scrollView setContentSize:CGSizeMake(self.scrollView.frame.size.width*4, self.scrollView.frame.size.height)];
[self.scrollView setPagingEnabled:YES];

currScrollPosition = 0;

现在你只需对这些按钮进行操作即可 上一个按钮

- (IBAction)prevBtnClicked:(id)sender {

if(currScrollPosition > 0 ){
[self.scrollView setContentOffset:CGPointMake(currScrollPosition-self.scrollView.frame.size.width, self.scrollView.contentOffset.y)
                         animated:YES];
    currScrollPosition = currScrollPosition-self.scrollView.frame.size.width;

}}

和下一个按钮

- (IBAction)nextBtnClicked:(id)sender {

if(currScrollPosition < self.scrollView.contentSize.width){
[self.scrollView setContentOffset:CGPointMake(currScrollPosition+self.scrollView.frame.size.width, self.scrollView.contentOffset.y)
                         animated:YES];
    currScrollPosition = currScrollPosition+self.scrollView.frame.size.width;

}}
肯定会对你有用。祝你好运