使用Animations動畫改變View的cornerRadius半徑

hello!大家好,今天想做一個動畫效果,就是把一個矩形UIImage(or UIView)通過動畫變成圓形。

開始是想這麼寫的,

    [UIView animateWithDuration:1.0f animations:^{
        //        NSLog(@"%f",x);
        _logo.layer.cornerRadius = _logo.frame.size.height/2;
        
    }];
結果妥妥的失敗,雖然變成了原形,但是卻沒有執行任何動畫。

ok,查一下文檔

可以參考這個文檔地址,然後你會發現,在“Table 4-1  Animatable UIView properties”表格裏,以上(代碼)的創建方式,和舊的創建方式只支持表格裏的屬性改變。

舊的創建方式如下,▼

[UIView beginAnimations:context:];
[UIView setAnimationDuration:];
// Change properties here...
[UIView commitAnimations];
支持的動畫的properties表格如下,▼

Property

Changes you can make

frame

Modify this property to change the view’s size and position relative to its superview’s coordinate system. (If the transformproperty does not contain the identity transform, modify the bounds or center properties instead.)

bounds

Modify this property to change the view’s size.

center

Modify this property to change the view’s position relative to its superview’s coordinate system.

transform

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.)

alpha

Modify this property to gradually change the transparency of the view.

backgroundColor

Modify this property to change the view’s background color.

contentStretch

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 class, you can use Core Animation and the view’s underlying layer to create the animation. Because view and layer objects are intricately linked together, changes to a view’s layer affect the view itself. Using Core Animation, you can animate the following types of changes for your view’s layer:

  • The size and position of the layer

  • The center point used when performing transformations

  • Transformations to the layer or its sublayers in 3D space

  • The addition or removal of a layer from the layer hierarchy

  • The layer’s Z-order relative to other sibling layers

  • The layer’s shadow

  • The layer’s border (including whether the layer’s corners are rounded)

  • The portion of the layer that stretches during resizing operations

  • The layer’s opacity

  • The clipping behavior for sublayers that lie outside the layer’s bounds

  • The current contents of the layer

  • The rasterization behavior of the layer

粗略的翻譯一下,就是

在你想進行更復雜的動畫,或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的運用不僅僅這麼簡單,大家可以自行了結~


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章