IOS开发之网络图片轮播图的实现

  

<强> IOS开发之网络图片轮播图的实现

  

<强>截图

  

 IOS开发之网络图片轮播图的实现

  

<强> 1。使用

        LJPhotoGroupView * _ljPhotoGroupView=[[LJPhotoGroupView alloc] initWithItem self.ljUrlArray):;   _ljPhotoGroupView。写成backgroundColor=[用户界面颜色blackColor];   _ljPhotoGroupView.frame=CGRectMake (0, 0, kDEVICEWIDTH kDEVICEHEIGHT);   [_ljPhotoGroupView showHintView:自我);之前      

<强> 2。源码

        #进口“LJPhotoGroupView.h”   #进口“LJWebIDataManager.h”      @ interface LJPhotoGroupCellView ()      @ property(原子、强)UIImageView * ljImageview;      @end      @ implementation LJPhotoGroupCellView      - (instancetype) initWithFrame:(CGRect中)框架的url: imageurl (NSString *)   {   自我=(超级initWithFrame:框架);   如果(自我){   (自我addSubview: self.ljImageview);//这里大家可以换成自己的网络请求图片的方法   [[LJWebIDataManager sharedInstances] retrieveData: imageurl successBlock: ^ (NSData * netData NSString * progressStr, BOOL结束){//在主线程中刷新界面   设置(dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)、^ {      用户界面图像* _ljImage=[界面图像imageWithData: netData规模:0.3);   @myWeakify(自我);   设置(dispatch_get_main_queue()、^ {   @myStrongify(自我);   self.ljImageview。形象=_ljImage;   });   });   });   }   回归自我;   }      - (UIImageView *) ljImageview   {   如果(! _ljImageview) {   _ljImageview=UIImageView.new;   _ljImageview.frame=CGRectMake (15 0 kDEVICEWIDTH - 30, 130);   _ljImageview。写成backgroundColor=[用户界面颜色redColor];   UIGestureRecognizer * _tap=[[UIGestureRecognizer alloc] initWithTarget:自我行动:@ selector (dismissHintView)];   [_ljImageview addGestureRecognizer: _tap];   }   返回_ljImageview;   }      @end      @ interface LJPhotoGroupView () & lt; UIScrollViewDelegate>      @ property(原子、强)UIScrollView * ljScrollView;   @ property(原子、强)NSArray * ljItemArray;   @ property(原子、强)UIImageView * ljImageview;   @ property(原子、强)UIPageControl * ljPageControl;         @end      @ implementation LJPhotoGroupView      - (instancetype) initWithItem:(NSArray *) ljArray   {   自我=(超级init);   如果(自我)   {   自我。ljItemArray=[NSArray arrayWithArray ljArray):;      (自我addSubview: self.ljScrollView);   (自我addSubview: self.ljPageControl);      for (int i=0;我& lt;self.ljItemArray.count;我+ +){//方法一:直接设置每个细胞的X坐标//LJPhotoGroupCellView * _cell=[[LJPhotoGroupCellView alloc] initWithFrame: CGRectMake ((kDEVICEWIDTH) *我,0 kDEVICEWIDTH 130) url: self.ljItemArray[我]];//方法二:先不用考虑细胞的X坐标,在下面设置X的坐标   LJPhotoGroupCellView *细胞=[[LJPhotoGroupCellView alloc] initWithFrame: self.ljScrollView。边界url: self.ljItemArray[我]];   UITapGestureRecognizer * _tap=[[UITapGestureRecognizer alloc] initWithTarget:自我行动:@ selector (dismissHintView)];   (细胞addGestureRecognizer: _tap);   (自我。ljScrollView addSubview:细胞);   }//方法二:设置细胞的X坐标//计算imageView的位置   [self.ljScrollView。子视图enumerateObjectsUsingBlock: ^ (LJPhotoGroupCellView *细胞,了NSUInteger idx, BOOL *停止)   {//调整x=比;起源=比;框架   CGRect中?cell.frame;   frame.origin。x=idx * frame.size.width;      cell.frame=框架;   });      self.ljPageControl。当前页=0;   }   回归自我;   }      - (UIScrollView *) ljScrollView   {   如果(! _ljScrollView)   {   _ljScrollView=UIScrollView.new;   kDEVICEWIDTH _ljScrollView.frame=CGRectMake(0, 250年,130年);   _ljScrollView.delegate=自我;//_scrollView。scrollsToTop=没有;   _ljScrollView。pagingEnabled=是的;   _ljScrollView。contentSize=CGSizeMake (_ljScrollView.bounds.size。宽度* self.ljItemArray。统计,130);//_scrollView。alwaysBounceHorizontal=groupItems。数比;1;//_scrollView。showsHorizontalScrollIndicator=没有;//_scrollView。showsVerticalScrollIndicator=没有;//_scrollView。autoresizingMask=UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;//_scrollView.delaysContentTouches=没有;//_scrollView。canCancelContentTouches=是的;   }   返回_ljScrollView;   }      - (UIPageControl *) ljPageControl   {   如果(_ljPageControl==nil)   {//分页控件,本质上和滚动视图没有任何关系,是两个独立的控件   _ljPageControl=[[UIPageControl alloc] init);//总页数   _ljPageControl。numberOfPages=self.ljItemArray.count;   CGSize大?[_ljPageControl sizeForNumberOfPages self.ljItemArray.count):;      _ljPageControl。边界=CGRectMake(0, 0,大小。宽度,size.height);   _ljPageControl。=CGPointMake (self.center中心。x, 380);//设置颜色   _ljPageControl。pageIndicatorTintColor=[用户界面颜色redColor];//当前页面的颜色   _ljPageControl。currentPageIndicatorTintColor=[用户界面颜色whiteColor];   [_ljPageControl addTarget:自我行动:@ selector (pageChanged:) forControlEvents: UIControlEventValueChanged);   }   返回_ljPageControl;   }//分页控件的监听方法   - (void) pageChanged: (UIPageControl *)页面   {   NSLog (@“% ld”,(长)page.currentPage);//根据页数,调整滚动视图中的图片位置内容偏移self.scrollView.bounds.size.width   CGFloat x=页面。当前页* (kDEVICEWIDTH);   (自我。ljScrollView setContentOffset: CGPointMake (x, 0)动画:是的);   }      - (UIImageView *) ljImageview   {   如果(! _ljImageview) {   _ljImageview=UIImageView.new;   _ljImageview.frame=CGRectMake (0, 0, kDEVICEWIDTH kDEVICEHEIGHT);   _ljImageview。写成backgroundColor=[用户界面颜色redColor];      null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

IOS开发之网络图片轮播图的实现