#import
@interfacehanoutScrollview :UIScrollView
@property(strong,nonatomic)NSArray*imgArr;
@end
#import"hanoutScrollview.h"
@interfacehanoutScrollview(){
}
/**
*保存可见的视图
*/
@property(nonatomic,strong)NSMutableSet*visibleImageViews;
/**
*保存可重用的
*/
@property(nonatomic,strong)NSMutableSet*reusedImageViews;
@end
@implementationhanoutScrollview
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
-(instancetype)initWithFrame:(CGRect)frame{
self= [superinitWithFrame:frame];
if(self) {
self.pagingEnabled=YES;
self.bounces=false;
self.delegate=self;
self.showsVerticalScrollIndicator=false;
self.showsHorizontalScrollIndicator=false;
}
returnself;
}
-(void)setImgArr:(NSArray*)imgArr{
self.contentSize=CGSizeMake(self.bounds.size.width*imgArr.count,self.bounds.size.height);
_imgArr= imgArr;
[selfshowImageViewAtIndex:0];
}
#pragma mark -- UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView*)scrollView {
[selfshowImages];
}
#pragma mark - Private Method
- (void)showImages {
//获取当前处于显示范围内的图片的索引
CGRectvisibleBounds =self.bounds;
CGFloatminX =CGRectGetMinX(visibleBounds);
CGFloatmaxX =CGRectGetMaxX(visibleBounds);
CGFloatwidth =CGRectGetWidth(visibleBounds);
NSIntegerfirstIndex = (NSInteger)floorf(minX / width);
NSIntegerlastIndex = (NSInteger)floorf(maxX / width);
//处理越界的情况
if(firstIndex < 0) {
firstIndex = 0;
}
if(lastIndex >= [_imgArrcount]) {
lastIndex = [_imgArrcount] - 1;
}
//回收不再显示的ImageView
NSIntegerimageViewIndex = 0;
for(UIImageView*imageViewinself.visibleImageViews) {
imageViewIndex = imageView.tag;
//不在显示范围内
if(imageViewIndex < firstIndex || imageViewIndex > lastIndex) {
[self.reusedImageViewsaddObject:imageView];
[imageViewremoveFromSuperview];
}
}
[self.visibleImageViewsminusSet:self.reusedImageViews];
//是否需要显示新的视图
for(NSIntegerindex = firstIndex; index <= lastIndex; index++) {
BOOLisShow =NO;
for(UIImageView*imageViewinself.visibleImageViews) {
if(imageView.tag== index) {
isShow =YES;
}
}
if(!isShow) {
[selfshowImageViewAtIndex:index];
}
}
}
//显示一个图片view
- (void)showImageViewAtIndex:(NSInteger)index {
UIImageView*imageView = [self.reusedImageViewsanyObject];
if(imageView) {
[self.reusedImageViewsremoveObject:imageView];
}else{
imageView = [[UIImageViewalloc]init];
imageView.contentMode=UIViewContentModeScaleAspectFill;
}
CGRectbounds =self.bounds;
CGRectimageViewFrame = bounds;
imageViewFrame.origin.x=CGRectGetWidth(bounds) * index;
imageView.tag= index;
imageView.frame= imageViewFrame;
[imageViewsd_setImageWithURL:[NSURLURLWithString:_imgArr[index]]placeholderImage:[UIImageimageNamed:@"placeholder"]];
[self.visibleImageViewsaddObject:imageView];
[selfaddSubview:imageView];
}
#pragma mark - Getters and Setters
- (NSMutableSet*)visibleImageViews {
if(_visibleImageViews==nil) {
_visibleImageViews= [[NSMutableSetalloc]init];
}
return_visibleImageViews;
}
- (NSMutableSet*)reusedImageViews {
if(_reusedImageViews==nil) {
_reusedImageViews= [[NSMutableSetalloc]init];
}
return_reusedImageViews;
}
@end