沖浪.gif
前言:
工作中遇到?jīng)_浪需求甩牺,于是找了很多資料做參考蘑志,最后集成了一個工具類
一、實現(xiàn)思路
- 用正弦函數(shù)贬派,計算波浪上的點用
UIBezierPath
的moveToPoint
和addLineToPoint
連接成線急但,
- 用定時器DisplayLink作為動力源
- 停止波浪: 停止定時器并賦值為nil
- 開啟波浪: 新建定時器,并
setNeedsDisplay
二搞乏、詳細代碼
1. 創(chuàng)建
- 提供的構(gòu)造方法
構(gòu)造方法 構(gòu)造方法將自動開啟沖浪不需要手動調(diào)用開啟
/**
* 沖浪視圖的類構(gòu)造方法
* @param colorMutableArray 顏色數(shù)組
* @param progress 高度或進度波桩,占self.frame.size.height得百分比
*/
+(instancetype)waveViewWithFrame:(CGRect)frame andColorSet: (NSMutableArray <UIColor *>*)colorMutableArray andProgress: (CGFloat)progress;
/**
* 沖浪視圖的類構(gòu)造方法
* @param colorMutableArray 顏色數(shù)組
* @param progress 高度或進度,占self.frame.size.height得百分比
*/
-(instancetype)initWithFrame:(CGRect)frame andColorSet: (NSMutableArray <UIColor *>*)colorMutableArray andProgress: (CGFloat)progress;
- 默認的構(gòu)方法要手動打開沖浪模式
2.開啟 & 停止沖浪
是否沖浪(設(shè)置成YES開始沖浪请敦,設(shè)置成NO停止沖浪镐躲。 默認為NO)
@property (nonatomic,assign) BOOL isWaveStart;
3.炫酷的擴展性
- 顏色
///需要畫出的顏色數(shù)組,可以隨時添加,內(nèi)部在繪圖的時候?qū)?shù)組進行了遍歷
@property (nonatomic,strong) NSMutableArray<UIColor *> *colorMutableArray;
- 海水的高度 (可以作為下載進度)
高度或進度侍筛,占self.frame.size.height得百分比
@property (nonatomic,assign) CGFloat progress;
- 水波的參數(shù)
1.振幅 (水波的振幅)
@property (nonatomic, assign) CGFloat amplitude;
2.水波的周期
@property (nonatomic, assign) CGFloat cycle;
3.兩個波水平之間偏移的距離
@property (nonatomic, assign) CGFloat distanceH;
4.兩個波豎直之間偏移
@property (nonatomic, assign) CGFloat distanceV;
5.水波的速率(默認0.1)
@property (nonatomic, assign) CGFloat waveScale;
- 形狀的擴展
1.自定義形狀
@property (nonatomic, strong) UIBezierPath *bazierPath;
2.形狀類型匀油,默認是圓形(分為矩形和圓形兩種,如果設(shè)置了bazierPath屬性勾笆,則優(yōu)先按照bazierPath路徑獲取形狀)
@property (nonatomic,assign) PYWaveViewPathType pathType;
3.關(guān)于形狀的枚舉
typedef enum : NSUInteger {
PYWaveViewPathType_CIRCULAR = 0,//圓形
PYWaveViewPathType_RECT = 1,//矩形
} PYWaveViewPathType;
話不多說敌蚜,源碼代碼請看這里