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

相關文章
相關標籤/搜索