40行代码在iPhone上动画显示路线图

lines

<!-- lang: cpp -->
void addAnimatedBezierDemo(GiPlayingHelper *play)
{
    __block Point2d pt1, pt2, pt3, pt4;
    __block float t = 0;

    [play addPlayProvider:^int(GiFrame frame) {
        MgShapes *shapes = MgShapes::fromHandle(frame.shapes);
    
        if (shapes->getShapeCount() == 0) {
            GiViewHelper *helper = [GiViewHelper sharedInstance:frame.view];
            MgShapeT<MgPathShape> path;
        
            path._context.setLineWidth(-5, true);
            pt1 = d2m(10, 50, helper);
            pt2 = d2m(100, 400, helper);
            pt3 = d2m(300, 400, helper);
            pt4 = d2m(300, 50, helper);
        
            path._shape.path().moveTo(pt1);
            path._shape.path().bezierTo(pt2, pt3, pt4);
            shapes->addShape(path);
        }
    
        if (frame.tick < frame.lastTick + 10) {
            return 0;
        }
    
        MgShape* newsp = shapes->getLastShape()->cloneShape();
        GiPath& path = ((MgPathShape*)newsp->shape())->path();
    
        Point2d pts[12] = { pt1, pt2, pt3, pt4 };
        mgcurv::splitBezier(pts, t, pts + 4, pts + 8);
        path.clear();
        path.moveTo(pts[4]);
        path.beziersTo(3, pts + 5);
    
        t += 1e-2;
        if (t > 1)
            t = 0;
        shapes->updateShape(newsp, true);
        return 1;
    } ended:nil tag:1];
}

代码见 vgplay-ios 项目的 AnimatedLines.mmAnimatedBezier.mmios

只是一个动画原型,还须要更多完善,使用还须要精简。目标使用还比较复杂,理想状况是写相似于脚本串指定速度、时间、moveto/lineto/curveto等简单绘图指令,隐藏坐标转换、图形更新等复杂细节。git

相关文章
相关标签/搜索