DDGBannerScrollView:iOS 沉浸式無限圖片輪播器
寫在前面
幾乎每個app都會用到圖片輪播器,而且圖片輪播器也越來越高大上,沉浸式等拉高了APP的檔次 ,沒有一個高大上的圖片輪播器,都不好意思上架。 像一些知名的app都採用了圖片輪播的背景漸變色,舉幾個栗子:優酷的首頁,喜馬拉雅,蜻蜓fm,嗶哩嗶哩漫畫等, page索引也是玩的很高大上,系統的早已滿足不了了需求。 鑑於此和專案的需要,在前人的基礎上,整理了一個這個庫,志在簡單的幾句程式碼,就能讓應用看上去高大上。 github:[DDGBannerScrollView](https://github.com/dudongge/DDGBannerScrollView)
DDGBannerScrollView 此庫的功能
1、無限圖片輪播功能 2、每個圖片的相對偏移量,方便開發者自己封裝東西 3、pageControl的幾個動畫,(旋轉,跳躍等慢慢會增加) DDGBannerScrollView 用到的知識 1、圖片輪播器(UICollectionView + 定時器) 2、一種顏色向另一種顏色線性的漸變。 3、簡單的旋轉動畫(frame動畫 CABasicAnimation) 4、簡單的貝塞爾曲線(半圓)與動畫的組合(UIBezierPath + CAKeyframeAnimation)
來看看效果(雖然效果不太明顯)
動畫的模組也可單獨使用
!
模組分解
圖片輪播器
圖片輪播器(UICollectionView + 定時器),這個參考了知名的第三方庫SDCycleScrollView,並在此基礎上做了修改,文末附有連結 所以在效能和穩定性上有了保證,在此表示感謝。
兩種顏色的線性漸變
!
我們都知道,一個畫素點有三原色加上透明度組成,也就是所說的RGBA(紅,綠,藍,透明度),改變其中的任意一個值,給我們呈現的顏色就不一樣。 比如,一個點的R1為10,另一個顏色的R2為30,那麼R1->R2的線性變化的差值就是20 ,如果滑塊的偏移量為100,那麼漸變係數為0.2,那麼R2 = 10 + 100 * 0.2, 當我們在拉滑塊的過程中,R在顏色變化中就是線性的,同理剩餘顏色也是漸變的。如上圖中的中間View,就是在兩個顏色之間過度。 這個關於顏色的擴充套件,我已經封裝到庫中,大家可以直接使用。 關鍵函式為下面,具體實現可參考程式碼 /** 得到一個顏色的原始值 RGBA @param originColor 傳入顏色 @return 顏色值陣列 */ + (NSArray *)getRGBDictionaryByColor:(UIColor *)originColor; /** 得到兩個值的色差 @param beginColor 起始顏色 @param endColor 終止顏色 @return 色差陣列 */ + (NSArray *)transColorBeginColor:(UIColor *)beginColor andEndColor:(UIColor *)endColor; /** 傳入兩個顏色和係數 @param beginColor 開始顏色 @param coe 係數(0->1) @param endColor 終止顏色 @return 過度顏色 */ + (UIColor *)getColorWithColor:(UIColor *)beginColor andCoe:(double)coeandEndColor:(UIColor *)endColor;
簡單的旋轉動畫和貝塞爾半圓動畫
簡單的旋轉動畫和貝塞爾半圓動畫(比較基礎和簡單,直接上程式碼) /** 新增旋轉動畫 @param imageView 旋轉的目標圖片 @param duration 旋轉持續時間 @param clockwise 旋轉的方向(正向還是逆向) */ - (void)startrRotationImageView:(UIImageView *)imageView duration:(CGFloat)duration clockwise:(BOOL)clockwise { CABasicAnimation* rotationAnimation; //動畫的方式,繞著z軸 rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; //旋轉的弧度 rotationAnimation.toValue = [NSNumber numberWithFloat: clockwise ? M_PI * 2.0 : -M_PI * 2.0 ]; //動畫持續的時間 rotationAnimation.duration = duration; //動畫角度值是否累加(預設為NO) rotationAnimation.cumulative = NO; //重複次數 rotationAnimation.repeatCount = 1; //動畫新增到layer上 [imageView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"]; } /** 沿著UIBezierPath運動 @param imageView 目標b圖片 @param duration 動畫持續時間 @param controlPoint 控制點 @param clockwise 旋轉方向(正向還是逆向) */ - (void)startrRotationImageView:(UIImageView *)imageView duration:(CGFloat)duration controlPoint:(CGPoint)controlPoint clockwise:(BOOL)clockwise { CAKeyframeAnimation *animation = [CAKeyframeAnimation animation]; //設定動畫屬性,因為是沿著貝塞爾曲線動,所以要設定為position animation.keyPath = @"position"; //設定動畫時間 animation.duration = duration; // 告訴在動畫結束的時候不要移除 animation.removedOnCompletion = YES; // 始終保持最新的效果 //animation.fillMode = kCAFillModeForwards; //貝塞爾曲線 UIBezierPath *circlePath = [UIBezierPath bezierPathWithArcCenter:controlPoint radius:((_dotMargin + _dotNomalSize.width ) /2.0) startAngle: clockwise ? M_PI : 0 endAngle: clockwise ? 0 : M_PI clockwise: clockwise]; // 設定貝塞爾曲線路徑 animation.path = circlePath.CGPath; // 將動畫物件新增到檢視的layer上 [imageView.layer addAnimation:animation forKey:@"position"]; }
如何使用
1,下載本demo,直接將DDGBannerScrollView資料夾下的檔案拖入即可,詳細使用見demo和原始碼 2,pod 'DDGBannerScrollView'
簡單程式碼
//頭部banner圖片 @property (nonatomic, strong) DDGBannerScrollView *bannerScrollView; //頭部banner背景圖片 @property (nonatomic, strong) UIView *bgHeaderView; - (UIView *)bgHeaderView { if (!_bgHeaderView) { _bgHeaderView = [[UIView alloc]init]; _bgHeaderView.frame = CGRectMake(0,0, screen_width , screen_width * 0.37 + 120); } return _bgHeaderView; } - (DDGBannerScrollView *)bannerScrollView { if (!_bannerScrollView) { CGRect frame = CGRectMake(30, 88, self.view.frame.size.width - 60, screen_width * 0.37); _bannerScrollView = [DDGBannerScrollView cycleScrollViewWithFrame:frame delegate:self placeholderImage:[UIImage imageNamed:@"cache_cancel_all"]]; _bannerScrollView.imageURLStringsGroup = @[@"3",@"1",@"2",@"1",@"3"]; } return _bannerScrollView; } [self.bgHeaderView addSubview:self.bannerScrollView]; self.bannerScrollView.pageControlAliment = DDGBannerScrollViewPageContolAlimentRight; self.bannerScrollView.pageControlStyle = DDGBannerScrollViewPageControlHorizontal; self.bannerScrollView.pageDotColor = UIColor.greenColor; self.bannerScrollView.currentPageDotColor = UIColor.redColor; //根據偏移量計算設定banner背景顏色 - (void)handelBannerBgColorWithOffset:(NSInteger )offset { if (1 == self.changeColors.count) return; NSInteger offsetCurrent = offset % (int)self.bannerScrollView.bounds.size.width ; float rate = offsetCurrent / self.bannerScrollView.bounds.size.width ; NSInteger currentPage = offset / (int)self.bannerScrollView.bounds.size.width; UIColor *startPageColor; UIColor *endPageColor; if (currentPage == self.changeColors.count - 1) { startPageColor = self.changeColors[currentPage]; endPageColor = self.changeColors[0]; } else { if (currentPage== self.changeColors.count) { return; } startPageColor = self.changeColors[currentPage]; endPageColor = self.changeColors[currentPage + 1]; } UIColor *currentToLastColor = [UIColor getColorWithColor:startPageColor andCoe:rate andEndColor:endPageColor]; self.bgHeaderView.backgroundColor = currentToLastColor; }
寫在最後
奉上github地址:DDGBannerScrollView
掘金地址:DDGBannerScrollView
簡書地址:DDGBannerScrollView
最後,再次感謝下SDCycleScrollView
的作者,也感謝大家的關心和支援,如果對你有幫助,希望你不吝