您的位置:首页 > 移动开发 > Objective-C

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: