UIScrolview为子视图添加了一些额外的空间

时间:2019-07-16 17:30:47

标签: ios objective-c uiscrollview slider

我正在尝试使用UIScrollView创建动态图像滑块。因此,在我搜索到预期的页面之前,我先发送需要的图像,然后将scrollview设置为将那些图像作为子视图,并在5秒钟的NSTimer中自动滚动浏览它们直到结束,然后返回到开始。

当Scrollview第一次浏览图像时,会出现问题,它显示图像+下一张图像的一部分。然后,当滑动到下一张图像时,它将显示3ed图像的一部分..ect

一旦滚动视图第一次遍历所有图像,然后返回第一幅图像,一切都很好,并且所有图像的大小都正确。

我尽我所能想到的一切,但是没有运气!也许我对此工作使用了错误的观点?我不知道..

这是我用来配置uiscrolview的功能(在ViewDidlayout中调用):

scrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.scrolSV.frame.size.height)];
    scrollview.delegate = self;

    [scrollview setPagingEnabled:YES];
    [scrollview setAlwaysBounceHorizontal:NO];
    [scrollview setShowsVerticalScrollIndicator:NO];
    [scrollview setShowsHorizontalScrollIndicator:NO];
    scrollview.contentInset = UIEdgeInsetsZero;
    scrollview.clipsToBounds= YES;

    CGFloat xOrigin ;

    NSArray *  tempList = [self.sightseeing.images allObjects];

    NSSortDescriptor *  sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"image_counter"
                                                 ascending:YES];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self.is_image = %@", @(YES)];
    ImagesList = [tempList filteredArrayUsingPredicate:predicate];
    ImagesList = [ImagesList sortedArrayUsingDescriptors:@[sortDescriptor]];
    self.paging.numberOfPages = [ImagesList count];
    selectedOffset = 0 ;

    for (int i = 0 ; i< [ImagesList count]; i++){

        xOrigin = self.view.frame.size.width * i ;


        Images * object = [ImagesList objectAtIndex:i];
        UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 0, scrollview.frame.size.width, scrollview.frame.size.height)];
        imageView.autoresizingMask = NO ;

        if (object.image.length > 5) {
            imageView.image = [UIImage imageWithData:object.image];
        }
        else
        {
            imageView.image = [UIImage imageNamed:Image_Placeholder];
        }


        [imageView setContentMode:UIViewContentModeScaleAspectFill];
        [scrollview addSubview:imageView];

    }



    [scrollview setContentOffset:CGPointMake(0, 0)];
    [scrollview setContentSize:CGSizeMake(self.scrolSV.frame.size.width *  [ImagesList count] , self.scrolSV.frame.size.height)];
    [self.scrolSV addSubview:scrollview];

这是ScrolDidScrol方法:


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

    [imageTimer invalidate];
    if (![imageTimer isValid]) {
        imageTimer = [NSTimer scheduledTimerWithTimeInterval:Timer_Length target:self selector:@selector(updateImages) userInfo:nil repeats:YES];
    }
    int offset = (int)scrollview.contentOffset.x /scrollView.frame.size.width;
    self.paging.currentPage = offset;
    if (selectedOffset != offset) {
        selectedOffset = offset;
        UIImageView * imageV= [scrollView.subviews objectAtIndex:offset];

        Images * image = [ImagesList objectAtIndex:offset];

        if (![image.image length]) {

            [QBRequest downloadFileFromClassName:Sightseeing_Table_Name objectID:self.sightseeing.object_id fileFieldName:image.field_id
                                    successBlock:^(QBResponse *response, NSData *loadedData) {
                                        if (loadedData.length > 100) {
                                            image.image = loadedData;
                                            imageV.image = [UIImage imageWithData:image.image];
                                            [imageV setContentMode:UIViewContentModeScaleAspectFill];
                                        }

                                    } statusBlock:^(QBRequest *request, QBRequestStatus *status) {

                                    } errorBlock:^(QBResponse *error) {

                                    }];
        }
    }


}

这是初次查看enter image description here

时的图像

浏览图像后,它显示如下: enter image description here

1 个答案:

答案 0 :(得分:0)

几年前,我在Ventura County Animal Services(VCAS)移动应用程序中实现了类似的操作(仍可免费下载)。我使用了Grant Davis的名为FGallery的库。我不知道它是否仍然可用,但是他们做得很好,包括支持缩略图和全尺寸图像查看,加载指示符,在实际图像尚未完成下载时显示占位符图像的功能等。如果可以的话,我会为自己看一下。也许您只需要借用他们的一些技术,或者仅使用他们已完成的工作即可。很快,我的雇主对此要求很高。

https://github.com/gdavis/FGallery-iPhone