UIScrollView创建相册

1.设置滚动相册

1.1将存放图片数组传过来,及当前图片的索引

1.2在控制器中创建ScrollView,设置它的contentSize,contentOffset.

1.3通过传过来的图片数组创建UIImageView并将每个UIImageView添加到ScrollView,ScrollView的ConSize是图片数组的总数乘以每个UIImageVIew的宽度.

1.4滚动图片设置图片当前索引及总数.

1.5删除图片操作

1.51当删除图片的索引>0时,设置图片的偏移量是当前图片的索引-1*当前UIImageView的宽度,

_imageScrollView.contentOffset = CGPointMake((self.currImageTag-1)*_imageScrollView.bounds.size.width, 0);

1.52当删除图片的索引= 0时,设置图片的偏移是是当前图片的索引*当前UIImageView的宽度,

_imageScrollView.contentOffset = CGPointMake((self.currImageTag)*_imageScrollView.bounds.size.width, 0);

1.6当删除其中一个图片后,刷新视图重新设置当前的图片的索引及数组的总数,如果是列表形式的,按照九宫格方式重新布局.

代码如下:

CGRect frame = self.view.bounds;
    frame.origin.x -= gap;
    frame.size.width += (2* gap);
    _imageScrollView = [[UIScrollView alloc] initWithFrame:frame];
    _imageScrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    ;
    // 设置伸缩比例
    _imageScrollView.maximumZoomScale = 2.0;
    _imageScrollView.minimumZoomScale = 1.0;
    
    _imageScrollView.pagingEnabled = YES;
    _imageScrollView.delegate = self;
    _imageScrollView.showsHorizontalScrollIndicator = NO;
    _imageScrollView.showsVerticalScrollIndicator = NO;
    _imageScrollView.backgroundColor = [UIColor clearColor];
    
    if (self.imageArray && self.imageArray.count){
        _imageScrollView.contentSize = CGSizeMake(frame.size.width *self.imageArray.count, 0);
        
        for (NSInteger i = 0; i <self.imageArray.count; i++) {
            _imageView = [[UIImageView alloc] init];
            CGRect bounds = _imageScrollView.bounds;
            _imageView.frame = CGRectMake(i*bounds.size.width,0, bounds.size.width-gap, bounds.size.height);
            UIImage *image = self.imageArray[i];
            _imageView.userInteractionEnabled = YES;
            _imageView.tag = i+200;
            if (image && [image isKindOfClass:[UIImage class]]) {
                _imageView.image = image;
            }
            [_imageScrollView addSubview:_imageView];
        }
    }
    
    [self.view addSubview:_imageScrollView];
    _imageScrollView.contentOffset = CGPointMake(self.currImageTag*frame.size.width, 0);

 

你可能感兴趣的:(UIScrollView创建相册)