-
首先我們有必要先了解下CALayer對象。CALayer包含在QuartzCore框架中赃梧,這是一個跨平臺的框架紧憾,既可以用在iOS中又可以用在Mac OS X中结笨。在使用Core Animation開發(fā)動畫的本質(zhì)就是將CALayer中的內(nèi)容轉(zhuǎn)化為位圖從而供硬件操作反惕。
Snip20160819_1.png -
CALayer常用屬性
在iOS中CALayer的設(shè)計主要是了為了內(nèi)容展示和動畫操作尝艘,CALayer本身并不包含在UIKit中,它不能響應(yīng)事件姿染。由于CALayer在設(shè)計之初就考慮它的動畫操作功能背亥,CALayer很多屬性在修改時都能形成動畫效果,這種屬性稱為“隱式動畫屬性”悬赏。但是對于UIView的根圖層而言屬性的修改并不形成動畫效果隘梨,因為很多情況下根圖層更多的充當容器的做用,如果它的屬性變動形成動畫效果會直接影響子圖層舷嗡。另外轴猎,UIView的根圖層創(chuàng)建工作完全由iOS負責完成,無法重新創(chuàng)建进萄,但是可以往根圖層中添加子圖層或移除子圖層
Snip20160819_3.png
以上這些都是支持“隱式動畫”
- (void)viewDidLoad {
[super viewDidLoad];
//實例化自定義圖層
self.myLayer = [CALayer layer];
//設(shè)置大小
[self.myLayer setBounds:CGRectMake(100, 100, 100, 100)];
//設(shè)置背景顏色
[self.myLayer setBackgroundColor:[UIColor redColor].CGColor];
// 中心點 Position 相當于View.center
[self.myLayer setPosition:CGPointMake(100, 100)];
self.myLayer.shadowColor = [UIColor orangeColor].CGColor;
self.myLayer.shadowOffset = CGSizeMake(10, 10);
[self.view.layer addSublayer:self.myLayer];
}
#warning - CALayer 屬性不能響應(yīng)事件
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
UITouch *touch = touches.anyObject;
CGPoint location = [touch locationInView:self.view];
//位置
[self.myLayer setPosition:location];
//尺寸
NSInteger size = arc4random_uniform(50) + 51;
[self.myLayer setBounds:CGRectMake(0, 0, size, size)];
//圓角
NSInteger rotation = arc4random_uniform(30);
[self.myLayer setCornerRadius:rotation];
//旋轉(zhuǎn)角度
CGFloat angle = arc4random_uniform(180) /180.0 * M_PI;
[self.myLayer setTransform:CATransform3DMakeRotation(angle, 0, 0, 1)];
self.myLayer.backgroundColor = [UIColor colorWithRed:arc4random_uniform(255.0)/255.0 green:arc4random_uniform(255.0)/255.0 blue:arc4random_uniform(255.0)/255.0 alpha:1].CGColor;
}
- UIView的根圖層創(chuàng)建工作完全由iOS負責完成捻脖,無法重新創(chuàng)建,但是可以往根圖層中添加子圖層或移除子圖層
Snip20160819_7.png
-
基礎(chǔ)動畫 - CABasicAnimation
Core Animation是蘋果公司給我們提供的一套強大的核心動畫類庫中鼠,而核心動畫又分為基礎(chǔ)動畫可婶、關(guān)鍵幀動畫、動畫組援雇、轉(zhuǎn)場動畫矛渴。這里只做 基礎(chǔ)動畫(CABasicAnimation)的筆記。
- CABasicAnimation:基礎(chǔ)動畫惫搏,通過屬性修改進行動畫參數(shù)控制具温,只有初始狀態(tài)和結(jié)束狀態(tài)
[super viewDidLoad];
self.view.backgroundColor=[UIColor orangeColor];
// 自定義一個圖層
_layer=[[CALayer alloc]init];
_layer.bounds=CGRectMake(0, 0, 30, 30);
_layer.position=CGPointMake(50, 150);
_layer.backgroundColor = [UIColor redColor].CGColor;
[self.view.layer addSublayer:_layer];
[self createYellowView];
}
- (void)createYellowView {
_yellowView = [[UIView alloc] initWithFrame:CGRectMake(100, 150, 30, 30)];
_yellowView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:_yellowView];
}
- (void)yellowRunPoint:(CGPoint)location {
[UIView animateWithDuration:3.0 animations:^{
_yellowView.center = location;
}];
}
#pragma mark 移動動畫
-(void)translatonAnimation:(CGPoint)location{
//1.創(chuàng)建動畫并指定動畫屬性
CABasicAnimation *basicAnimation=[CABasicAnimation animationWithKeyPath:@"position"];
//2.設(shè)置動畫屬性初始值和結(jié)束值
// basicAnimation.fromValue=[NSNumber numberWithInteger:50];//可以不設(shè)置,默認為圖層初始狀態(tài)
basicAnimation.toValue=[NSValue valueWithCGPoint:location];
//設(shè)置其他動畫屬性
basicAnimation.duration=3.0;//動畫時間5秒
// basicAnimation.repeatCount=HUGE_VALF;//設(shè)置重復(fù)次數(shù),HUGE_VALF可看做無窮大筐赔,起到循環(huán)動畫的效果
//basicAnimation.removedOnCompletion=YES;//運行一次是否移除動畫
//3.添加動畫到圖層铣猩,注意key相當于給動畫進行命名,以后獲得該動畫時可以使用此名稱獲取
[_layer addAnimation:basicAnimation forKey:@"BasicAnimation_Translation"];
}
#pragma mark 點擊事件
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch=touches.anyObject;
CGPoint location= [touch locationInView:self.view];
//創(chuàng)建并開始動畫
[self translatonAnimation:location];
[self yellowRunPoint:location];
}
-
紅色VIew是和黃色View的動畫效果是一樣的茴丰,所紅色VIew是黃色View的系統(tǒng)封裝方法的實現(xiàn)
先寫這么多达皿,寫博客太累。贿肩。峦椰。