36

贝塞尔曲线的动画运用

 6 years ago
source link: http://www.cocoachina.com/ios/20190315/26569.html?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

背景:因为公司需要做等级切换查看对应的权益,需要做一个曲线运动的一个动画形式,现决定用贝塞尔曲线的动画和CAKeyframeAnimation 动画的形式实现

上图
Yzay2iv.gif

grade.gif

制作曲线路径

  • 不想用 - (void)drawRect:(CGRect)rect 的形式画圆,我这里就直接用CAShapeLayer写了。

    首先绘制一个半圆形当背景,使用CAShapeLayer 和 UIBezierPath绘制曲线路径,声明圆的半径 radius 圆心所在的点centerPoint。

//懒加载
- (CAShapeLayer *)shapeLayer
{
    if (!_shapeLayer) {
        _shapeLayer = [CAShapeLayer layer];
        _shapeLayer.path = self.bezierPath.CGPath;
        _shapeLayer.lineWidth = 3;
        _shapeLayer.fillColor = [UIColor clearColor].CGColor;
        _shapeLayer.strokeColor = [UIColor colorWithWhite:1 alpha:0.6].CGColor;
    }
    return _shapeLayer;
}

-(UIBezierPath *)bezierPath
{
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithArcCenter:self.centerPoint
                                   radius:self.radius
                               startAngle:0
                                 endAngle:M_PI
                                clockwise:YES];
    return bezierPath;
}
  • 创建等级对应的数据模型,我这里使用的json本地数据进行建模了

   // 获取文件路径
    NSString *path = [[NSBundle mainBundle] pathForResource:@"grade" ofType:@"json"];
    // 将文件数据化
    NSData *data = [[NSData alloc] initWithContentsOfFile:path];
    // 对数据进行JSON格式化并返回字典形式
    NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
  • 这里的动画形式主要是通过贝塞尔的角度计算的

       double angle = gradeInfo.angle.doubleValue;
        CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
        animation.keyPath = @"position";
        animation.duration = 0.6;
        animation.removedOnCompletion = NO;
        animation.fillMode = kCAFillModeForwards;
        // 设置贝塞尔曲线路径
        UIBezierPath *circylePath = [[UIBezierPath alloc] init];
        [circylePath addArcWithCenter:self.centerPoint radius:self.radius startAngle:angle *M_PI endAngle:(angle +angleTrend)*M_PI clockwise:clockwise];
        animation.path = circylePath.CGPath;
  • 动画执行后真实的所在位置并不会发生改变,我们需要对动画完成后对当前的图片做位置修改

   //位置纠正
        CGFloat a = (gradeInfo.angle.floatValue + angleTrend) * M_PI;
        CGPoint point = CGPointMake(self.centerPoint.x + cos(a) *(self.radius), self.centerPoint.y + sin(a) *(self.radius));
        gradeInfo.iconImage.center = point;

好了,结束!

GitHub地址: https://github.com/zyaxuan/NTGrade

作者:只因为趁年轻
链接:https://www.jianshu.com/p/412dd8e33d13


Recommend

  • 128
    • 掘金 juejin.im 7 years ago
    • Cache

    Android 自定义贝塞尔曲线工具

    Android 自定义贝塞尔曲线工具 之前在学习贝塞尔曲线的相关内容,在查找相关资料的时候发现网上的资料重复的太多了,而且因为android的canvas只提供了quadTo,cubicTo两种方法来绘制二阶和三阶的贝塞尔曲线.在线的贝塞尔曲线绘制网站也很少

  • 46
    • 掘金 juejin.im 6 years ago
    • Cache

    深入理解贝塞尔曲线

    贝塞尔曲线(Bezier Curve)在计算机图形领域应用非常广泛,比如我们熟知的 CSS 动画、 Canvas 以及 Photoshop 等都可以看到贝塞尔曲线的身影。 文章目录 一、什么是贝塞尔曲线? 二、贝塞尔曲线分为哪些类型? 三、贝塞尔曲线是如何

  • 35

    话说为什么笔者我要求虐去研究什么贝塞尔曲线?讲真,我一个数学一般般,高数及格飘过的人为什么要求虐去搞数学公式啊!研究完贝塞尔曲线,我突然想好好学习数学。真的是数学不好,学什么编程啊。(哭晕在草稿纸中……) 正片干货在此: 科普时间 提到贝塞尔曲线,大...

  • 15

    作者:哈哈将 -个推 Android 高级开发工程师前言APP开发市场已经告别“野蛮生长”时代,人们不再满足于APP外形创新,而将目光转向全方面的用户体验上。在这过程中,动效化作为移动互联网产品的新趋势,如何实现酷炫丝滑的动画效果已然成为开发者们的新课题。实现

  • 10

    by zhangxinxu from https://www.zhangxinxu.com 本文地址:https://www.zhangxinxu.com/wordpress/?p=3...

  • 16
    • blog.csdn.net 4 years ago
    • Cache

    贝塞尔曲线开发的艺术

    贝塞尔曲线开发的艺术 一句话概括贝塞尔曲线:将任意一条曲线转化为精确的数学公式。 很多绘图工具中的钢笔工具,就是典型的贝塞尔曲线的应用,这里的一个网站可以在线模拟钢笔工具的使用:

  • 11

    by zhangxinxu from http://www.zhangxinxu.com 本文地址:http://www.zhangxinxu.com/wordpress/?p=4197

  • 11

    使用贝塞尔曲线实现道具随机飞动效果 2020-12-12 1 工作中,遇到了一个需求是要实现获得道具和货币的飞动效果: 根据道具的多少生成不同数目的道具 ...

  • 6
    • leovan.me 3 years ago
    • Cache

    贝塞尔曲线 (Bézier Curve)

    贝塞尔曲线 (Bézier Curve) 范叶亮 / 2019-02-19 分类: 数学,

  • 10

    最近研究了一下svg的path标签,功能非常强大,理论上来讲path标签可以画出任意图形。自己记性不太好,记录一下path的使用语法和自己的理解。path介绍path用d属性来描述路径,语法格式大概如下:<s...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK