关键帧动画
关键帧动画和基本动画一样。创建步骤都是
1 实例化动画对象
2 设置属性
3 添加到图层。
如果使用了路径,values属性就被忽略。
对于位移动画,需要在动画结束时候,设置视图位置。在创建动画的时候需要设置代理。然后在动画结束代理程序里设置位置。
//
// 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) {
[selfsetBackgroundColor:[UIColorredColor]];
}
returnself;
}
- (void)moveto:(CGPoint)point
{
CAKeyframeAnimation *animation = [CAKeyframeAnimationanimationWithKeyPath:@"position"];
NSValue *p1 = [NSValuevalueWithCGPoint:self.center];
NSValue *p2 = [NSValuevalueWithCGPoint: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
贝塞尔曲线
//
// 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) {
[selfsetBackgroundColor:[UIColorredColor]];
}
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)moveByBeiSaiEr2:(CGPoint)point withTime:(CFTimeInterval)time
{
CAKeyframeAnimation *animation = [CAKeyframeAnimationanimationWithKeyPath:@"position"];
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, nil, self.center.x, self.center.y);
CGPoint midpoint = [self randomPoint];
CGPoint midpoint2 = [self randomPoint];
CGPathAddCurveToPoint(path, nil, midpoint.x, midpoint.y, midpoint2.x,midpoint2.y, point.x, point.y);
[animation setPath:path];
CGPathRelease(path);
[animation setDuration:2.0f];
[animation setDelegate:self];
[animation setValue:[NSValuevalueWithCGPoint:point] forKey:@"targetPoint"];
[animation setValue:@"translationTo"forKey:@"animationType"];
[self.layer addAnimation:animation forKey:@"translationTo"];
}
//单点贝塞尔路径动画
- (void)moveByBeiSaiEr:(CGPoint)point withTime:(CFTimeInterval)time
{
CAKeyframeAnimation *animation = [CAKeyframeAnimationanimationWithKeyPath:@"position"];
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, nil, self.center.x, self.center.y);
CGPoint midpoint = [self randomPoint];
CGPathAddQuadCurveToPoint(path, nil, midpoint.x, midpoint.y, point.x, point.y);
[animation setPath:path];
CGPathRelease(path);
[animation setDuration:2.0f];
[animation setDelegate:self];
[animation setValue:[NSValuevalueWithCGPoint:point] forKey:@"targetPoint"];
[animation setValue:@"translationTo"forKey:@"animationType"];
[self.layer addAnimation:animation forKey:@"translationTo"];
}
//根据路径动画
- (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 = [NSMutableArrayarrayWithCapacity:num + 2];
[array addObject:[NSValuevalueWithCGPoint:self.center]];
for (NSInteger i=0; i<num; i++) {
NSValue *p = [NSValue valueWithCGPoint:[self randomPoint]];
[array addObject:p];
}
[array addObject:[NSValuevalueWithCGPoint:point]];
[animation setValues:array];
[animation setDelegate:self];
[animation setValue:[NSValuevalueWithCGPoint:point] forKey:@"targetPoint"];
[animation setValue:@"translationTo"forKey:@"animationType"];
[self.layer addAnimation:animation forKey:@"myframe"];
}
- (void)moveto:(CGPoint)point
{
CAKeyframeAnimation *animation = [CAKeyframeAnimationanimationWithKeyPath:@"position"];
NSValue *p1 = [NSValuevalueWithCGPoint:self.center];
NSValue *p2 = [NSValuevalueWithCGPoint: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