IOS 开发之网络图片轮播图的实现
程序员文章站
2023-12-18 10:18:04
ios 开发之网络图片轮播图的实现
截图
1.使用
ljphotogroupview *_ljphotogroupview = [[ljphotogrou...
ios 开发之网络图片轮播图的实现
截图
1.使用
ljphotogroupview *_ljphotogroupview = [[ljphotogroupview alloc]initwithitem:self.ljurlarray]; _ljphotogroupview.backgroundcolor = [uicolor blackcolor]; _ljphotogroupview.frame = cgrectmake(0, 0, kdevicewidth, kdeviceheight); [_ljphotogroupview showhintview:self];
2.源码
#import "ljphotogroupview.h" #import "ljwebidatamanager.h" @interface ljphotogroupcellview() @property (nonatomic, strong) uiimageview *ljimageview; @end @implementation ljphotogroupcellview - (instancetype)initwithframe:(cgrect)frame url:(nsstring*)imageurl { self = [super initwithframe:frame]; if (self) { [self addsubview:self.ljimageview]; //这里大家可以换成自己的网络请求图片的方法 [[ljwebidatamanager sharedinstances]retrievedata:imageurl successblock:^(nsdata *netdata, nsstring *progressstr, bool isfinished) { //在主线程中刷新界面 dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0), ^{ uiimage *_ljimage = [uiimage imagewithdata: netdata scale:0.3]; @myweakify(self); dispatch_async(dispatch_get_main_queue(), ^{ @mystrongify(self); self.ljimageview.image = _ljimage; }); }); }]; } return self; } - (uiimageview*)ljimageview { if (!_ljimageview) { _ljimageview = uiimageview.new; _ljimageview.frame = cgrectmake(15, 0, kdevicewidth - 30, 130); _ljimageview.backgroundcolor = [uicolor redcolor]; uigesturerecognizer *_tap = [[uigesturerecognizer alloc]initwithtarget:self action:@selector(dismisshintview)]; [_ljimageview addgesturerecognizer:_tap]; } return _ljimageview; } @end @interface ljphotogroupview()<uiscrollviewdelegate> @property (nonatomic, strong) uiscrollview *ljscrollview; @property (nonatomic, strong) nsarray *ljitemarray; @property (nonatomic, strong) uiimageview *ljimageview; @property (nonatomic, strong) uipagecontrol *ljpagecontrol; @end @implementation ljphotogroupview - (instancetype)initwithitem:(nsarray*)ljarray { self = [super init]; if (self) { self.ljitemarray = [nsarray arraywitharray:ljarray]; [self addsubview:self.ljscrollview]; [self addsubview:self.ljpagecontrol]; for (int i = 0; i < self.ljitemarray.count; i++) { //方法一:直接设置每个cell的x坐标 // ljphotogroupcellview *_cell = [[ljphotogroupcellview alloc]initwithframe:cgrectmake((kdevicewidth )*i, 0, kdevicewidth, 130) url:self.ljitemarray[i]]; //方法二:先不用考虑cell的x坐标,在下面设置x的坐标 ljphotogroupcellview *cell = [[ljphotogroupcellview alloc]initwithframe:self.ljscrollview.bounds url:self.ljitemarray[i]]; uitapgesturerecognizer *_tap = [[uitapgesturerecognizer alloc]initwithtarget:self action:@selector(dismisshintview)]; [cell addgesturerecognizer:_tap]; [self.ljscrollview addsubview:cell]; } //方法二:设置cell的x坐标 // 计算imageview的位置 [self.ljscrollview.subviews enumerateobjectsusingblock:^(ljphotogroupcellview *cell, nsuinteger idx, bool *stop) { // 调整x => origin => frame cgrect frame = cell.frame; frame.origin.x = idx * frame.size.width; cell.frame = frame; }]; self.ljpagecontrol.currentpage = 0; } return self; } - (uiscrollview*)ljscrollview { if (!_ljscrollview) { _ljscrollview = uiscrollview.new; _ljscrollview.frame = cgrectmake(0, 250, kdevicewidth, 130); _ljscrollview.delegate = self; //_scrollview.scrollstotop = no; _ljscrollview.pagingenabled = yes; _ljscrollview.contentsize = cgsizemake(_ljscrollview.bounds.size.width * self.ljitemarray.count, 130); //_scrollview.alwaysbouncehorizontal = groupitems.count > 1; // _scrollview.showshorizontalscrollindicator = no; //_scrollview.showsverticalscrollindicator = no; //_scrollview.autoresizingmask = uiviewautoresizingflexiblewidth | uiviewautoresizingflexibleheight; //_scrollview.delayscontenttouches = no; //_scrollview.cancancelcontenttouches = yes; } return _ljscrollview; } - (uipagecontrol *)ljpagecontrol { if (_ljpagecontrol == nil) { // 分页控件,本质上和scrollview没有任何关系,是两个独立的控件 _ljpagecontrol = [[uipagecontrol alloc] init]; // 总页数 _ljpagecontrol.numberofpages = self.ljitemarray.count; cgsize size = [_ljpagecontrol sizefornumberofpages:self.ljitemarray.count]; _ljpagecontrol.bounds = cgrectmake(0, 0, size.width, size.height); _ljpagecontrol.center = cgpointmake(self.center.x, 380); // 设置颜色 _ljpagecontrol.pageindicatortintcolor = [uicolor redcolor]; //当前页面的颜色 _ljpagecontrol.currentpageindicatortintcolor = [uicolor whitecolor]; [_ljpagecontrol addtarget:self action:@selector(pagechanged:) forcontrolevents:uicontroleventvaluechanged]; } return _ljpagecontrol; } // 分页控件的监听方法 - (void)pagechanged:(uipagecontrol *)page { nslog(@"%ld", (long)page.currentpage); // 根据页数,调整滚动视图中的图片位置 contentoffset self.scrollview.bounds.size.width cgfloat x = page.currentpage * (kdevicewidth); [self.ljscrollview setcontentoffset:cgpointmake(x, 0) animated:yes]; } - (uiimageview*)ljimageview { if (!_ljimageview) { _ljimageview = uiimageview.new; _ljimageview.frame = cgrectmake(0, 0, kdevicewidth, kdeviceheight); _ljimageview.backgroundcolor = [uicolor redcolor]; uigesturerecognizer *_tap = [[uigesturerecognizer alloc]initwithtarget:self action:@selector(dismisshintview)]; [_ljimageview addgesturerecognizer:_tap]; } return _ljimageview; } - (void)scrollviewdidscroll:(uiscrollview *)scrollview { //cgfloat floatpage = _scrollview.contentoffset.x / _scrollview.width; //nsinteger page = _scrollview.contentoffset.x / _scrollview.width; } - (void)scrollviewdidenddragging:(uiscrollview *)scrollview willdecelerate:(bool)decelerate{ if (!decelerate) { } } #pragma mark - scrollview的代理方法 // 滚动视图停下来,修改页面控件的小点(页数) - (void)scrollviewdidenddecelerating:(uiscrollview *)scrollview { // 停下来的当前页数 nslog(@"%@", nsstringfromcgpoint(scrollview.contentoffset)); // 计算页数 int page = scrollview.contentoffset.x / scrollview.bounds.size.width; self.ljpagecontrol.currentpage = page; } - (void)showhintview:(uiview*)view { //[view addsubview:self]; [[uiapplication sharedapplication].delegate.window.rootviewcontroller.view addsubview:self]; self.alpha = 0.0; [uiview animatewithduration:0.3 delay:0 options:uiviewanimationoptioncurveeaseout animations:^{ self.alpha = 1.0; } completion:^(bool finished) { }]; } - (void)dismisshintview { [uiview animatewithduration:0.3 delay:0 options:uiviewanimationoptioncurveeasein animations:^{ self.alpha = 0.0; } completion:^(bool finished){ [self removefromsuperview]; }]; } @end
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!