iOS Objective-C基本核心动画,偏移,旋转,缩放,路径,抖动,组动画
2016-04-11 00:05
435 查看
/*
主要思路就是创建CABasicAnimation、CAKeyframeAnimation、CAAnimationGroup对象,编写相应的动画效果后添加到view的layer里面。
*/
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, strong) CALayer *redLayer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.redLayer = [CALayer layer];
self.redLayer.bounds = CGRectMake(0, 0, 100, 100);
self.redLayer.position = CGPointMake(100, 100);
self.redLayer.backgroundColor =
[UIColor redColor].CGColor;
[self.view.layer addSublayer:self.redLayer];
// [self test01];
// [self test02];
// [self test03];
// [self test04];
// [self test05];
[self test06];
}
//组动画:多个动画同时进行
- (void) test06{
CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
//1.路径
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(100, 100, 200, 100)].CGPath;
//2.旋转
CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
rotationAnimation.byValue = @(M_PI * 2);
//3.缩放
CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transfrom.scale"];
scaleAnimation.toValue = @(0.5);
animationGroup.animations = @[pathAnimation, rotationAnimation, scaleAnimation];
animationGroup.duration = 3;
animationGroup.repeatCount = CGFLOAT_MAX;
[self.redLayer addAnimation:animationGroup forKey:nil];
}
//抖动
- (void) test05{
CAKeyframeAnimation *keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
keyAnimation.values = @[@(-M_PI_4/4), @(M_PI_4/4), @(-M_PI_4/4)];
keyAnimation.repeatCount = CGFLOAT_MAX;
// keyAnimation.duration = 0.25; //all animation's duration is 0.25
[self.redLayer addAnimation:keyAnimation forKey:nil];
}
//按指定路径走的动画
- (void) test04{
CAKeyframeAnimation *keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
keyAnimation.path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(100, 100, 200, 100)].CGPath;
keyAnimation.duration = 3;
[self.redLayer addAnimation:keyAnimation forKey:nil];
}
//基本动画,旋转
- (void) test03 {
CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
basicAnimation.toValue = @(M_PI * 2);
basicAnimation.repeatCount = 10;
basicAnimation.removedOnCompletion = NO;
basicAnimation.fillMode = kCAFillModeForwards;
[self.redLayer addAnimation:basicAnimation forKey:nil];
}
//基本动画,缩放
- (void) test02 {
CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
//缩小小于1, 放大大于1
basicAnimation.toValue = @0.5;
basicAnimation.duration = 3;
basicAnimation.removedOnCompletion = NO;
basicAnimation.fillMode = kCAFillModeForwards;
[self.redLayer addAnimation:basicAnimation forKey:nil];
}
//基本动画,移动
- (void) test01{
CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
basicAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(300, 300)];
basicAnimation.duration = 3;
basicAnimation.removedOnCompletion = NO;
basicAnimation.fillMode = kCAFillModeForwards;
[self.redLayer addAnimation:basicAnimation forKey:nil];
}
@end
主要思路就是创建CABasicAnimation、CAKeyframeAnimation、CAAnimationGroup对象,编写相应的动画效果后添加到view的layer里面。
*/
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, strong) CALayer *redLayer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.redLayer = [CALayer layer];
self.redLayer.bounds = CGRectMake(0, 0, 100, 100);
self.redLayer.position = CGPointMake(100, 100);
self.redLayer.backgroundColor =
[UIColor redColor].CGColor;
[self.view.layer addSublayer:self.redLayer];
// [self test01];
// [self test02];
// [self test03];
// [self test04];
// [self test05];
[self test06];
}
//组动画:多个动画同时进行
- (void) test06{
CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
//1.路径
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(100, 100, 200, 100)].CGPath;
//2.旋转
CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
rotationAnimation.byValue = @(M_PI * 2);
//3.缩放
CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transfrom.scale"];
scaleAnimation.toValue = @(0.5);
animationGroup.animations = @[pathAnimation, rotationAnimation, scaleAnimation];
animationGroup.duration = 3;
animationGroup.repeatCount = CGFLOAT_MAX;
[self.redLayer addAnimation:animationGroup forKey:nil];
}
//抖动
- (void) test05{
CAKeyframeAnimation *keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
keyAnimation.values = @[@(-M_PI_4/4), @(M_PI_4/4), @(-M_PI_4/4)];
keyAnimation.repeatCount = CGFLOAT_MAX;
// keyAnimation.duration = 0.25; //all animation's duration is 0.25
[self.redLayer addAnimation:keyAnimation forKey:nil];
}
//按指定路径走的动画
- (void) test04{
CAKeyframeAnimation *keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
keyAnimation.path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(100, 100, 200, 100)].CGPath;
keyAnimation.duration = 3;
[self.redLayer addAnimation:keyAnimation forKey:nil];
}
//基本动画,旋转
- (void) test03 {
CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
basicAnimation.toValue = @(M_PI * 2);
basicAnimation.repeatCount = 10;
basicAnimation.removedOnCompletion = NO;
basicAnimation.fillMode = kCAFillModeForwards;
[self.redLayer addAnimation:basicAnimation forKey:nil];
}
//基本动画,缩放
- (void) test02 {
CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
//缩小小于1, 放大大于1
basicAnimation.toValue = @0.5;
basicAnimation.duration = 3;
basicAnimation.removedOnCompletion = NO;
basicAnimation.fillMode = kCAFillModeForwards;
[self.redLayer addAnimation:basicAnimation forKey:nil];
}
//基本动画,移动
- (void) test01{
CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
basicAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(300, 300)];
basicAnimation.duration = 3;
basicAnimation.removedOnCompletion = NO;
basicAnimation.fillMode = kCAFillModeForwards;
[self.redLayer addAnimation:basicAnimation forKey:nil];
}
@end
相关文章推荐
- Swift项目中调用Objecttive-C
- 什么是writeObject 和readObject?可定制的序列化过程(转)
- iOS开发67- valueForKey:和objectForKey:的区别
- libcudnn.so.6.5:cannot open sharedobject file: No such file or directory
- Objective-C中nil与release的区别与用法
- objective-c中自己创建的对象为什么不能调用release
- poj 3241 Object Clustering 曼哈顿最小生成树
- Swift项目里使用Objective-C第三方库的总结
- Android属性动画完全解析(中),ValueAnimator和ObjectAnimator的高级用法
- Object类到底实现了哪些方法
- 重构第23天 引用参数对象(Introduce Parameter Object)
- Object-C 介绍
- WaitForMultipleObjects使用详解
- Objective-C 链式编程思想
- python 绘图添加图例显示UserWarning: Legend does not support [<matplotlib.lines.Line2D object at 0x07FA3210>]
- Ry’s Objective-C 教程
- Windows下的Objective-C集成开发环境(IDE)的搭建
- Objective-C @encode关键字
- OAViewObject中clearCache(),reset(),setMaxFetchSize(-1)的使用
- 第一章 熟悉Objective-C