IOS 開發(fā)之網(wǎng)絡(luò)圖片輪播圖的實(shí)現(xiàn)
坪山網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)建站!從網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)公司等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營(yíng)維護(hù)。創(chuàng)新互聯(lián)建站自2013年創(chuàng)立以來到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)建站。
截圖
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]; //這里大家可以換成自己的網(wǎng)絡(luò)請(qǐng)求圖片的方法 [[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()@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++) { //方法一:直接設(shè)置每個(gè)cell的X坐標(biāo) // LJPhotoGroupCellView *_cell = [[LJPhotoGroupCellView alloc]initWithFrame:CGRectMake((kDEVICEWIDTH )*i, 0, kDEVICEWIDTH, 130) url:self.ljItemArray[i]]; //方法二:先不用考慮cell的X坐標(biāo),在下面設(shè)置X的坐標(biāo) 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]; } //方法二:設(shè)置cell的X坐標(biāo) // 計(jì)算imageView的位置 [self.ljScrollView.subviews enumerateObjectsUsingBlock:^(LJPhotoGroupCellView *cell, NSUInteger idx, BOOL *stop) { // 調(diào)整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) { // 分頁(yè)控件,本質(zhì)上和scrollView沒有任何關(guān)系,是兩個(gè)獨(dú)立的控件 _ljPageControl = [[UIPageControl alloc] init]; // 總頁(yè)數(shù) _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); // 設(shè)置顏色 _ljPageControl.pageIndicatorTintColor = [UIColor redColor]; //當(dāng)前頁(yè)面的顏色 _ljPageControl.currentPageIndicatorTintColor = [UIColor whiteColor]; [_ljPageControl addTarget:self action:@selector(pageChanged:) forControlEvents:UIControlEventValueChanged]; } return _ljPageControl; } // 分頁(yè)控件的監(jiān)聽方法 - (void)pageChanged:(UIPageControl *)page { NSLog(@"%ld", (long)page.currentPage); // 根據(jù)頁(yè)數(shù),調(diào)整滾動(dòng)視圖中的圖片位置 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的代理方法 // 滾動(dòng)視圖停下來,修改頁(yè)面控件的小點(diǎn)(頁(yè)數(shù)) - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { // 停下來的當(dāng)前頁(yè)數(shù) NSLog(@"%@", NSStringFromCGPoint(scrollView.contentOffset)); // 計(jì)算頁(yè)數(shù) 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
如有疑問請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!