如何从 UIBezierPath 获取点列表? [英] How to get a list of points from a UIBezierPath?

查看:32
本文介绍了如何从 UIBezierPath 获取点列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UIBezierPath 我需要使用来自的点列表.

I have a UIBezierPath that I need to take a list of points from.

Qt 中有一个名为pointAtPercent 的函数,它可以满足我的需要,但我在Objective-C 中找不到任何等效的东西.

In Qt there is a function called pointAtPercent that would fit my needs but I can't find anything equivalent in Objective-C.

有人知道怎么做吗?

推荐答案

你可以试试这个:

UIBezierPath *yourPath; // Assume this has some points in it
CGPath yourCGPath = yourPath.CGPath;
NSMutableArray *bezierPoints = [NSMutableArray array];
CGPathApply(yourCGPath, bezierPoints, MyCGPathApplierFunc);

path applier 函数会依次传递path的每个path元素.
查看 CGPathApplierFunctionCGPathApply.

The path applier function will be handed each of the path's path elements in turn.
Check out CGPathApplierFunction and CGPathApply.

路径应用器函数可能看起来像这样

The path applier function might look something like this

void MyCGPathApplierFunc (void *info, const CGPathElement *element) {
    NSMutableArray *bezierPoints = (NSMutableArray *)info;

    CGPoint *points = element->points;
    CGPathElementType type = element->type;

    switch(type) {
        case kCGPathElementMoveToPoint: // contains 1 point
            [bezierPoints addObject:[NSValue valueWithCGPoint:points[0]]];
            break;
        
        case kCGPathElementAddLineToPoint: // contains 1 point
            [bezierPoints addObject:[NSValue valueWithCGPoint:points[0]]];            
            break;
        
        case kCGPathElementAddQuadCurveToPoint: // contains 2 points
            [bezierPoints addObject:[NSValue valueWithCGPoint:points[0]]];
            [bezierPoints addObject:[NSValue valueWithCGPoint:points[1]]];            
            break;
        
        case kCGPathElementAddCurveToPoint: // contains 3 points
            [bezierPoints addObject:[NSValue valueWithCGPoint:points[0]]];
            [bezierPoints addObject:[NSValue valueWithCGPoint:points[1]]];
            [bezierPoints addObject:[NSValue valueWithCGPoint:points[2]]];
            break;
        
        case kCGPathElementCloseSubpath: // contains no point
            break;
    }
}

这篇关于如何从 UIBezierPath 获取点列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆