IOS中无限滚动Scrollview效果
本文实例讲了IOS无限滚动效果,分享给大家供大家参考,具体内容如下
滑动到当前位置时候才去请求,本地有内容则直接显示(以来SDWebImage,UIView+Ext)
HZScrollView.h
#import<UIKit/UIKit.h> typedefvoid(^HZReturnBlock)(NSIntegerindex,CGFloatoffset); typedefNS_ENUM(NSUInteger,HZScrollViewPageControllPosition){ HZScrollViewPageControllPositionNone, HZScrollViewPageControllPositionLeft, HZScrollViewPageControllPositionCenter, HZScrollViewPageControllPositionRight, }; @classHZScrollView; @protocolHZScrollViewDelegate<NSObject> @optional -(void)resetPosition:(HZScrollView*)scrollViewAndIndex:(NSInteger)index; @end @interfaceHZScrollView:UIView /** *返回当前位置 */ @property(nonatomic,weak)id<HZScrollViewDelegate>delegate; /** *默认图 */ @property(nonatomic,strong)UIImage*hz_placeImage; /** *图片列表哇 */ @property(nonatomic,strong)NSArray*hz_ImageList; /** *pagecongroll位置 */ @property(nonatomic,assign)HZScrollViewPageControllPositionhz_PageControllPosition; /** *变换自身frame * *@paramoffset偏移量 */ -(void)transformView:(CGFloat)offset; /** *获取当前位置以及便宜 * *@paramblock返回内容 */ -(void)hz_getContent:(HZReturnBlock)block; @end
HZScrollView.m
#import"UIImageView+WebCache.h" #import"HZScrollView.h" #import"UIView+DylanFramTool.h" #defineHZ_FormatImage(val_imageView)\ -(UIImageView*)val_imageView\ {\ if(!_##val_imageView){\ _##val_imageView=[[UIImageViewalloc]init];\ }\ return_##val_imageView;\ }\ //宽度 #defineHZ_SWidthself.bounds.size.width //高度 #defineHZ_SHeightself.bounds.size.height #defineHZ_PHeight16 @interfaceHZScrollView()<UIScrollViewDelegate> @property(nonatomic,copy)HZReturnBlockhz_block; /** *总数 */ @property(nonatomic,assign)NSIntegerhz_MaxCount; /** *当前位置 */ @property(nonatomic,assign)NSIntegerhz_currentIndex; /** *容器 */ @property(nonatomic,strong)UIScrollView*scrollView; /** *你懂滴 */ @property(nonatomic,strong)UIPageControl*pageControll; /**视图**/ @property(nonatomic,strong)UIImageView*leftImageView; @property(nonatomic,strong)UIImageView*centerImageView; @property(nonatomic,strong)UIImageView*rightImageView; @end @implementationHZScrollView { CGRect_hz_Frame; } #pragmamark- #pragmamark-init -(instancetype)initWithFrame:(CGRect)frame { self=[superinitWithFrame:frame]; if(self){ _hz_Frame=frame; [selfaddSubview:self.scrollView]; [selfconfigImageView]; [selfaddSubview:self.pageControll]; } returnself; } #pragmamark- #pragmamark-config -(void)configImageView { self.leftImageView.frame=CGRectMake(0,0,HZ_SWidth,HZ_SHeight); [self.scrollViewaddSubview:self.leftImageView]; self.centerImageView.frame=CGRectMake(HZ_SWidth,0,HZ_SWidth,HZ_SHeight); [self.scrollViewaddSubview:self.centerImageView]; self.rightImageView.frame=CGRectMake(HZ_SWidth*2,0,HZ_SWidth,HZ_SHeight); [self.scrollViewaddSubview:self.rightImageView]; } #pragmamark- #pragmamark-reSet -(void)setHz_placeImage:(UIImage*)hz_placeImage { _hz_placeImage=hz_placeImage; [selfchangeImageLeft:-1center:-1right:-1]; if(self.hz_MaxCount){ [selfsetHz_MaxCount:self.hz_ImageList.count]; } } -(void)setHz_ImageList:(NSArray*)hz_ImageList { _hz_ImageList=[hz_ImageListcopy]; [selfsetHz_MaxCount:_hz_ImageList.count]; } -(void)setHz_MaxCount:(NSInteger)hz_MaxCount { _hz_MaxCount=hz_MaxCount; switch(_hz_MaxCount){ case0: self.scrollView.scrollEnabled=NO; [selfchangeImageLeft:-1center:-1right:-1]; break; case1: self.scrollView.scrollEnabled=NO; [selfchangeImageLeft:0center:0right:0]; break; default: self.scrollView.scrollEnabled=YES; [selfchangeImageLeft:_hz_MaxCount-1center:0right:1]; break; } self.pageControll.numberOfPages=_hz_MaxCount; [selfsetHz_PageControllPosition:_hz_PageControllPosition]; } -(void)setHz_PageControllPosition:(HZScrollViewPageControllPosition)hz_PageControllPosition { _hz_PageControllPosition=hz_PageControllPosition; CGFloatwidth=self.hz_MaxCount*HZ_PHeight; switch(_hz_PageControllPosition){ caseHZScrollViewPageControllPositionNone: self.pageControll.hidden=YES; break; caseHZScrollViewPageControllPositionLeft: self.pageControll.hidden=NO; self.pageControll.frame=CGRectMake(10,self.pageControll.frame.origin.y,width,self.pageControll.frame.size.height); break; caseHZScrollViewPageControllPositionCenter: self.pageControll.hidden=NO; self.pageControll.frame=CGRectMake((self.bounds.size.width-width)/2.f,self.pageControll.frame.origin.y,width,self.pageControll.frame.size.height); break; caseHZScrollViewPageControllPositionRight: self.pageControll.hidden=NO; self.pageControll.frame=CGRectMake(self.bounds.size.width-10-width,self.pageControll.frame.origin.y,width,self.pageControll.frame.size.height); break; default: break; } if(width<=HZ_PHeight){ self.pageControll.hidden=YES; } } #pragmamark- #pragmamark-LZ -(UIScrollView*)scrollView { if(!_scrollView){ _scrollView=[[UIScrollViewalloc]initWithFrame:self.bounds]; _scrollView.pagingEnabled=YES; _scrollView.showsHorizontalScrollIndicator=NO; _scrollView.delegate=self; _scrollView.contentSize=CGSizeMake(HZ_SWidth*3,0); } return_scrollView; } -(UIPageControl*)pageControll { if(!_pageControll){ _pageControll=[[UIPageControlalloc]initWithFrame:CGRectMake(0,HZ_SHeight-HZ_PHeight,HZ_PHeight,7)]; _pageControll.pageIndicatorTintColor=[UIColorlightGrayColor]; _pageControll.currentPageIndicatorTintColor=[UIColorwhiteColor]; _pageControll.numberOfPages=self.hz_MaxCount; _pageControll.currentPage=0; } return_pageControll; } HZ_FormatImage(leftImageView); HZ_FormatImage(centerImageView); HZ_FormatImage(rightImageView); #pragmamark- #pragmamark-privateMethod -(void)changeImageWithOffset:(CGFloat)offsetX { if(offsetX>=HZ_SWidth*2){ self.hz_currentIndex++; if(self.hz_currentIndex==self.hz_MaxCount-1){ [selfchangeImageLeft:self.hz_currentIndex-1center:self.hz_currentIndexright:0]; }elseif(self.hz_currentIndex==self.hz_MaxCount){ self.hz_currentIndex=0; [selfchangeImageLeft:self.hz_MaxCount-1center:0right:1]; }else{ [selfchangeImageLeft:self.hz_currentIndex-1center:self.hz_currentIndexright:self.hz_currentIndex+1]; } if(self.hz_block){ self.hz_block(self.hz_currentIndex,offsetX); } self.pageControll.currentPage=self.hz_currentIndex; } if(offsetX<=0){ self.hz_currentIndex--; if(self.hz_currentIndex==0){ [selfchangeImageLeft:self.hz_MaxCount-1center:0right:1]; }elseif(self.hz_currentIndex==-1){ self.hz_currentIndex=self.hz_MaxCount-1; [selfchangeImageLeft:self.hz_currentIndex-1center:self.hz_currentIndexright:0]; }else{ [selfchangeImageLeft:self.hz_currentIndex-1center:self.hz_currentIndexright:self.hz_currentIndex+1]; } if(self.hz_block){ self.hz_block(self.hz_currentIndex,offsetX); } self.pageControll.currentPage=self.hz_currentIndex; } [selfsetHz_PageControllPosition:_hz_PageControllPosition]; } -(void)changeImageLeft:(NSInteger)leftIndexcenter:(NSInteger)centerIndexright:(NSInteger)rightIndex { if(self.hz_currentIndex>self.hz_MaxCount){ return; } if(leftIndex==-1&¢erIndex==-1&&rightIndex==-1){ self.leftImageView.image=self.hz_placeImage; self.centerImageView.image=self.hz_placeImage; self.rightImageView.image=self.hz_placeImage; }else{ [selfcheckExistImage:self.hz_ImageList[leftIndex] ImageView:self.leftImageView CurrentIndex:@(leftIndex)]; [selfcheckExistImage:self.hz_ImageList[centerIndex] ImageView:self.centerImageView CurrentIndex:@(centerIndex)]; [selfcheckExistImage:self.hz_ImageList[rightIndex] ImageView:self.rightImageView CurrentIndex:@(rightIndex)]; } [self.scrollViewsetContentOffset:CGPointMake(HZ_SWidth,0)]; } -(void)checkExistImage:(NSString*)urlString ImageView:(UIImageView*)currentImageView CurrentIndex:(NSNumber*)index { if([[[SDWebImageManagersharedManager]imageCache]imageFromDiskCacheForKey:urlString]) { currentImageView.image=[[[SDWebImageManagersharedManager]imageCache]imageFromMemoryCacheForKey:urlString]; return; }else{ currentImageView.image=self.hz_placeImage; if(self.hz_currentIndex!=[indexintegerValue]){ return; } [selfperformSelector:@selector(downLoadImage:)withObject:@[urlString,currentImageView]afterDelay:0inModes:@[NSDefaultRunLoopMode]]; } } -(void)downLoadImage:(NSArray*)param { NSString*urlString=[paramfirstObject]; __weakUIImageView*currentImageView=[paramlastObject]; [[SDWebImageManagersharedManager]downloadImageWithURL:[NSURLURLWithString:urlString]options:0progress:^(NSIntegerreceivedSize,NSIntegerexpectedSize){ NSLog(@"received:%@",@(receivedSize)); }completed:^(UIImage*image,NSError*error,SDImageCacheTypecacheType,BOOLfinished,NSURL*imageURL){ currentImageView.image=image; //[[[SDWebImageManagersharedManager]imageCache]storeImage:imageforKey:urlString]; [[[SDWebImageManagersharedManager]imageCache]storeImage:imageforKey:urlStringtoDisk:YES]; }]; } #pragmamark- #pragmamark-UIScrollViewDelegate -(void)scrollViewDidScroll:(UIScrollView*)scrollView { [selfchangeImageWithOffset:scrollView.contentOffset.x]; } -(void)scrollViewDidEndDecelerating:(UIScrollView*)scrollView { NSIntegerindex=scrollView.contentOffset.x/scrollView.width; if([self.delegaterespondsToSelector:@selector(resetPosition:AndIndex:)]){ [self.delegateresetPosition:selfAndIndex:self.hz_currentIndex]; } } #pragmamark- #pragmamark-publicmethod -(void)transformView:(CGFloat)offset { if(offset>0){ return; } CGFloatcurrentHeight=210-offset; self.height=currentHeight; CGFloatcurrentScale=currentHeight/_hz_Frame.size.height; self.left=_hz_Frame.origin.x-(_hz_Frame.size.width*currentScale-_hz_Frame.size.width)/2.f; self.width=_hz_Frame.size.width*currentScale; self.top=offset; self.scrollView.size=self.size; self.leftImageView.size=self.size; self.centerImageView.size=self.size; self.rightImageView.size=self.size; self.centerImageView.x=self.width; self.rightImageView.x=self.width*2; self.scrollView.contentSize=CGSizeMake(self.width*3,self.height); [self.scrollViewsetContentOffset:CGPointMake(HZ_SWidth,0)]; } -(void)hz_getContent:(HZReturnBlock)block { self.hz_block=block; } -(void)layoutSubviews { [superlayoutSubviews]; } @end
以上就是本文的全部内容,希望对大家的学习有所帮助。