CAKeyframeAnimation简单实用
2014-10-20 14:23
357 查看
//
// MJViewController.m
// 06-核心动画02-CAKeyframeAnimation
//
// Created by apple on 14-4-21.
// Copyright (c) 2014年 itcast. All rights reserved.
//
#import "MJViewController.h"
@interface MJViewController ()
@property (weak, nonatomic) IBOutlet UIView *redView;
@end
@implementation MJViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
anim.keyPath = @"position";
anim.removedOnCompletion = NO;
anim.fillMode = kCAFillModeForwards;
anim.duration = 2.0;
// 创建一个路径
CGMutablePathRef path = CGPathCreateMutable();
// 添加一个圆到这个路径
CGPathAddEllipseInRect(path, NULL, CGRectMake(100, 100, 200, 200));
anim.path = path;
// 释放这个路径(Create出来的对象都需要释放)
CGPathRelease(path);
// 设置动画的执行节奏
// kCAMediaTimingFunctionEaseInEaseOut : 一开始比较慢, 中间会加速, 临近结束的时候, 会变慢
anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
// 监听动画的执行
anim.delegate = self;
[self.redView.layer addAnimation:anim forKey:nil];
}
#pragma mark - 动画的代理方法
#pragma mark 动画开始的时候调用
- (void)animationDidStart:(CAAnimation *)anim
{
NSLog(@"animationDidStart");
}
#pragma mark 动画结束的时候调用
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
NSLog(@"animationDidStop");
}
- (void)testMove
{
// CABasicAnimation fromValue --> toValue
CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
anim.keyPath = @"position";
NSValue *v1 = [NSValue valueWithCGPoint:CGPointZero];
NSValue *v2 = [NSValue valueWithCGPoint:CGPointMake(100, 0)];
NSValue *v3 = [NSValue valueWithCGPoint:CGPointMake(100, 200)];
NSValue *v4 = [NSValue valueWithCGPoint:CGPointMake(0, 200)];
anim.values = @[v1, v2, v3, v4];
// anim.keyTimes = @[@(0.5), @(0.25), @(0.25)];
anim.duration = 2.0;
anim.removedOnCompletion = NO;
anim.fillMode = kCAFillModeForwards;
[self.redView.layer addAnimation:anim forKey:nil];
}
@end
// MJViewController.m
// 06-核心动画02-CAKeyframeAnimation
//
// Created by apple on 14-4-21.
// Copyright (c) 2014年 itcast. All rights reserved.
//
#import "MJViewController.h"
@interface MJViewController ()
@property (weak, nonatomic) IBOutlet UIView *redView;
@end
@implementation MJViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
anim.keyPath = @"position";
anim.removedOnCompletion = NO;
anim.fillMode = kCAFillModeForwards;
anim.duration = 2.0;
// 创建一个路径
CGMutablePathRef path = CGPathCreateMutable();
// 添加一个圆到这个路径
CGPathAddEllipseInRect(path, NULL, CGRectMake(100, 100, 200, 200));
anim.path = path;
// 释放这个路径(Create出来的对象都需要释放)
CGPathRelease(path);
// 设置动画的执行节奏
// kCAMediaTimingFunctionEaseInEaseOut : 一开始比较慢, 中间会加速, 临近结束的时候, 会变慢
anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
// 监听动画的执行
anim.delegate = self;
[self.redView.layer addAnimation:anim forKey:nil];
}
#pragma mark - 动画的代理方法
#pragma mark 动画开始的时候调用
- (void)animationDidStart:(CAAnimation *)anim
{
NSLog(@"animationDidStart");
}
#pragma mark 动画结束的时候调用
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
NSLog(@"animationDidStop");
}
- (void)testMove
{
// CABasicAnimation fromValue --> toValue
CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
anim.keyPath = @"position";
NSValue *v1 = [NSValue valueWithCGPoint:CGPointZero];
NSValue *v2 = [NSValue valueWithCGPoint:CGPointMake(100, 0)];
NSValue *v3 = [NSValue valueWithCGPoint:CGPointMake(100, 200)];
NSValue *v4 = [NSValue valueWithCGPoint:CGPointMake(0, 200)];
anim.values = @[v1, v2, v3, v4];
// anim.keyTimes = @[@(0.5), @(0.25), @(0.25)];
anim.duration = 2.0;
anim.removedOnCompletion = NO;
anim.fillMode = kCAFillModeForwards;
[self.redView.layer addAnimation:anim forKey:nil];
}
@end
相关文章推荐
- CAKeyframeAnimation的简单使用
- CAKeyFrameAnimation 的简单用法
- CAKeyframeAnimation(关键帧动画)的简单使用
- CAKeyframeAnimation的简单使用
- IOS Animation-CAKeyframeAnimation例子(简单动画实现)
- CABaseAnimation + CAAnimationGroup + CAKeyframeAnimation的简单动画
- CAKeyframeAnimation简单旋转动画
- CAKeyframeAnimation ---关键帧动画
- CAKeyframeAnimation 动画使用 开始、暂停动画
- CAKeyframeAnimation 关键帧动画的用法
- CAKeyframeAnimation path 绘制动画 和 《CALayer 自定义属性绘制动画》 的比较
- CAKeyframeAnimation
- Core Animation学习笔记三:CAkeyframeAnimation
- CAKeyframeAnimation 关键帧动画
- CAKeyFrameAnimation 关键帧动画
- CAKeyFrameAnimation 关键帧动画
- CAKeyframeAnimation——关键帧动画和动画组~
- <原>关键帧动画CAKeyframeAnimation
- CAKeyframeAnimation
- CoreAnimation-CAKeyframeAnimation