@TOC## 寫在前面
幾乎每個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一下。
歡迎使用Markdown編輯器
你好! 這是你第一次使用 Markdown編輯器 所展示的歡迎頁。如果你想學習如何使用Markdown編輯器, 可以仔細閱讀這篇文章,瞭解一下Markdown的基本語法知識。
新的改變
我們對Markdown編輯器進行了一些功能拓展與語法支持,除了標準的Markdown編輯器功能,我們增加了如下幾點新功能,幫助你用它寫博客:
- 全新的界面設計 ,將會帶來全新的寫作體驗;
- 在創作中心設置你喜愛的代碼高亮樣式,Markdown 將代碼片顯示選擇的高亮樣式 進行展示;
- 增加了 圖片拖拽 功能,你可以將本地的圖片直接拖拽到編輯區域直接展示;
- 全新的 KaTeX數學公式 語法;
- 增加了支持甘特圖的mermaid語法1 功能;
- 增加了 多屏幕編輯 Markdown文章功能;
- 增加了 焦點寫作模式、預覽模式、簡潔寫作模式、左右區域同步滾輪設置 等功能,功能按鈕位於編輯區域與預覽區域中間;
- 增加了 檢查列表 功能。
功能快捷鍵
撤銷:Ctrl/Command + Z
重做:Ctrl/Command + Y
加粗:Ctrl/Command + B
斜體:Ctrl/Command + I
標題:Ctrl/Command + Shift + H
無序列表:Ctrl/Command + Shift + U
有序列表:Ctrl/Command + Shift + O
檢查列表:Ctrl/Command + Shift + C
插入代碼:Ctrl/Command + Shift + K
插入鏈接:Ctrl/Command + Shift + L
插入圖片:Ctrl/Command + Shift + G
合理的創建標題,有助於目錄的生成
直接輸入1次#,並按下space後,將生成1級標題。
輸入2次#,並按下space後,將生成2級標題。
以此類推,我們支持6級標題。有助於使用TOC
語法後生成一個完美的目錄。
如何改變文本的樣式
強調文本 強調文本
加粗文本 加粗文本
標記文本
刪除文本
引用文本
H2O is是液體。
210 運算結果是 1024.
插入鏈接與圖片
鏈接: link.
圖片:
帶尺寸的圖片:
居中的圖片:
居中並且帶尺寸的圖片:
當然,我們爲了讓用戶更加便捷,我們增加了圖片拖拽功能。
如何插入一段漂亮的代碼片
去博客設置頁面,選擇一款你喜歡的代碼片高亮樣式,下面展示同樣高亮的 代碼片
.
// An highlighted block
var foo = 'bar';
生成一個適合你的列表
- 項目
- 項目
- 項目
- 項目
- 項目1
- 項目2
- 項目3
- 計劃任務
- 完成任務
創建一個表格
一個簡單的表格是這麼創建的:
項目 | Value |
---|---|
電腦 | $1600 |
手機 | $12 |
導管 | $1 |
設定內容居中、居左、居右
使用:---------:
居中
使用:----------
居左
使用----------:
居右
第一列 | 第二列 | 第三列 |
---|---|---|
第一列文本居中 | 第二列文本居右 | 第三列文本居左 |
SmartyPants
SmartyPants將ASCII標點字符轉換爲“智能”印刷標點HTML實體。例如:
TYPE | ASCII | HTML |
---|---|---|
Single backticks | 'Isn't this fun?' |
‘Isn’t this fun?’ |
Quotes | "Isn't this fun?" |
“Isn’t this fun?” |
Dashes | -- is en-dash, --- is em-dash |
– is en-dash, — is em-dash |
創建一個自定義列表
- Markdown
- Text-to-HTML conversion tool
- Authors
- John
- Luke
如何創建一個註腳
一個具有註腳的文本。2
註釋也是必不可少的
Markdown將文本轉換爲 HTML。
KaTeX數學公式
您可以使用渲染LaTeX數學表達式 KaTeX:
Gamma公式展示 是通過歐拉積分
你可以找到更多關於的信息 LaTeX 數學表達式here.
新的甘特圖功能,豐富你的文章
- 關於 甘特圖 語法,參考 這兒,
UML 圖表
可以使用UML圖表進行渲染。 Mermaid. 例如下面產生的一個序列圖::
這將產生一個流程圖。:
- 關於 Mermaid 語法,參考 這兒,
FLowchart流程圖
我們依舊會支持flowchart的流程圖:
- 關於 Flowchart流程圖 語法,參考 這兒.
導出與導入
導出
如果你想嘗試使用此編輯器, 你可以在此篇文章任意編輯。當你完成了一篇文章的寫作, 在上方工具欄找到 文章導出 ,生成一個.md文件或者.html文件進行本地保存。
導入
如果你想加載一篇你寫過的.md文件或者.html文件,在上方工具欄可以選擇導入功能進行對應擴展名的文件導入,
繼續你的創作。
註腳的解釋 ↩︎