怎樣才能實(shí)現(xiàn)高效圖片輪播
推薦 + 挑錯(cuò) + 收藏(0) + 用戶評(píng)論(0)
輪播實(shí)現(xiàn)步驟
接下來(lái),筆者將從各方面逐一分析。
層級(jí)結(jié)構(gòu)
最底層是一個(gè)UIView,上面有一個(gè)UIScrollView以及UIPageControl,scrollView上有兩個(gè)UIImageView,imageView寬高 = scrollview寬高 = view寬高
輪播原理
假設(shè)輪播控件的寬度為x高度為y,我們?cè)O(shè)置scrollview的contentSize.width為3x,并讓scrollview的水平偏移量為x,既顯示最中間內(nèi)容
scrollView.contentSize = CGSizeMake(3x, y); scrollView.contentOffset = CGPointMake(x, 0);
將imageView添加到scrollview內(nèi)容視圖的中間位置
接下來(lái)使用代理方法scrollViewDidScroll來(lái)監(jiān)聽(tīng)scrollview的滾動(dòng),定義一個(gè)枚舉變量來(lái)記錄滾動(dòng)的方向
typedef enum{ DirecNone, DirecLeft, DirecRight } Direction;@property (nonatomic, assign) Direction direction; - (void)scrollViewDidScroll:(UIScrollView *)scrollView { self.direction = scrollView.contentOffset.x 》x? DirecLeft : DirecRight; }
使用KVO來(lái)監(jiān)聽(tīng)direction屬性值的改變
[self addObserver:self forKeyPath:@“direction” options:NSKeyValueObservingOptionNew context:nil];
判斷滾動(dòng)的方向,當(dāng)偏移量大于x,表示左移,則將otherImageView加在右邊,偏移量小于x,表示右移,則將otherImageView加在左邊
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { //self.currIndex表示當(dāng)前顯示圖片的索引,self.nextIndex表示將要顯示圖片的索引 //_images為圖片數(shù)組 if(change[NSKeyValueChangeNewKey] == change[NSKeyValueChangeOldKey]) return; if ([change[NSKeyValueChangeNewKey] intValue] == DirecRight) { self.otherImageView.frame = CGRectMake(0, 0, self.width, self.height); self.nextIndex = self.currIndex - 1; if (self.nextIndex 《 0) self.nextIndex = _images.count – 1; } else if ([change[NSKeyValueChangeNewKey] intValue] == DirecLeft){ self.otherImageView.frame = CGRectMake(CGRectGetMaxX(_currImageView.frame), 0, self.width, self.height); self.nextIndex = (self.currIndex + 1) % _images.count; } self.otherImageView.image = self.images[self.nextIndex]; }
通過(guò)代理方法scrollViewDidEndDecelerating來(lái)監(jiān)聽(tīng)滾動(dòng)結(jié)束,結(jié)束后,會(huì)變成以下兩種情況:
此時(shí),scrollview的偏移量為0或者2x,我們通過(guò)代碼再次將scrollview的偏移量設(shè)置為x,并將currImageView的圖片修改為otherImageView的圖片
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { [self pauseScroll]; } - (void)pauseScroll { self.direction = DirecNone;//清空滾動(dòng)方向 //判斷最終是滾到了右邊還是左邊 int index = self.scrollView.contentOffset.x / x; if (index == 1) return; //等于1表示最后沒(méi)有滾動(dòng),返回不做任何操作 self.currIndex = self.nextIndex;//當(dāng)前圖片索引改變 self.pageControl.currentPage = self.currIndex; self.currImageView.frame = CGRectMake(x, 0, x, y); self.currImageView.image = self.otherImageView.image; self.scrollView.contentOffset = CGPointMake(x, 0); }
那么我們看到的還是currImageView,只不過(guò)展示的是下一張圖片,如圖,又變成了最初的效果
自動(dòng)滾動(dòng)
輪播的功能實(shí)現(xiàn)了,接下來(lái)添加定時(shí)器讓它自動(dòng)滾動(dòng),相當(dāng)簡(jiǎn)單
- (void)startTimer { //如果只有一張圖片,則直接返回,不開(kāi)啟定時(shí)器 if (_images.count 《= 1) return; //如果定時(shí)器已開(kāi)啟,先停止再重新開(kāi)啟 if (self.timer) [self stopTimer]; self.timer = [NSTimer timerWithTimeInterval:self.time target:self selector:@selector(nextPage) userInfo:nil repeats:YES]; [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes]; } - (void)nextPage { //動(dòng)畫(huà)改變scrollview的偏移量就可以實(shí)現(xiàn)自動(dòng)滾動(dòng) [self.scrollView setContentOffset:CGPointMake(self.width * 2, 0) animated:YES]; }
注意:setContentOffset:animated:方法執(zhí)行完畢后不會(huì)調(diào)用scrollview的scrollViewDidEndDecelerating方法,但是會(huì)調(diào)用scrollViewDidEndScrollingAnimation方法,因此我們要在該方法中調(diào)用pauseScroll
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView { [self pauseScroll]; }
拖拽時(shí)停止自動(dòng)滾動(dòng)
當(dāng)我們手動(dòng)拖拽圖片時(shí),需要停止自動(dòng)滾動(dòng),此時(shí)我們只需要讓定時(shí)器失效就行了,當(dāng)停止拖拽時(shí),重新啟動(dòng)定時(shí)器
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { [self.timer invalidate]; } - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{ [self startTimer]; }
非常好我支持^.^
(0) 0%
不好我反對(duì)
(0) 0%