Objective-C 学习记录-2

Objective-C 实现简单动画的步骤:

 

#import "ViewController.h"

@interface ViewController ()

@property (strong, nonatomic) CALayer *ALayer;  //定义一个名为ALayer的图层

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.ALayer = [CALayer layer];  //初始化
    
    self.ALayer.bounds = CGRectMake(50, 50, 100, 100);  //设置ALayer的边界
    
    //self.ALayer.position = CGPointMake(0, 0);   //设置起始位置
    
    self.ALayer.backgroundColor = [UIColor blackColor].CGColor;  //设置背景颜色
    
    [self.view.layer addSublayer:self.ALayer];  //显示ALayer
    
    [self positionAnimate];  //显示动画
    
}


- (void)positionAnimate
{

    CAKeyframeAnimation *positionChange = [CAKeyframeAnimation	animationWithKeyPath:@"position"];  //使用CAKeyframeAnimation建立一个有关键帧的移动动画
    
    CGMutablePathRef path2 = CGPathCreateMutable();  //创建一个可改变路径path2
    CGPathMoveToPoint(path2, NULL, 0, 0);  //设置起点为0,0
    //CGPathAddQuadCurveToPoint(path2,NULL,100,100,300,300);  //
    CGPathAddLineToPoint(path2, NULL , 300, 0);  //设置路径横向移动300
   
    positionChange.path = path2;  //将path2设置为动画路径
    //positionChange.delegate = self;
    
    CGPathRelease(path2);  //释放路径
    
    positionChange.duration = 1;  //设置动画速度为1
    
    [self.ALayer addAnimation:positionChange forKey:nil];  //将动画添加进ALayer
    
}

参考资料:https://blog.csdn.net/sinat_31177681/article/details/51116413

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值