IOS開發之CLAyer 隱式動畫

每一個uiview都默認關聯著一個CALayer,我們成這個layer為root layer
所有的非root layer都存在默認的隱私動畫,隱式動畫默認為1/4秒。

如果關閉默認的動畫效果:可以通過動畫的事務方法實現。

[CATransaction beign]
[CATransaction setDisableActions:Yes]
[CATransaction commit]

隱式動畫的創建

//實例化自定義圖層
    CALayer *myLayer = [CALayer layer];
    //設置大小
    [myLayer setBounds:CGRectMake(0, 0, 100, 100)];
    //設置背景顏色
    [myLayer setBackgroundColor:[UIColor redColor].CGColor];
    [myLayer setPosition:CGPointMake(50, 50)];
    [self.view.layer addSublayer:myLayer];

在觸摸事件touchesBegan事件中創建動畫。完成位置變化,顏色,透明度,尺寸,圓角等操作。


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = touches.anyObject;
    CGPoint location = [touch locationInView:self.view];
    //關閉動畫
//    [CATransaction begin];
//    [CATransaction setDisableActions:YES];
//    [CATransaction commit];
    //位置
    [self.mylayer setPosition:location];
    //顏色
    NSInteger r1 = arc4random_uniform(self.colorArray.count);
    [self.mylayer setBackgroundColor:[self.colorArray[r1] CGColor]];
    //透明度
    CGFloat alpha = (arc4random_uniform(5) + 1.0) / 10.0 + 0.5;
    [self.mylayer setOpacity:alpha];
    //尺寸
    NSInteger size = arc4random_uniform(50) + 51;
    [self.mylayer setBounds:CGRectMake(0, 0, size, size)];
    //圓角
    NSInteger r2 = arc4random_uniform(30);
    [self.mylayer setCornerRadius:r2];
    //旋轉角度
    CGFloat angle = arc4random_uniform(180) /180.0 * M_PI;
    [self.mylayer setTransform:CATransform3DMakeRotation(angle, 0, 0, 1)];
    //設置content
    NSInteger r3 = arc4random_uniform(self.imageArray.count);
    UIImage *image = self.imageArray[r3];
    [self.mylayer setContents:(id)image.CGImage];
}

CALayer還有很多的屬性可以進行設置,為蘋果的文檔中搜索:CALayer Animatable Properties ,查看更多。

以下摘自文檔

Table B-1 Layer properties and their default animations
Property
Default animation
anchorPoint
Uses the default implied CABasicAnimation object, described in Table B-2.
backgroundColor
Uses the default implied CABasicAnimation object, described in Table B-2.
backgroundFilters
Uses the default implied CATransition object, described in Table B-3. Sub-properties of the filters are animated using the default implied CABasicAnimation object, described in Table B-2.
borderColor
Uses the default implied CABasicAnimation object, described in Table B-2.
borderWidth
Uses the default implied CABasicAnimation object, described in Table B-2.
bounds
Uses the default implied CABasicAnimation object, described in Table B-2.
compositingFilter
Uses the default implied CATransition object, described in Table B-3. Sub-properties of the filters are animated using the default implied CABasicAnimation object, described in Table B-2.
contents
Uses the default implied CABasicAnimation object, described in Table B-2.
contentsRect
Uses the default implied CABasicAnimation object, described in Table B-2.
cornerRadius
Uses the default implied CABasicAnimation object, described in Table B-2.
doubleSided
There is no default implied animation.
filters
Uses the default implied CABasicAnimation object, described in Table B-2. Sub-properties of the filters are animated using the default implied CABasicAnimation object, described in Table B-2.
frame
This property is not animatable. You can achieve the same results by animating the bounds and position properties.
hidden
Uses the default implied CABasicAnimation object, described in Table B-2.
mask
Uses the default implied CABasicAnimation object, described in Table B-2.
masksToBounds
Uses the default implied CABasicAnimation object, described in Table B-2.
opacity
Uses the default implied CABasicAnimation object, described in Table B-2.
position
Uses the default implied CABasicAnimation object, described in Table B-2.
shadowColor
Uses the default implied CABasicAnimation object, described in Table B-2.
shadowOffset
Uses the default implied CABasicAnimation object, described in Table B-2.
shadowOpacity
Uses the default implied CABasicAnimation object, described in Table B-2.
shadowPath
Uses the default implied CABasicAnimation object, described in Table B-2.
shadowRadius
Uses the default implied CABasicAnimation object, described in Table B-2.
sublayers
Uses the default implied CABasicAnimation object, described in Table B-2.
sublayerTransform
Uses the default implied CABasicAnimation object, described in Table B-2.
transform
Uses the default implied CABasicAnimation object, described in Table B-2.
zPosition
Uses the default implied CABasicAnimation object, described in Table B-2.

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容