一箱靴、前言
- 每一個(gè)UIView內(nèi)部都默認(rèn)關(guān)聯(lián)著一個(gè)CALayer對(duì)象突委,我們可用稱(chēng)這個(gè)Layer為
Root Layer
(根層) - 所有的非RootLayer根層,也就是手動(dòng)創(chuàng)建的CALayer對(duì)象侣集,都存在著隱式動(dòng)畫(huà)
二坛增、什么是隱式動(dòng)畫(huà)?
- 當(dāng)對(duì)非RootLayer的部分屬性進(jìn)行修改時(shí)雕欺,默認(rèn)會(huì)自動(dòng)產(chǎn)生一些動(dòng)畫(huà)效果
- 而這些屬性稱(chēng)為AnimatableProperties(可動(dòng)畫(huà)屬性)
- 也就是說(shuō)岛马,對(duì)非根層的layer可動(dòng)畫(huà)屬性進(jìn)行修改產(chǎn)生的動(dòng)畫(huà),就稱(chēng)為隱式動(dòng)畫(huà)
三屠列、可動(dòng)畫(huà)屬性 Animatable Properties
如上所述:我們可以修改可動(dòng)畫(huà)屬性啦逆,來(lái)進(jìn)行隱式動(dòng)畫(huà),現(xiàn)在介紹可動(dòng)畫(huà)屬性
查看CALayer頭文件:查看部分可動(dòng)畫(huà)屬性
哪些才可以稱(chēng)著脸哀,可動(dòng)畫(huà)屬性呢蹦浦? -> 屬性,注釋中包含
Animatable
的-
常見(jiàn)可動(dòng)畫(huà)屬性
- bounds:用于設(shè)置CALayer的寬度和高度撞蜂。修改這個(gè)屬性會(huì)產(chǎn)生縮放動(dòng)畫(huà)
- backgroundColor:用于設(shè)置CALayer的背景色盲镶。修改這個(gè)屬性會(huì)產(chǎn)生背景色的漸變動(dòng)畫(huà)
- position:用于設(shè)置CALayer的位置。修改這個(gè)屬性會(huì)產(chǎn)生平移動(dòng)畫(huà)
- ............
// 查看部分頭文件蝌诡,還有其他的可動(dòng)畫(huà)屬性不在此
/* The bounds of the layer. Defaults to CGRectZero. Animatable. */
@property CGRect bounds;
/* The position in the superlayer that the anchor point of the layer's
* bounds rect is aligned to. Defaults to the zero point. Animatable. */
@property CGPoint position;
/* The Z component of the layer's position in its superlayer. Defaults
* to zero. Animatable. */
@property CGFloat zPosition;
/* Defines the anchor point of the layer's bounds rect, as a point in
* normalized layer coordinates - '(0, 0)' is the bottom left corner of
* the bounds rect, '(1, 1)' is the top right corner. Defaults to
* '(0.5, 0.5)', i.e. the center of the bounds rect. Animatable. */
@property CGPoint anchorPoint;
/* The Z component of the layer's anchor point (i.e. reference point for
* position and transform). Defaults to zero. Animatable. */
@property CGFloat anchorPointZ;
/* A transform applied to the layer relative to the anchor point of its
* bounds rect. Defaults to the identity transform. Animatable. */
@property CATransform3D transform;
/* Convenience methods for accessing the `transform' property as an
* affine transform. */
- (CGAffineTransform)affineTransform;
- (void)setAffineTransform:(CGAffineTransform)m;
/* Unlike NSView, each Layer in the hierarchy has an implicit frame
* rectangle, a function of the `position', `bounds', `anchorPoint',
* and `transform' properties. When setting the frame the `position'
* and `bounds.size' are changed to match the given frame. */
@property CGRect frame;
/* When true the layer and its sublayers are not displayed. Defaults to
* NO. Animatable. */
@property(getter=isHidden) BOOL hidden;
四 隱式動(dòng)畫(huà) 實(shí)現(xiàn) : CATransaction(動(dòng)畫(huà)事務(wù))
- 可以通過(guò)動(dòng)畫(huà)事務(wù)(CATransaction)關(guān)閉默認(rèn)的隱式動(dòng)畫(huà)效果
[CATransactionbegin];
[CATransactionsetDisableActions:YES];
self.myview.layer.position= CGPointMake(10,10);
[CATransactioncommit];
五溉贿、實(shí)例
-
實(shí)例:
隱式動(dòng)畫(huà).gif- 點(diǎn)擊屏幕,讓控制器上的view視圖上的一個(gè)矩形子控件浦旱,不斷地進(jìn)行改變:位置宇色,顏色,與圓角
思路:其實(shí)很簡(jiǎn)單颁湖,在touchBegin:方法中 -> 就是不斷修改layer的可動(dòng)畫(huà)屬性實(shí)現(xiàn)的
代碼實(shí)現(xiàn):如下
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, weak) CALayer *greenLayer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 添加 一個(gè)自定義的layer到控制器的view的layer上
CALayer *layer = [CALayer layer];
_greenLayer = layer;
layer.position = CGPointMake(0, 0);
layer.bounds = CGRectMake(0, 0, 100, 100);
layer.anchorPoint = CGPointMake(0, 0);
layer.backgroundColor = [UIColor greenColor].CGColor;
[self.view.layer addSublayer:layer];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// 開(kāi)啟事務(wù)
[CATransaction begin];
// [CATransaction setDisableActions:YES];
// [CATransaction setAnimationDuration:2];
_greenLayer.position = CGPointMake(arc4random_uniform(250), arc4random_uniform(300));
_greenLayer.backgroundColor = [self randomColor].CGColor;
_greenLayer.cornerRadius = arc4random_uniform(50);
_greenLayer.borderColor = [self randomColor].CGColor;
_greenLayer.borderWidth = arc4random_uniform(5);
// 提交事務(wù)
[CATransaction commit];
}
/**
* 返回 隨機(jī)色
*/
- (UIColor *)randomColor
{
CGFloat r = arc4random_uniform(256) / 255.0;
CGFloat g = arc4random_uniform(256) / 255.0;
CGFloat b = arc4random_uniform(256) / 255.0;
return [UIColor colorWithRed:r green:g blue:b alpha:1];
}
@end
```