如果使用路径动画,设置的关键点values就失去作用。
0)创建动画
1) 创建路径
2)设置路径内容
3)将路径添加到动画
4)释放路径
5)将动画添加到图层
//
// AnimationView.m
// 关键帧动画_demo1
//
// Created by mac on 13-9-30.
// Copyright (c) 2013年 mac. All rights reserved.
//
#import "AnimationView.h"
#import <QuartzCore/QuartzCore.h>
@implementation AnimationView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setBackgroundColor:[UIColor redColor]];
}
returnself;
}
- (CGPoint)randomPoint
{
CGSize size = self.superview.bounds.size;
CGFloat x = arc4random_uniform(size.width);
CGFloat y = arc4random_uniform(size.height);
CGPoint point = CGPointMake(x, y);
return point;
}
- (void)moveByRectToPoint:(CGPoint)point withTime:(CFTimeInterval)time
{
CAKeyframeAnimation *animation = [CAKeyframeAnimationanimationWithKeyPath:@"position"];
CGRect rect = CGRectMake(self.center.x, self.center.y, point.x - self.center.x, point.y - self.center.y);
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, nil, rect);
[animation setDuration:time];
[animation setPath:path];
CGPathRelease(path);//释放路径,路径一定要释放
[self.layer addAnimation:animation forKey:@"mypath"];
}
- (void)moveToPoint:(CGPoint)point withduration:(CFTimeInterval)time withpointnum:(NSInteger)num
{
CAKeyframeAnimation *animation = [CAKeyframeAnimationanimationWithKeyPath:@"position"];
[animation setDuration:time];
NSMutableArray *array = [NSMutableArray arrayWithCapacity:num + 2];
[array addObject:[NSValue valueWithCGPoint:self.center]];
for (NSInteger i=0; i<num; i++) {
NSValue *p = [NSValue valueWithCGPoint:[self randomPoint]];
[array addObject:p];
}
[array addObject:[NSValue valueWithCGPoint:point]];
[animation setValues:array];
[animation setDelegate:self];
[animation setValue:[NSValue valueWithCGPoint:point] forKey:@"targetPoint"];
[animation setValue:@"translationTo" forKey:@"animationType"];
[self.layer addAnimation:animation forKey:@"myframe"];
}
- (void)moveto:(CGPoint)point
{
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
NSValue *p1 = [NSValue valueWithCGPoint:self.center];
NSValue *p2 = [NSValue valueWithCGPoint:CGPointMake(0, 0)];
NSValue *p3 = [NSValue valueWithCGPoint:point];
[animation setValues:@[p1,p2,p3]];
[animation setDuration:2.0f];
[animation setDelegate:self];
[animation setValue:p3 forKey:@"targetPoint"];
[animation setValue:@"translationTo" forKey:@"animationType"];
[self.layer addAnimation:animation forKey:@"mykeyframe"];
}
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
NSString *animationType = [anim valueForKey:@"animationType"];
if ([animationType isEqualToString:@"translationTo"]) {
CGPoint point = [[anim valueForKey:@"targetPoint"] CGPointValue];
[self setCenter:point];
}
}
@end
晃动关键帧动画
- (void)shakeAnimatioin:(CFTimeInterval)time
{
CAKeyframeAnimation *animaton = [CAKeyframeAnimationanimationWithKeyPath:@"transform.rotation.z"];
CGFloat angle1 = M_PI_4/7;
CGFloat angle2 = -angle1;
[animaton setValues:@[@(angle1),@(angle2),@(angle1)]];
[animaton setDuration:time];
[animaton setRepeatCount:HUGE_VALF];
[self.layer addAnimation:animaton forKey:@"myrotation"];
}