国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

IOS 開發之網絡圖片輪播圖的實現

2020-07-26 02:41:26
字體:
來源:轉載
供稿:網友

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 

如有疑問請留言或者到本站社區交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 富阳市| 福贡县| 白城市| 邹城市| 张家界市| 托里县| 昭觉县| 太保市| 乌恰县| 浮山县| 新竹市| 陆川县| 浦北县| 双辽市| 威远县| 南平市| 黑水县| 甘泉县| 藁城市| 庆安县| 大石桥市| 柳州市| 安乡县| 吉安县| 手游| 泸水县| 陈巴尔虎旗| 屏山县| 东光县| 舞阳县| 景东| 安吉县| 昌宁县| 邹平县| 合阳县| 安陆市| 长泰县| 根河市| 盐津县| 晋州市| 黑山县|