#import#import "FKViewController.h"
@implementation FKViewController
CALayer *imageLayer;
- (void)viewDidLoad
{
[super viewDidLoad];
// 創建一個CALayer對象
imageLayer = [CALayer layer];
// 設置該CALayer的邊框、大小、位置等屬性
imageLayer.cornerRadius = 6;
imageLayer.borderWidth = 1;
imageLayer.borderColor = [UIColor blackColor].CGColor;
imageLayer.masksToBounds = YES;
imageLayer.frame = CGRectMake(30, 30, 100, 135);
// 設置該imageLayer顯示的圖片
imageLayer.contents = (id)[[UIImage imageNamed:@"android"] CGImage];
[self.view.layer addSublayer:imageLayer];
NSArray* bnTitleArray = [NSArray arrayWithObjects:@"位移"
, @"旋轉" , @"縮放" , @"動畫組" , nil];
// 獲取屏幕的內部高度
CGFloat totalHeight = [UIScreen mainScreen].bounds.size.height;
NSMutableArray* bnArray = [[NSMutableArray alloc] init];
// 采用循環創建4個按鈕
for(int i = 0 ; i < 4 ; i++)
{
UIButton* bn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
bn.frame = CGRectMake(5 + i * 80, totalHeight - 45 - 20 , 70 , 35);
[bn setTitle:[bnTitleArray objectAtIndex:i]
forState:UIControlStateNormal];
[bnArray addObject:bn];
[self.view addSubview:bn];
}
// 為4個按鈕綁定不同的事件處理方法
[[bnArray objectAtIndex:0] addTarget:self action:@selector(move:)
forControlEvents:UIControlEventTouchUpInside];
[[bnArray objectAtIndex:1] addTarget:self action:@selector(rotate:)
forControlEvents:UIControlEventTouchUpInside];
[[bnArray objectAtIndex:2] addTarget:self action:@selector(scale:)
forControlEvents:UIControlEventTouchUpInside];
[[bnArray objectAtIndex:3] addTarget:self action:@selector(group:)
forControlEvents:UIControlEventTouchUpInside];
}
-(void) move:(id)sender
{
CGPoint fromPoint = imageLayer.position;
CGPoint toPoint = CGPointMake(fromPoint.x + 80 , fromPoint.y);
// 創建不斷改變CALayer的position屬性的屬性動畫
CABasicAnimation* anim = [CABasicAnimation
animationWithKeyPath:@"position"];
// 設置動畫開始的屬性值
anim.fromValue = [NSValue valueWithCGPoint:fromPoint];
// 設置動畫結束的屬性值
anim.toValue = [NSValue valueWithCGPoint:toPoint];
anim.duration = 0.5;
imageLayer.position = toPoint;
anim.removedOnCompletion = YES;
// 為imageLayer添加動畫
[imageLayer addAnimation:anim forKey:nil];
}
-(void) rotate:(id)sender
{
// 創建不斷改變CALayer的transform屬性的屬性動畫
CABasicAnimation* anim = [CABasicAnimation animationWithKeyPath:@"transform"];
CATransform3D fromValue = imageLayer.transform;
// 設置動畫開始的屬性值
anim.fromValue = [NSValue valueWithCATransform3D:fromValue];
// 繞X軸旋轉180度
CATransform3D toValue = CATransform3DRotate(fromValue, M_PI , 1 , 0 , 0);
// 繞Y軸旋轉180度
// CATransform3D toValue = CATransform3DRotate(fromValue, M_PI , 0 , 1 , 0);
// // 繞Z軸旋轉180度
// CATransform3D toValue = CATransform3DRotate(fromValue, M_PI , 0 , 0 , 1);
// 設置動畫結束的屬性值
anim.toValue = [NSValue valueWithCATransform3D:toValue];
anim.duration = 0.5;
imageLayer.transform = toValue;
anim.removedOnCompletion = YES;
// 為imageLayer添加動畫
[imageLayer addAnimation:anim forKey:nil];
}
-(void) scale:(id)sender
{
// 創建不斷改變CALayer的transform屬性的屬性動畫
CAKeyframeAnimation* anim = [CAKeyframeAnimation
animationWithKeyPath:@"transform"];
// 設置CAKeyframeAnimation控制transform屬性依次經過的屬性值
anim.values = [NSArray arrayWithObjects:
[NSValue valueWithCATransform3D:imageLayer.transform],
[NSValue valueWithCATransform3D:CATransform3DScale
(imageLayer.transform , 0.2, 0.2, 1)],
[NSValue valueWithCATransform3D:CATransform3DScale
(imageLayer.transform, 2, 2 , 1)],
[NSValue valueWithCATransform3D:imageLayer.transform], nil];
anim.duration = 5;
anim.removedOnCompletion = YES;
// 為imageLayer添加動畫
[imageLayer addAnimation:anim forKey:nil];
}
-(void) group:(id)sender
{
CGPoint fromPoint = imageLayer.position;
CGPoint toPoint = CGPointMake(280 , fromPoint.y + 300);
// 創建不斷改變CALayer的position屬性的屬性動畫
CABasicAnimation* moveAnim = [CABasicAnimation
animationWithKeyPath:@"position"];
// 設置動畫開始的屬性值
moveAnim.fromValue = [NSValue valueWithCGPoint:fromPoint];
// 設置動畫結束的屬性值
moveAnim.toValue = [NSValue valueWithCGPoint:toPoint];
moveAnim.removedOnCompletion = YES;
// 創建不斷改變CALayer的transform屬性的屬性動畫
CABasicAnimation* transformAnim = [CABasicAnimation
animationWithKeyPath:@"transform"];
CATransform3D fromValue = imageLayer.transform;
// 設置動畫開始的屬性值
transformAnim.fromValue = [NSValue valueWithCATransform3D: fromValue];
// 創建縮放為X、Y兩個方向上縮放為0.5的變換矩陣
CATransform3D scaleValue = CATransform3DScale(fromValue , 0.5 , 0.5, 1);
// 繞Z軸旋轉180度的變換矩陣
CATransform3D rotateValue = CATransform3DRotate(fromValue, M_PI , 0 , 0 , 1);
// 計算兩個變換矩陣的和
CATransform3D toValue = CATransform3DConcat(scaleValue, rotateValue);
// 設置動畫技術的屬性值
transformAnim.toValue = [NSValue valueWithCATransform3D:toValue];
// 動畫效果累加
transformAnim.cumulative = YES;
// 動畫重復執行2次,旋轉360度
transformAnim.repeatCount = 2;
transformAnim.duration = 3;
// 位移、縮放、旋轉組合起來執行
CAAnimationGroup *animGroup = [CAAnimationGroup animation];
animGroup.animations = [NSArray arrayWithObjects:moveAnim
, transformAnim , nil];
animGroup.duration = 6;
// 為imageLayer添加動畫
[imageLayer addAnimation:animGroup forKey:nil];
}
@end