寫(xiě)在前面
幾乎每個(gè)app都會(huì)用到圖片輪播器仗颈,而且圖片輪播器也越來(lái)越高大上佛舱,沉浸式等拉高了APP的檔次
,沒(méi)有一個(gè)高大上的圖片輪播器挨决,都不好意思上架请祖。
像一些知名的app都采用了圖片輪播的背景漸變色,舉幾個(gè)栗子:優(yōu)酷的首頁(yè)凰棉,喜馬拉雅损拢,蜻蜓fm,嗶哩嗶哩漫畫(huà)等撒犀,
page索引也是玩的很高大上,系統(tǒng)的早已滿足不了了需求掏秩。
鑒于此和項(xiàng)目的需要或舞,在前人的基礎(chǔ)上,整理了一個(gè)這個(gè)庫(kù)蒙幻,志在簡(jiǎn)單的幾句代碼映凳,就能讓?xiě)?yīng)用看上去高大上。
github:[DDGBannerScrollView](https://github.com/dudongge/DDGBannerScrollView)
DDGBannerScrollView 此庫(kù)的功能
1邮破、無(wú)限圖片輪播功能
2诈豌、每個(gè)圖片的相對(duì)偏移量,方便開(kāi)發(fā)者自己封裝東西
3抒和、pageControl的幾個(gè)動(dòng)畫(huà)矫渔,(旋轉(zhuǎn),跳躍等慢慢會(huì)增加)
DDGBannerScrollView 用到的知識(shí)
1摧莽、圖片輪播器(UICollectionView + 定時(shí)器)
2庙洼、一種顏色向另一種顏色線性的漸變。
3镊辕、簡(jiǎn)單的旋轉(zhuǎn)動(dòng)畫(huà)(frame動(dòng)畫(huà) CABasicAnimation)
4油够、簡(jiǎn)單的貝塞爾曲線(半圓)與動(dòng)畫(huà)的組合(UIBezierPath + CAKeyframeAnimation)
來(lái)看看效果(雖然效果不太明顯)
[圖片上傳失敗...(image-cb323d-1548121510551)]
動(dòng)畫(huà)的模塊也可單獨(dú)使用
image
!
模塊分解
圖片輪播器
圖片輪播器(UICollectionView + 定時(shí)器),這個(gè)參考了知名的第三方庫(kù)SDCycleScrollView征懈,并在此基礎(chǔ)上做了修改石咬,文末附有鏈接
所以在性能和穩(wěn)定性上有了保證,在此表示感謝卖哎。
兩種顏色的線性漸變
image
!
我們都知道鬼悠,一個(gè)像素點(diǎn)有三原色加上透明度組成删性,也就是所說(shuō)的RGBA(紅,綠厦章,藍(lán)镇匀,透明度),改變其中的任意一個(gè)值袜啃,給我們呈現(xiàn)的顏色就不一樣汗侵。
比如,一個(gè)點(diǎn)的R1為10群发,另一個(gè)顏色的R2為30晰韵,那么R1->R2的線性變化的差值就是20 ,如果滑塊的偏移量為100,那么漸變系數(shù)為0.2熟妓,那么R2 = 10 + 100 * 0.2雪猪,
當(dāng)我們?cè)诶瑝K的過(guò)程中,R在顏色變化中就是線性的起愈,同理剩余顏色也是漸變的只恨。如上圖中的中間View,就是在兩個(gè)顏色之間過(guò)度抬虽。
這個(gè)關(guān)于顏色的擴(kuò)展官觅,我已經(jīng)封裝到庫(kù)中,大家可以直接使用阐污。
關(guān)鍵函數(shù)為下面休涤,具體實(shí)現(xiàn)可參考代碼
/**
得到一個(gè)顏色的原始值 RGBA
@param originColor 傳入顏色
@return 顏色值數(shù)組
*/
+ (NSArray *)getRGBDictionaryByColor:(UIColor *)originColor;
/**
得到兩個(gè)值的色差
@param beginColor 起始顏色
@param endColor 終止顏色
@return 色差數(shù)組
*/
+ (NSArray *)transColorBeginColor:(UIColor *)beginColor andEndColor:(UIColor *)endColor;
/**
傳入兩個(gè)顏色和系數(shù)
@param beginColor 開(kāi)始顏色
@param coe 系數(shù)(0->1)
@param endColor 終止顏色
@return 過(guò)度顏色
*/
+ (UIColor *)getColorWithColor:(UIColor *)beginColor andCoe:(double)coe andEndColor:(UIColor *)endColor;
簡(jiǎn)單的旋轉(zhuǎn)動(dòng)畫(huà)和貝塞爾半圓動(dòng)畫(huà)
簡(jiǎn)單的旋轉(zhuǎn)動(dòng)畫(huà)和貝塞爾半圓動(dòng)畫(huà)(比較基礎(chǔ)和簡(jiǎn)單,直接上代碼)
/**
添加旋轉(zhuǎn)動(dòng)畫(huà)
@param imageView 旋轉(zhuǎn)的目標(biāo)圖片
@param duration 旋轉(zhuǎn)持續(xù)時(shí)間
@param clockwise 旋轉(zhuǎn)的方向(正向還是逆向)
*/
- (void)startrRotationImageView:(UIImageView *)imageView duration:(CGFloat)duration clockwise:(BOOL)clockwise {
CABasicAnimation* rotationAnimation;
//動(dòng)畫(huà)的方式笛辟,繞著z軸
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
//旋轉(zhuǎn)的弧度
rotationAnimation.toValue = [NSNumber numberWithFloat: clockwise ? M_PI * 2.0 : -M_PI * 2.0 ];
//動(dòng)畫(huà)持續(xù)的時(shí)間
rotationAnimation.duration = duration;
//動(dòng)畫(huà)角度值是否累加(默認(rèn)為NO)
rotationAnimation.cumulative = NO;
//重復(fù)次數(shù)
rotationAnimation.repeatCount = 1;
//動(dòng)畫(huà)添加到layer上
[imageView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}
/**
沿著UIBezierPath運(yùn)動(dòng)
@param imageView 目標(biāo)b圖片
@param duration 動(dòng)畫(huà)持續(xù)時(shí)間
@param controlPoint 控制點(diǎn)
@param clockwise 旋轉(zhuǎn)方向(正向還是逆向)
*/
- (void)startrRotationImageView:(UIImageView *)imageView duration:(CGFloat)duration controlPoint:(CGPoint)controlPoint clockwise:(BOOL)clockwise {
CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
//設(shè)置動(dòng)畫(huà)屬性功氨,因?yàn)槭茄刂惾麪柷€動(dòng),所以要設(shè)置為position
animation.keyPath = @"position";
//設(shè)置動(dòng)畫(huà)時(shí)間
animation.duration = duration;
// 告訴在動(dòng)畫(huà)結(jié)束的時(shí)候不要移除
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];
// 設(shè)置貝塞爾曲線路徑
animation.path = circlePath.CGPath;
// 將動(dòng)畫(huà)對(duì)象添加到視圖的layer上
[imageView.layer addAnimation:animation forKey:@"position"];
}
如何使用
1手幢,下載本demo捷凄,直接將DDGBannerScrollView文件夾下的文件拖入即可,詳細(xì)使用見(jiàn)demo和源碼
2弯菊,pod 'DDGBannerScrollView'
簡(jiǎn)單代碼
//頭部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;
//根據(jù)偏移量計(jì)算設(shè)置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;
}
寫(xiě)在最后
奉上github地址:DDGBannerScrollView
掘金地址:DDGBannerScrollView
簡(jiǎn)書(shū)地址:DDGBannerScrollView
最后纵势,再次感謝下SDCycleScrollView的作者,也感謝大家的關(guān)心和支持管钳,如果對(duì)你有幫助钦铁,希望你不吝?star一下。