IOS中无限滚动Scrollview效果
程序员文章站
2023-11-22 08:38:52
本文实例讲了ios无限滚动效果,分享给大家供大家参考,具体内容如下
滑动到当前位置时候才去请求,本地有内容则直接显示(以来sdwebimage,uiview+ext)...
本文实例讲了ios无限滚动效果,分享给大家供大家参考,具体内容如下
滑动到当前位置时候才去请求,本地有内容则直接显示(以来sdwebimage,uiview+ext)
hzscrollview.h
#import <uikit/uikit.h> typedef void(^hzreturnblock)(nsinteger index,cgfloat offset); typedef ns_enum(nsuinteger, hzscrollviewpagecontrollposition) { hzscrollviewpagecontrollpositionnone, hzscrollviewpagecontrollpositionleft, hzscrollviewpagecontrollpositioncenter, hzscrollviewpagecontrollpositionright, }; @class hzscrollview; @protocol hzscrollviewdelegate <nsobject> @optional - (void)resetposition:(hzscrollview *)scrollview andindex:(nsinteger)index; @end @interface hzscrollview : uiview /** * 返回当前位置 */ @property (nonatomic, weak)id <hzscrollviewdelegate>delegate; /** * 默认图 */ @property (nonatomic, strong) uiimage *hz_placeimage; /** * 图片列表哇 */ @property (nonatomic, strong) nsarray *hz_imagelist; /** * pagecongroll 位置 */ @property (nonatomic, assign) hzscrollviewpagecontrollposition hz_pagecontrollposition; /** * 变换自身frame * * @param offset 偏移量 */ - (void)transformview:(cgfloat)offset; /** * 获取当前位置以及便宜 * * @param block 返回内容 */ - (void)hz_getcontent:(hzreturnblock)block; @end
hzscrollview.m
#import "uiimageview+webcache.h" #import "hzscrollview.h" #import "uiview+dylanframtool.h" #define hz_formatimage(val_imageview)\ - (uiimageview *)val_imageview\ {\ if (!_##val_imageview) {\ _##val_imageview = [[uiimageview alloc] init];\ }\ return _##val_imageview;\ }\ //宽度 #define hz_swidth self.bounds.size.width //高度 #define hz_sheight self.bounds.size.height #define hz_pheight 16 @interface hzscrollview ()<uiscrollviewdelegate> @property (nonatomic, copy) hzreturnblock hz_block; /** * 总数 */ @property (nonatomic,assign) nsinteger hz_maxcount; /** * 当前位置 */ @property (nonatomic,assign) nsinteger hz_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 @implementation hzscrollview { cgrect _hz_frame; } #pragma mark - #pragma mark - init - (instancetype)initwithframe:(cgrect)frame { self = [super initwithframe:frame]; if (self) { _hz_frame = frame; [self addsubview:self.scrollview]; [self configimageview]; [self addsubview:self.pagecontroll]; } return self; } #pragma mark - #pragma mark - config - (void)configimageview { self.leftimageview.frame = cgrectmake(0, 0, hz_swidth, hz_sheight); [self.scrollview addsubview:self.leftimageview]; self.centerimageview.frame = cgrectmake(hz_swidth, 0, hz_swidth, hz_sheight); [self.scrollview addsubview:self.centerimageview]; self.rightimageview.frame = cgrectmake(hz_swidth * 2, 0, hz_swidth, hz_sheight); [self.scrollview addsubview:self.rightimageview]; } #pragma mark - #pragma mark - reset - (void)sethz_placeimage:(uiimage *)hz_placeimage { _hz_placeimage = hz_placeimage; [self changeimageleft:-1 center:-1 right:-1]; if (self.hz_maxcount) { [self sethz_maxcount:self.hz_imagelist.count]; } } - (void)sethz_imagelist:(nsarray *)hz_imagelist { _hz_imagelist = [hz_imagelist copy]; [self sethz_maxcount:_hz_imagelist.count]; } - (void)sethz_maxcount:(nsinteger)hz_maxcount { _hz_maxcount = hz_maxcount; switch (_hz_maxcount) { case 0: self.scrollview.scrollenabled = no; [self changeimageleft:-1 center:-1 right:-1]; break; case 1: self.scrollview.scrollenabled = no; [self changeimageleft:0 center:0 right:0]; break; default: self.scrollview.scrollenabled = yes; [self changeimageleft:_hz_maxcount - 1 center:0 right:1]; break; } self.pagecontroll.numberofpages = _hz_maxcount; [self sethz_pagecontrollposition:_hz_pagecontrollposition]; } - (void)sethz_pagecontrollposition:(hzscrollviewpagecontrollposition)hz_pagecontrollposition { _hz_pagecontrollposition = hz_pagecontrollposition; cgfloat width = self.hz_maxcount * hz_pheight; switch (_hz_pagecontrollposition) { case hzscrollviewpagecontrollpositionnone: self.pagecontroll.hidden = yes; break; case hzscrollviewpagecontrollpositionleft: self.pagecontroll.hidden = no; self.pagecontroll.frame = cgrectmake(10, self.pagecontroll.frame.origin.y, width, self.pagecontroll.frame.size.height); break; case hzscrollviewpagecontrollpositioncenter: 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; case hzscrollviewpagecontrollpositionright: 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; } } #pragma mark - #pragma mark - lz - (uiscrollview *)scrollview { if (!_scrollview) { _scrollview = [[uiscrollview alloc] 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 = [[uipagecontrol alloc] initwithframe:cgrectmake(0,hz_sheight - hz_pheight,hz_pheight, 7)]; _pagecontroll.pageindicatortintcolor = [uicolor lightgraycolor]; _pagecontroll.currentpageindicatortintcolor = [uicolor whitecolor]; _pagecontroll.numberofpages = self.hz_maxcount; _pagecontroll.currentpage = 0; } return _pagecontroll; } hz_formatimage(leftimageview); hz_formatimage(centerimageview); hz_formatimage(rightimageview); #pragma mark - #pragma mark - private method - (void)changeimagewithoffset:(cgfloat)offsetx { if (offsetx >= hz_swidth * 2) { self.hz_currentindex++; if (self.hz_currentindex == self.hz_maxcount - 1) { [self changeimageleft:self.hz_currentindex - 1 center:self.hz_currentindex right:0]; }else if (self.hz_currentindex == self.hz_maxcount) { self.hz_currentindex = 0; [self changeimageleft:self.hz_maxcount - 1 center:0 right:1]; }else { [self changeimageleft:self.hz_currentindex-1 center:self.hz_currentindex right: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) { [self changeimageleft:self.hz_maxcount-1 center:0 right:1]; }else if (self.hz_currentindex == -1) { self.hz_currentindex = self.hz_maxcount-1; [self changeimageleft:self.hz_currentindex-1 center:self.hz_currentindex right:0]; }else { [self changeimageleft:self.hz_currentindex-1 center:self.hz_currentindex right:self.hz_currentindex+1]; } if (self.hz_block) { self.hz_block(self.hz_currentindex,offsetx); } self.pagecontroll.currentpage = self.hz_currentindex; } [self sethz_pagecontrollposition:_hz_pagecontrollposition]; } - (void)changeimageleft:(nsinteger)leftindex center:(nsinteger)centerindex right:(nsinteger)rightindex { if (self.hz_currentindex > self.hz_maxcount) { return; } if (leftindex == -1 && centerindex == -1 && rightindex == -1) { self.leftimageview.image = self.hz_placeimage; self.centerimageview.image = self.hz_placeimage; self.rightimageview.image = self.hz_placeimage; } else { [self checkexistimage:self.hz_imagelist[leftindex] imageview:self.leftimageview currentindex:@(leftindex)]; [self checkexistimage:self.hz_imagelist[centerindex] imageview:self.centerimageview currentindex:@(centerindex)] ; [self checkexistimage:self.hz_imagelist[rightindex] imageview:self.rightimageview currentindex:@(rightindex)]; } [self.scrollview setcontentoffset:cgpointmake(hz_swidth, 0)]; } - (void)checkexistimage:(nsstring *)urlstring imageview:(uiimageview *)currentimageview currentindex:(nsnumber *)index { if ([[[sdwebimagemanager sharedmanager] imagecache] imagefromdiskcacheforkey:urlstring]) { currentimageview.image = [[[sdwebimagemanager sharedmanager] imagecache] imagefrommemorycacheforkey:urlstring]; return; } else { currentimageview.image = self.hz_placeimage; if (self.hz_currentindex != [index integervalue]) { return; } [self performselector:@selector(downloadimage:) withobject:@[urlstring,currentimageview] afterdelay:0 inmodes:@[nsdefaultrunloopmode]]; } } - (void)downloadimage:(nsarray *)param { nsstring *urlstring = [param firstobject]; __weak uiimageview *currentimageview = [param lastobject]; [[sdwebimagemanager sharedmanager] downloadimagewithurl:[nsurl urlwithstring:urlstring] options:0 progress:^(nsinteger receivedsize, nsinteger expectedsize) { nslog(@"received:%@",@(receivedsize)); } completed:^(uiimage *image, nserror *error, sdimagecachetype cachetype, bool finished, nsurl *imageurl) { currentimageview.image = image; // [[[sdwebimagemanager sharedmanager] imagecache] storeimage:image forkey:urlstring]; [[[sdwebimagemanager sharedmanager] imagecache] storeimage:image forkey:urlstring todisk:yes]; }]; } #pragma mark - #pragma mark - uiscrollviewdelegate - (void)scrollviewdidscroll:(uiscrollview *)scrollview { [self changeimagewithoffset:scrollview.contentoffset.x]; } - (void)scrollviewdidenddecelerating:(uiscrollview *)scrollview { nsinteger index = scrollview.contentoffset.x/scrollview.width; if ([self.delegate respondstoselector:@selector(resetposition:andindex:)]) { [self.delegate resetposition:self andindex:self.hz_currentindex]; } } #pragma mark - #pragma mark - public method - (void)transformview:(cgfloat)offset { if (offset > 0) { return; } cgfloat currentheight = 210 - offset; self.height = currentheight ; cgfloat currentscale = 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.scrollview setcontentoffset:cgpointmake(hz_swidth, 0)]; } - (void)hz_getcontent:(hzreturnblock)block { self.hz_block = block; } - (void)layoutsubviews { [super layoutsubviews]; } @end
以上就是本文的全部内容,希望对大家的学习有所帮助。
上一篇: 91家居装修设计软件的图文使用教程