rotationMode属性的强大

本文介绍如何利用iOS中的Core Animation来创建一个沿贝塞尔路径移动并改变颜色的动画效果。通过创建CAShapeLayer和CALayer,定义路径动画和颜色变化动画,并组合成动画组应用于图层。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    //create a path

    UIBezierPath *bezierPath = [[UIBezierPathalloc]init];

    [bezierPath moveToPoint:CGPointMake(0,150)];

    [bezierPath addCurveToPoint:CGPointMake(300,150)controlPoint1:CGPointMake(75,0)controlPoint2:CGPointMake(225,300)];

    //draw the path using a CAShapeLayer

    CAShapeLayer *pathLayer = [CAShapeLayerlayer];

    pathLayer.path = bezierPath.CGPath;

    pathLayer.fillColor = [UIColorclearColor].CGColor;

    pathLayer.strokeColor = [UIColorredColor].CGColor;

    pathLayer.lineWidth =3.0f;

    [self.view.layeraddSublayer:pathLayer];

    

    //add a colored layer

    CALayer *colorLayer = [CALayerlayer];

    colorLayer.frame =CGRectMake(0,0,64,64);

    colorLayer.position =CGPointMake(0,150);

    colorLayer.backgroundColor = [UIColorgreenColor].CGColor;

    [self.view.layeraddSublayer:colorLayer];

    

    //create the position animation

    CAKeyframeAnimation *animation1 = [CAKeyframeAnimationanimation];

    animation1.keyPath =@"position";

    animation1.path = bezierPath.CGPath;

    animation1.rotationMode =kCAAnimationRotateAuto;

    

    //create the color animation

    CABasicAnimation *animation2 = [CABasicAnimationanimation];

    animation2.keyPath =@"backgroundColor";

    animation2.toValue = (__bridgeid)[UIColorredColor].CGColor;

    //create group animation

    CAAnimationGroup *groupAnimation = [CAAnimationGroupanimation];

    groupAnimation.animations =@[animation1, animation2];

    groupAnimation.duration =4.0;

    groupAnimation.removedOnCompletion =NO;

    groupAnimation.fillMode =kCAFillModeForwards;

    //add the animation to the color layer

    [colorLayer addAnimation:groupAnimationforKey:nil];





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值