Animation(动画)

本文深入探讨了使用Objective-C和Swift语言在iOS开发中实现动画技术的关键概念,包括隐式动画、基本动画、关键帧动画和过渡动画的原理与实践。通过实例演示了如何创建和应用各种动画效果,旨在帮助开发者掌握移动应用中的动画设计技巧。
#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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值