沈浸式圖片輪播器 -- DDGBannerScrollView使用文檔
阿新 • • 發佈:2019-01-21
hang href 運動 oat str tar 默認 ray image
寫在前面
幾乎每個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)coe andEndColor:(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的作者,也感謝大家的關心和支持,如果對你有幫助,希望你不吝?star一下。
沈浸式圖片輪播器 -- DDGBannerScrollView使用文檔