hello!大家好,今天想做一個動畫效果,就是把一個矩形UIImage(or UIView)通過動畫變成圓形。
開始是想這麼寫的,
[UIView animateWithDuration:1.0f animations:^{
// NSLog(@"%f",x);
_logo.layer.cornerRadius = _logo.frame.size.height/2;
}];
結果妥妥的失敗,雖然變成了原形,但是卻沒有執行任何動畫。
ok,查一下文檔
可以參考這個文檔地址,然後你會發現,在“ Animatable UIView
properties”表格裏,以上(代碼)的創建方式,和舊的創建方式只支持表格裏的屬性改變。
舊的創建方式如下,▼
[UIView beginAnimations:context:];
[UIView setAnimationDuration:];
// Change properties here...
[UIView commitAnimations];
支持的動畫的properties表格如下,▼
Property |
Changes you can make |
---|---|
Modify this property to change the view’s size and position relative to its superview’s coordinate system. (If the |
|
Modify this property to change the view’s size. |
|
Modify this property to change the view’s position relative to its superview’s coordinate system. |
|
Modify this property to scale, rotate, or translate the view relative to its center point. Transformations using this property are always performed in 2D space. (To perform 3D transformations, you must animate the view’s layer object using Core Animation.) |
|
Modify this property to gradually change the transparency of the view. |
|
Modify this property to change the view’s background color. |
|
Modify this property to change the way the view’s contents are stretched to fill the available space |
In places where you want to perform more sophisticated animations, or animations not supported by the
|
在你想進行更復雜的動畫,或UIView類不支持動畫,你可以使用Core Animation和視圖的基礎層創建動畫。因爲視圖和層對象是錯綜複雜的聯繫在一起,對一個視圖層的影響視圖本身的變化。使用Core Animation,您可以爲您的視圖層設置以下類型的更改:
層的大小和位置
執行轉換時所使用的中心點
轉換層或在三維空間中的子層
層從層的層中的添加或移除
圖層的順序相對於其他同級層
圖層的陰影
該層的邊界(包括該層的角是否是圓形的)
該部分的層延伸在調整操作
層的不透明度
裁剪行爲層之外的邊界層
層的當前內容
該層的光柵化行爲
------------------------------
CABasicAnimation, CAAnimationGroup, CAKeyframeAnimation等就是依賴於Core Animation的動畫,這裏我使用CABasicAnimation來實現修改View的圓角,代碼如下
<span style="font-size:10px;">CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"cornerRadius"];
animation.duration = 0.5f;
[_logo.layer setCornerRadius:_logo.frame.size.height/2];
[_logo.layer addAnimation:animation forKey:@"cornerRadius"];</span>
實現:)
Core
Animation的運用不僅僅這麼簡單,大家可以自行了結~