貝塞爾曲線的基礎(chǔ)我們了解了,接下來驶忌,我們開始自己做一些動(dòng)畫效果擂仍。
那么配合貝塞爾曲線的一般使用關(guān)鍵幀動(dòng)畫囤屹。為啥呢?因?yàn)槲蚁矚g啊,哈哈哈哈逢渔!
我們之前使用過CATransition,CATransition直接繼承于CAAnimation肋坚。
CAAnimation 簡(jiǎn)單來說是從一個(gè)值到另一個(gè)值得變化。(例如我們?cè)O(shè)置某個(gè)視圖的顏色為紅色,然后在動(dòng)畫中設(shè)置結(jié)果為綠色,那么就會(huì)顯示這兩種值得變化)
而我們今天用的動(dòng)畫CAKeyframeAnimation繼承于CAPropertyAnimation肃廓,CAPropertyAnimation雖然也繼承于CAAnimation,但是CAPropertyAnimation在CAAnimation基礎(chǔ)上增加了一種路徑屬性的設(shè)置智厌。我們不再僅限于改變兩個(gè)值的變化,而是路徑中不同坐標(biāo)點(diǎn)的變化盲赊。
接下來我們就可以了解一下: CAKeyframeAnimation
附加官網(wǎng)文檔地址:
https://developer.apple.com/documentation/quartzcore/cakeyframeanimation?language=objc
@property(nullable, copy) NSArray *values;
這個(gè)屬性我們?cè)谥暗男男膭?dòng)畫中已經(jīng)運(yùn)用到多了铣鹏。簡(jiǎn)單來說就是:
@[@1,@3,@4,@5] 我們使用CAAnimation可以會(huì)設(shè)置屬性值@1和@3之間的變化,而我們利用values就可以
逐個(gè)的從@1依次到@5產(chǎn)生變化哀蘑。
這個(gè)屬性是只有path屬性為nil的時(shí)候才有效果
我們簡(jiǎn)單的使用代碼實(shí)現(xiàn)一下:
CAKeyframeAnimation *keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
keyAnimation.values = @[@1,@2,@3,@4,@3,@2,@1];
keyAnimation.duration = 3.0f;
[self.label.layer addAnimation:keyAnimation forKey:@"transform.scale"];
可以看到,我們?cè)O(shè)置了視圖的transform.scale 即縮放屬性诚卸。然后設(shè)置它值得變化是@1,@2,@3,@4,@3,@2,@1,也就是從小到大绘迁,然后從大到小合溺。
@property(nullable) CGPathRef path;
這個(gè)屬性就是我們?cè)O(shè)置了path之后,視圖會(huì)跟著路徑去移動(dòng)缀台。
我們做個(gè)簡(jiǎn)單的路徑動(dòng)畫:
先繪制一個(gè)Z字型路徑:
_bezier = [UIBezierPath bezierPath];
[_bezier moveToPoint:(CGPoint){0,100}];
[_bezier addLineToPoint:(CGPoint){kSCREENWIDTH,100}];
[_bezier addLineToPoint:(CGPoint){0,200}];
[_bezier addLineToPoint:(CGPoint){kSCREENWIDTH,200}];
[_bezier closePath];
[_bezier setLineWidth:0.2];
然后將繪制好的路徑設(shè)置到動(dòng)畫當(dāng)中
//路徑動(dòng)畫
CAKeyframeAnimation *keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
keyAnimation.duration = 6.0f;
keyAnimation.path = _bezier.CGPath;
[self.label.layer addAnimation:keyAnimation forKey:@"position"];
@property(nullable, copy) NSArray<NSNumber *> *keyTimes;
官網(wǎng)中這樣寫道:
https://developer.apple.com/documentation/quartzcore/cakeyframeanimation/1412522-keytimes?language=objc
Each value in the array is a floating point number between 0.0 and 1.0 that defines the time point (specified as a fraction of the animation’s total duration) at which to apply the corresponding keyframe value.
數(shù)組中的每個(gè)值都是介于0.0和1.0之間的浮點(diǎn)數(shù)棠赛,它定義了時(shí)間點(diǎn)(指定為動(dòng)畫總長(zhǎng)度的一小部分),用于應(yīng)用對(duì)應(yīng)的關(guān)鍵幀值。
Each successive value in the array must be greater than, or equal to, the previous value.
數(shù)組中的每一個(gè)連續(xù)值都必須大于或等于前面的值
該屬性是一個(gè)數(shù)組恭朗,每個(gè)值分別對(duì)應(yīng)每個(gè)子路徑(AB,BC,CD,DE)的時(shí)間屏镊。
如果你沒有顯式地對(duì)keyTimes進(jìn)行設(shè)置,則系統(tǒng)會(huì)默認(rèn)每條子路徑的時(shí)間為:ti=duration/(幀數(shù))痰腮,即每條子路徑的duration相等
我們?cè)谏鲜龅膭?dòng)畫中加入該屬性之后而芥,觀看一下效果:
//路徑動(dòng)畫
CAKeyframeAnimation *keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
keyAnimation.path = _bezier.CGPath;
keyAnimation.keyTimes = @[@(0.0f),@(0.1f),@(0.2f),@(0.3f),@(0.4f)];
keyAnimation.duration = 6.0f;
[self.label.layer addAnimation:keyAnimation forKey:@"position"];
可以看到我們?cè)谠O(shè)置了對(duì)應(yīng)的速度之后,它們的時(shí)候速度明顯變快了.
@property(copy) NSString *calculationMode;
計(jì)算模式在關(guān)鍵幀動(dòng)畫中也非常重要,效果比較類似于下面說到的timingFunctions。
它一般可以配合keyTimes屬性進(jìn)行設(shè)置膀值。在官網(wǎng)介紹keyTimes的時(shí)候有這樣一段話:
The appropriate values to include in the array are dependent on the calculationMode property.
在數(shù)組中包含的適當(dāng)值依賴于計(jì)算模式屬性棍丐。
If the calculationMode is set to kCAAnimationLinear or kCAAnimationCubic,
the first value in the array must be 0.0 and the last value must be 1.0.
All intermediate values represent time points between the start and end times.
如果將計(jì)算模式設(shè)置為kCAAnimationLinear或kCAAnimationCubic,那么數(shù)組中的第一個(gè)值必須是0.0沧踏,
最后一個(gè)值必須是1.0歌逢。所有中間值都表示開始和結(jié)束時(shí)間之間的時(shí)間點(diǎn)。
If the calculationMode is set to kCAAnimationDiscrete,
the first value in the array must be 0.0 and the last value must be 1.0.
The array should have one more entry than appears in the values array. For example,
if there are two values, there should be three key times.
如果將計(jì)算模式設(shè)置為kCAAnimationDiscrete翘狱,那么數(shù)組中的第一個(gè)值必須是0.0秘案,最后一個(gè)值必須是1.0。
該數(shù)組應(yīng)該有一個(gè)比在值數(shù)組中顯示的還要多的條目潦匈。例如阱高,如果有兩個(gè)值,則應(yīng)該有三個(gè)關(guān)鍵時(shí)刻茬缩。
If the calculationMode is set to kCAAnimationPaced or kCAAnimationCubicPaced, the values in this property are ignored.
如果將計(jì)算模式設(shè)置為kCAAnimationPaced或kCAAnimationCubicPaced赤惊,那么該屬性的值就會(huì)被忽略。
If the values in this array are invalid or inappropriate for the current calculation mode, they are ignored.
如果該數(shù)組中的值無效或不適合當(dāng)前計(jì)算模式凰锡,則會(huì)忽略它們未舟。
它的值如下所示:
默認(rèn)值,表示當(dāng)關(guān)鍵幀為坐標(biāo)點(diǎn)的時(shí)候,關(guān)鍵幀之間線性運(yùn)動(dòng);
CA_EXTERN NSString * const kCAAnimationLinear
CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0);
所有關(guān)鍵幀直接逐個(gè)運(yùn)動(dòng);
CA_EXTERN NSString * const kCAAnimationDiscrete
CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0);
使得動(dòng)畫均勻進(jìn)行,而不是按keyTimes設(shè)置的或者按關(guān)鍵幀平分時(shí)間,此時(shí)keyTimes和timingFunctions無效;
CA_EXTERN NSString * const kCAAnimationPaced
CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0);
對(duì)關(guān)鍵幀為坐標(biāo)點(diǎn)的關(guān)鍵幀進(jìn)行圓滑曲線相連后插值計(jì)算,對(duì)于曲線的形狀還可以通過tensionValues,continuityValues,biasValues來進(jìn)行調(diào)整自定義,
這里的主要目的是使得運(yùn)行的軌跡變得圓滑;
CA_EXTERN NSString * const kCAAnimationCubic
CA_AVAILABLE_STARTING (10.7, 4.0, 9.0, 2.0);
在kCAAnimationCubic的基礎(chǔ)上使得動(dòng)畫運(yùn)行變得均勻,
就是系統(tǒng)時(shí)間內(nèi)運(yùn)動(dòng)的距離相同,此時(shí)keyTimes以及timingFunctions也是無效的.
CA_EXTERN NSString * const kCAAnimationCubicPaced
CA_AVAILABLE_STARTING (10.7, 4.0, 9.0, 2.0);
@property(nullable, copy) NSArray<CAMediaTimingFunction *> *timingFunctions;
設(shè)置它可以影響動(dòng)畫的階段速度。
//線性速度,各階段保持勻速
CA_EXTERN NSString * const kCAMediaTimingFunctionLinear
CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0);
//淡入 也就是開頭會(huì)慢一點(diǎn)
CA_EXTERN NSString * const kCAMediaTimingFunctionEaseIn
CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0);
//淡出 結(jié)束的時(shí)候會(huì)慢
CA_EXTERN NSString * const kCAMediaTimingFunctionEaseOut
CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0);
//淡入淡出 開頭和結(jié)束的時(shí)候會(huì)慢
CA_EXTERN NSString * const kCAMediaTimingFunctionEaseInEaseOut
CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0);
//默認(rèn)
CA_EXTERN NSString * const kCAMediaTimingFunctionDefault
CA_AVAILABLE_STARTING (10.6, 3.0, 9.0, 2.0);
下面的三個(gè)值都是配合 calculationMode 的 kCAAnimationCubic值進(jìn)行使用的掂为。用來調(diào)整自定義的曲線
我們可以通過官網(wǎng)的解釋來了解一下裕膀,有空的話我也會(huì)做一些demo來直觀了解一下。
@property(nullable, copy) NSArray<NSNumber *> *tensionValues;
一組NSNumber對(duì)象定義了曲線的緊密性勇哗。
This property is used only for the cubic calculation modes. Positive values indicate a tighter curve while negative values indicate a rounder curve. The first value defines the behavior of the tangent to the first control point, the second value controls the second point’s tangents, and so on. If you do not specify a value for a given control point, the value 0 is used.
該屬性僅用于立方計(jì)算模式魂角。正值表示一條更緊的曲線,而負(fù)值則表示一條圓曲線智绸。第一個(gè)值定義了第一個(gè)控制點(diǎn)的切線的行為野揪,第二個(gè)值控制了第二個(gè)點(diǎn)的切線,以此類推瞧栗。如果不指定給定控制點(diǎn)的值斯稳,則使用值0。
@property(nullable, copy) NSArray<NSNumber *> *continuityValues;
一組NSNumber對(duì)象迹恐,它定義了時(shí)間曲線的銳度挣惰。
This property is used only for the cubic calculation modes. Positive values result in sharper corners while negative values create inverted corners. The first value defines the behavior of the tangent to the first control point, the second value controls the second point’s tangents, and so on. If you do not specify a value for a given control point, the value 0 is used.
該屬性僅用于cubic calculation modes。正值會(huì)導(dǎo)致更尖銳的角落,而負(fù)值則會(huì)產(chǎn)生倒角憎茂。第一個(gè)值定義了第一個(gè)控制點(diǎn)的切線的行為珍语,第二個(gè)值控制了第二個(gè)點(diǎn)的切線,以此類推竖幔。如果不指定給定控制點(diǎn)的值板乙,則使用值0。
@property(nullable, copy) NSArray<NSNumber *> *biasValues;
一組NSNumber對(duì)象拳氢,它定義了相對(duì)于控制點(diǎn)的曲線的位置募逞。
This property is used only for the cubic calculation modes. Positive values move the curve before the control point while negative values move it after the control point. The first value defines the behavior of the tangent to the first control point, the second value controls the second point’s tangents, and so on. If you do not specify a value for a given control point, the value 0 is used.
該屬性僅用于cubic calculation modes。正值在控制點(diǎn)前移動(dòng)曲線馋评,而負(fù)值則在控制點(diǎn)之后移動(dòng)放接。第一個(gè)值定義了第一個(gè)控制點(diǎn)的切線的行為,第二個(gè)值控制了第二個(gè)點(diǎn)的切線留特,以此類推纠脾。如果不指定給定控制點(diǎn)的值,則使用值0蜕青。
@property(nullable, copy) NSString *rotationMode;
旋轉(zhuǎn)樣式
根據(jù)路徑自動(dòng)旋轉(zhuǎn)
CA_EXTERN NSString * const kCAAnimationRotateAuto
CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0);
根據(jù)路徑自動(dòng)翻轉(zhuǎn)
CA_EXTERN NSString * const kCAAnimationRotateAutoReverse
CA_AVAILABLE_STARTING (10.5, 2.0, 9.0, 2.0);
好吧乳乌,圖片總是上傳失敗,簡(jiǎn)單來說就是市咆,圖像根據(jù)路徑移動(dòng)的時(shí)候是會(huì)調(diào)整自身的方向的,不那么刻板的一直是擺在正中間那樣的效果再来。