#import "ViewController.h"
@interface ViewController ()
{
CAShapeLayer *shapeLayer;
CALayer *layer;
UIView *view;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
shapeLayer = [CAShapeLayer layer];
shapeLayer.frame = self.view.bounds;
shapeLayer.strokeColor = [UIColor redColor].CGColor;
shapeLayer.fillColor = [UIColor greenColor].CGColor;
[self.view.layer addSublayer:shapeLayer];
layer = [CALayer layer];
[self.view.layer addSublayer:layer];
layer.frame = CGRectMake(0, 100, 100, 100);
layer.backgroundColor = [UIColor redColor].CGColor;
view = [[UIView alloc]initWithFrame:CGRectMake(0, 200, 100, 100)];
[self.view addSubview:view];
view.backgroundColor = [UIColor cyanColor];
// Do any additional setup after loading the view, typically from a nib.
}
//隐式动画
- (void)animation
{
[UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
view.frame = CGRectMake(100, 200, 100, 100);
} completion:nil];
[CATransaction begin];
//时间曲线
[CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
layer.frame = CGRectMake(100, 100, 100, 100);
[CATransaction commit];
}
//基本动画
- (void)basicAnimation
{
CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
basicAnimation.fromValue = (__bridge id)([UIColor cyanColor].CGColor);
basicAnimation.toValue = (__bridge id)([UIColor yellowColor].CGColor);
basicAnimation.duration = 3;
basicAnimation.delegate = self;
[layer addAnimation:basicAnimation forKey:@"animation"];
layer.backgroundColor = [UIColor yellowColor].CGColor;
}
//关键帧动画
- (void)keyFramePathAnimation
{
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, nil, 0, 350);
CGPathAddLineToPoint(path, nil, 100, 350);
CGPathAddArc(path, nil, 100, 350, 100, -M_PI_2, M_PI_2, NO);
shapeLayer.path = path;
CAKeyframeAnimation *keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
keyAnimation.duration = 5;
keyAnimation.path = path;
[layer addAnimation:keyAnimation forKey:@"animation1"];
CGPathRelease(path);
}
//过度动画
- (void)transition
{
CATransition *transition = [CATransition animation];
//`fade', `moveIn', `push' and `reveal'
transition.type = @"reveal";
//`fromLeft', `fromRight', `fromTop' and* `fromBottom'
transition.subtype = @"fromLeft";
transition.startProgress = 0;
transition.endProgress = 1;
transition.duration = 10;
[layer addAnimation:transition forKey:@"animation2"];
}
- (IBAction)didStarClicked:(id)sender {
// [self animation];
// [self basicAnimation];
// [self keyFramePathAnimation];
[self transition];
}
- (IBAction)didReductionClicked:(id)sender {
layer.frame = CGRectMake(0, 100, 100, 100);
view.frame = CGRectMake(0, 200, 100, 100);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
@interface ViewController ()
{
CAShapeLayer *shapeLayer;
CALayer *layer;
UIView *view;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
shapeLayer = [CAShapeLayer layer];
shapeLayer.frame = self.view.bounds;
shapeLayer.strokeColor = [UIColor redColor].CGColor;
shapeLayer.fillColor = [UIColor greenColor].CGColor;
[self.view.layer addSublayer:shapeLayer];
layer = [CALayer layer];
[self.view.layer addSublayer:layer];
layer.frame = CGRectMake(0, 100, 100, 100);
layer.backgroundColor = [UIColor redColor].CGColor;
view = [[UIView alloc]initWithFrame:CGRectMake(0, 200, 100, 100)];
[self.view addSubview:view];
view.backgroundColor = [UIColor cyanColor];
// Do any additional setup after loading the view, typically from a nib.
}
//隐式动画
- (void)animation
{
[UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
view.frame = CGRectMake(100, 200, 100, 100);
} completion:nil];
[CATransaction begin];
//时间曲线
[CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
layer.frame = CGRectMake(100, 100, 100, 100);
[CATransaction commit];
}
//基本动画
- (void)basicAnimation
{
CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
basicAnimation.fromValue = (__bridge id)([UIColor cyanColor].CGColor);
basicAnimation.toValue = (__bridge id)([UIColor yellowColor].CGColor);
basicAnimation.duration = 3;
basicAnimation.delegate = self;
[layer addAnimation:basicAnimation forKey:@"animation"];
layer.backgroundColor = [UIColor yellowColor].CGColor;
}
//关键帧动画
- (void)keyFramePathAnimation
{
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, nil, 0, 350);
CGPathAddLineToPoint(path, nil, 100, 350);
CGPathAddArc(path, nil, 100, 350, 100, -M_PI_2, M_PI_2, NO);
shapeLayer.path = path;
CAKeyframeAnimation *keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
keyAnimation.duration = 5;
keyAnimation.path = path;
[layer addAnimation:keyAnimation forKey:@"animation1"];
CGPathRelease(path);
}
//过度动画
- (void)transition
{
CATransition *transition = [CATransition animation];
//`fade', `moveIn', `push' and `reveal'
transition.type = @"reveal";
//`fromLeft', `fromRight', `fromTop' and* `fromBottom'
transition.subtype = @"fromLeft";
transition.startProgress = 0;
transition.endProgress = 1;
transition.duration = 10;
[layer addAnimation:transition forKey:@"animation2"];
}
- (IBAction)didStarClicked:(id)sender {
// [self animation];
// [self basicAnimation];
// [self keyFramePathAnimation];
[self transition];
}
- (IBAction)didReductionClicked:(id)sender {
layer.frame = CGRectMake(0, 100, 100, 100);
view.frame = CGRectMake(0, 200, 100, 100);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end