2

好吧,我正在尝试在 iOS 上制作儿童追踪应用程序。我在实现绘制字母动画时遇到问题。该应用程序将绘制字母以演示应如何跟踪它。

如何实施?

我找到了一些代码: https ://github.com/ole/Animated-Paths 但是这里的字母被映射到两条路径(内体和外体)

我想在一条路径中实现它。我试图编辑代码但失败了

谁能指导我怎么做?

谢谢你

4

2 回答 2

0

使用 FloodFill 算法,这将帮助您进行跟踪

于 2013-07-03T08:35:31.290 回答
0

我也一直在挠头来实现同样的东西,我可以在上面画一个字母,这样就可以识别对字母的触摸。

这是我使用的一些逻辑,

  • 获取字母(字母)。1
  • 获取字母的字形。2
  • 为字形创建路径。3
  • 渲染从字形到贝塞尔路径的路径。4
  • 在视图中绘制贝塞尔曲线。5
  • 在贝塞尔曲线上获得积分。6
  • 有一次,在它们属于我们的贝塞尔点上触摸搜索它们。7

小路。

这是相同的代码示例。

//Get the glyph
const char charater = "A";    //1:
UIFont *font = [UIFont fontWithName:@"Helvetica" size:100.0];
CTFontRef cgFont = CTFontCreateWithName((__bridge CFStringRef)font.fontName, 100.0, nil);
CFStringRef str = CFStringCreateWithCString(NULL, &charater, kCFStringEncodingUTF8);
CGGlyph glyph = CTFontGetGlyphWithName(cgFont, str);  //2:

// render the glyph into a bezier path
CGPathRef path = CTFontCreatePathForGlyph(cgFont, glyph, nil); //3:
UIBezierPath *BPath = [UIBezierPath bezierPathWithCGPath:path];
BPath = BPath.bezierPathByReversingPath;
NSLog(@"Path is %@", BPath); //4:
CAShapeLayer *line = [[CAShapeLayer alloc] init];
line.path = BPath.CGPath;
line.strokeColor = [[UIColor blueColor] CGColor];
line.fillColor = [[UIColor colorWithPatternImage:img2] CGColor];
[myView.layer addSublayer:line];  //5:

//Get the points on path

CGPathRef yourCGPath = BPath.CGPath;
NSMutableArray *bezierPoints = [NSMutableArray array];
CGPathApply(yourCGPath, (__bridge void * _Nullable)(bezierPoints), MyCGPathApplierFunc);
NSLog(@"Points on path %@", bezierPoints); //6:


//Check if point present on path
CGPoint StartPoint1 = [Touch locationInView:myView];
if(CGRectContainsPoint(myView.frame, StartPoint1)){
//Do your work.   //7:
}


//GetPoints

void MyCGPathApplierFunc (void *info, const CGPathElement *element) {
NSMutableArray *bezierPoints = (__bridge 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;
    }
}
于 2016-10-07T08:49:05.990 回答