structure field:
keypath:
我们可以通过animationWithKeyPath键值对的方式来改变动画
animationWithKeyPath的值:
transform.scale = 比例轉換
transform.scale.x = 闊的比例轉換
transform.scale.y = 高的比例轉換
transform.rotation.z = 平面圖的旋轉
opacity = 透明度
margin
zPosition
backgroundColor
cornerRadius
borderWidth
bounds
contents
contentsRect
cornerRadius
frame
hidden
mask
masksToBounds
opacity
position
shadowColor
shadowOffset
shadowOpacity
shadowRadius
//
//
ViewController.m
//
CABasicAnimationDemo
//
//
Created by user on 13-1-23.
//
Copyright (c) 2013年 user. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)dealloc
{
[_animationKeyPaths release];
[_button release];
[_imageView release];
[super dealloc];
}
- (void)viewDidLoad
{
[super viewDidLoad];
//
@"translation",
//
@"translation.x",
//
@"translation.y",
//
@"translation.z",
//
//
@"rotation",
//
@"rotation.x",
//
@"rotation.y",
//
@"rotation.z",
//
@"scale",
//
@"scale.x",
//
@"scale.y",
//
@"scale.z",
_animationKeyPaths = [[NSArray alloc] initWithObjects:
@"opacity",
@"cornerRadius",
@"borderWidth",
@"zPosition",
@"backgroundColor",
@"bounds",
@"margin",
@"contents",
@"contentsRect",
@"mask",
@"masksToBounds",
@"frame",
@"hidden",
@"shadowColor",
@"shadowOffset",
@"shadowOpacity",
@"shadowRadius",
@"transform",
//旋转(CATransform3D)
@"transform.rotation.x",
@"transform.rotation.y",
@"transform.rotation.z",//旋转
@"transform.scale",
@"transform.translation",//点移动
@"position.x",
//横向移动
@"position.y",
//下面的不是CABasicAnimation里的keyPath
@"position",
//路径动画(CAKeyframeAnimation)
nil];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterf
aceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPo
rtraitUpsideDown);
}
- (IBAction)changeAnimationStyle:(UIButton *)button {
if (button == _button) {
static int i = 0;
[_imageView.layer removeAnimationForKey:_animationKeyPath];
//[_imageView.layer removeAllAnimations];
_animationKeyPath = [_animationKeyPaths objectAtIndex:i];
NSLog(@"%@", _animationKeyPath);
[_button setTitle:_animationKeyPath forState:UIControlStateNormal];
[_imageView.layer addAnimation:[self generateAnimation:i] forKey:_animationKeyPath];
i++;
if (i >= _animationKeyPaths.count) {
i = 0;
}
} else if (button == _groupBtn) {
[_imageView.layer removeAnimationForKey:_animationKeyPath];
//[_imageView.layer removeAllAnimations];
_animationKeyPath = @"GroupAnimation";
NSLog(@"%@", _animationKeyPath);
CAAnimationGroup *groupAnimation = [CAAnimationGroup animation];
groupAnimation.fillMode = kCAFillModeBackwards;
groupAnimation.removedOnCompletion = NO;
groupAnimation.duration = 10000.25f;
//groupAnimation.repeatCount = FLT_MAX;
//groupAnimation.autoreverses = YES;
[groupAnimation setAnimations:
[NSArray arrayWithObjects:
[self generateAnimation:0],
//[self generateAnimation:1],
//[self generateAnimation:2],
//[self generateAnimation:3],
//[self generateAnimation:4],
nil]];
//_imageView.layer.position = CGPointMake(_imageView.frame.size.width, _imageView.frame.size.height*0.5f);
groupAnimation = (CAAnimationGroup *)[self generateGroupAnimation];
[_imageView.layer addAnimation:groupAnimation forKey:_animationKeyPath];
}
}
- (CAAnimation *)generateGroupAnimation {
CALayer *layer=[CALayer layer];
layer.frame=CGRectMake(50, 200, 50, 50);
layer.backgroundColor=[UIColor orangeColor].CGColor;
layer.cornerRadius=8.0f;
CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
animation.duration=4.0f;
animation.autoreverses=NO;
animation.repeatCount=1;
animation.toValue=[NSNumber numberWithInt:-10];
animation.fromValue=[NSNumber numberWithInt:200];
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEa
seInEaseOut];
CABasicAnimation *animationZoomIn=[CABasicAnimation animationWithKeyPath:@"transform.scale"];
animationZoomIn.duration=2.0f;
animationZoomIn.autoreverses = YES;
//animationZoomIn.autoreverses=NO;
animationZoomIn.repeatCount=1;
animationZoomIn.toValue=[NSNumber numberWithFloat:1.56];
animationZoomIn.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEa
seIn];
CABasicAnimation *animationZoomOut=[CABasicAnimation animationWithKeyPath:@"transform.scale"];
//等待放大动画执行完
animationZoomOut.beginTime=2.0f;
animationZoomOut.duration=2.0f;
animationZoomOut.autoreverses=NO;
animationZoomOut.repeatCount=1;
animationZoomOut.toValue=[NSNumber numberWithFloat:.01];
animationZoomOut.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEa
seOut];
CAAnimationGroup *groupAnimation = [CAAnimationGroup animation];
groupAnimation.duration=4.0f;
groupAnimation.animations=[NSArray arrayWithObjects: animation, animationZoomIn, animationZoomOut,nil];
groupAnimation.removedOnCompletion=NO;
groupAnimation.fillMode=kCAFillModeForwards;
//[layer addAnimation:groupAnimation forKey:nil];
//[self.view.layer addSublayer:layer];
return groupAnimation;
}
- (CAAnimation *)generateAnimation:(int)i {
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:[_animationKeyPaths objectAtIndex:i]];
//执行时间
//反转动画
animation.autoreverses = YES;
//执行次数
animation.repeatCount = FLT_MAX;
animation.removedOnCompletion = NO;
//animation.fillMode = kCAFillModeForwards;
//动画开始结束的快慢,设置为加速
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEa
seInEaseOut];
if ([_animationKeyPath isEqualToString:@"opacity"]) {
animation.fromValue
= [NSNumber numberWithFloat:1.0f];
animation.toValue
= [NSNumber numberWithFloat:0.0f];
//animation.duration
= 1.0f;
} else if ([_animationKeyPath isEqualToString:@"transform.rotation.z"]) {
animation.fromValue
= [NSNumber numberWithFloat:0.0f];
animation.toValue
= [NSNumber numberWithFloat:M_PI_2];
animation.duration
= 1.0f;
animation.autoreverses = NO;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLi
near];
} else if ([_animationKeyPath isEqualToString:@"position"]){
CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:_animationKeyPath];
keyFrameAnimation.repeatCount = FLT_MAX;
keyFrameAnimation.autoreverses = NO;
keyFrameAnimation.duration = 4.0f;
//
坐标累加(可以实现前进一段距离再后退一段距离,然后重复)
keyFrameAnimation.cumulative = YES;
//CGPoint p1 = _imageView.center;
CGPoint p1 = CGPointMake(0, 0);
CGPoint p2 = CGPointMake(30, 30);
CGPoint p3 = CGPointMake(60, 60);
CGPoint p4 = CGPointMake(90, 90);
CGPoint p5 = CGPointMake(120, 120);
CGPoint p6 = CGPointMake(90, 90);
CGPoint p7 = CGPointMake(60, 60);
[keyFrameAnimation setValues:[NSArray arrayWithObjects:
[NSValue valueWithCGPoint:p1],
[NSValue valueWithCGPoint:p2],
[NSValue valueWithCGPoint:p3],
[NSValue valueWithCGPoint:p4],
[NSValue valueWithCGPoint:p5],
[NSValue valueWithCGPoint:p6],
[NSValue valueWithCGPoint:p7],
nil]];
[keyFrameAnimation setKeyTimes:[NSArray arrayWithObjects:
[NSNumber numberWithFloat:0.0],
[NSNumber numberWithFloat:0.3],
[NSNumber numberWithFloat:0.4],
[NSNumber numberWithFloat:0.5],
[NSNumber numberWithFloat:0.6],
[NSNumber numberWithFloat:0.7],
[NSNumber numberWithFloat:0.8],
nil]];
return keyFrameAnimation;
} else if ([_animationKeyPath isEqualToString:@"transform.translation"]) {
//point貌似是相对_imageView;
animation.toValue=[NSValue valueWithCGPoint:CGPointMake(200, -200)];
} else if ([_animationKeyPath isEqualToString:@"transform"]) {
CATransform3D rotationTransform
= CATransform3DMakeRotatio
n(45, 0, 0, 2);
animation.toValue= [NSValue valueWithCATransform3D:rotationTransform];
animation.duration = 2.0f;
animation.autoreverses = NO;
//角度累加
animation.cumulative = YES;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLi
near];
} else if ([_animationKeyPath isEqualToString:@"borderWidth"]) {
animation.fromValue
= [NSNumber numberWithFloat:0.0f];
animation.toValue
= [NSNumber numberWithFloat:4.0f];
animation.duration
= 3.0f;
} else if ([_animationKeyPath isEqualToString:@"cornerRadius"]) {
#pragma mark M
_imageView.layer.masksToBounds = YES;
_imageView.layer.cornerRadius = 10.0f;
_imageView.layer.borderWidth = 2.0f;
_imageView.layer.borderColor = [UIColor orangeColor].CGColor;
animation.fromValue
= [NSNumber numberWithFloat:0.0f];
animation.toValue
= [NSNumber numberWithFloat:20.0f];
animation.duration
= 1.0f;
} else if ([_animationKeyPath isEqualToString:@"bounds"]) {
animation.fromValue
= [NSValue valueWithCGRect:CGRectMake(0,0,10,10)];
animation.toValue
= [NSValue valueWithCGRect:CGRectMake(10,10,200,200)];
//animation.byValue
= [NSValue valueWithCGRect:self.view.bounds];
animation.duration
= 3.0f;
} else if ([_animationKeyPath isEqualToString:@"contents"]) {
animation.fromValue = (id)[UIImage imageNamed:@"home_module_apps_normal@2x.png"].CGImage;
animation.toValue = (id)[UIImage imageNamed:@"disk_scan_center@2x.png"].CGImage;
//无效果
//animation.toValue = (id)[UIColor redColor].CGColor;
//animation.byValue
= (id)[UIImage imageNamed:@"GuideArrow.png"].CGImage;
animation.duration
= 3.0f;
} else if ([_animationKeyPath isEqualToString:@"shadowColor"]) {
animation.fromValue
= (id)[UIColor redColor].CGColor;
animation.toValue
= (id)[UIColor blackColor].CGColor;
animation.duration
= 1.0f;
} else if ([_animationKeyPath isEqualToString:@"shadowOffset"]) {
animation.fromValue = [NSValue valueWithCGSize:CGSizeMake(0,0)];
animation.toValue = [NSValue valueWithCGSize:CGSizeMake(3,3)];
animation.duration
= 2.0f;
} else if ([_animationKeyPath isEqualToString:@"shadowOpacity"]) {
animation.fromValue
= [NSNumber numberWithFloat:0.5f];
animation.toValue
= [NSNumber numberWithFloat:1.0f];
animation.duration
= 1.0f;
} else if ([_animationKeyPath isEqualToString:@"shadowRadius"]) {
animation.fromValue
= [NSNumber numberWithFloat:10.f];
animation.toValue
= [NSNumber numberWithFloat:5.0f];
animation.duration
= 1.0f;
} else {
animation.fromValue
= [NSNumber numberWithFloat:0.0f];
animation.toValue
= [NSNumber numberWithFloat:400.0f];
animation.duration
= 3.0f;
//从当前位置开始
animation.additive
= YES;
}
return animation;
}
@end