码迷,mamicode.com
首页 > 其他好文 > 详细

将UIBezierPath存为自己定义格式的字符串,再将字符串转为UIBezierPath

时间:2017-07-19 21:51:48      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:return   file   string   UI   move   解析   value   close   bpa   

<pre name="code" class="objc">自己定义字符串格式为:@"123.02,234.23|321.23,432.0002|543.0003,432.0045|654.0034,567.0034|23.3443,56.0034|77.3440,55.3443|"


/**
 *@brief:将BezierPath中的点转为字符串
 */
+ (NSString*)convertBezierPathToNSString:(UIBezierPath*) bezierPath;
{

    NSString *pathString = @"";

    CGPathRef yourCGPath = bezierPath.CGPath;
    NSMutableArray *bezierPoints = [NSMutableArray array];
    CGPathApply(yourCGPath, (__bridge void *)(bezierPoints), MyCGPathApplierFunc);
    
    for (int i = 0; i < [bezierPoints count]; ++i)
    {
     
        CGPoint point = [bezierPoints[i] CGPointValue];
        pathString = [pathString stringByAppendingString:[NSString stringWithFormat:@"%f",point.x]];
        pathString = [pathString stringByAppendingFormat:@"%@",@","];
        pathString = [pathString stringByAppendingString:[NSString stringWithFormat:@"%f",point.y]];
        pathString = [pathString stringByAppendingString:@"|"];

    }

    return pathString;
}


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;
    }
}



<pre name="code" class="objc">
/**
 *@brief:将字符串中的点转为BezierPath
 */
+ (UIBezierPath*)convertNSStringToBezierPath:(NSString*) bezierPathString
{
    
    NSMutableArray *pointsArray = [[NSMutableArray alloc] init];
    NSInteger length = 0;
    
    //解析字符串
    NSString *separatorString1 = @",";
    NSString *separatorString2 = @"|";
    
    NSScanner *aScanner = [NSScanner scannerWithString:bezierPathString];
    
    while (![aScanner isAtEnd]) {
        
        NSString *xString, *yString;
        
        [aScanner scanUpToString:separatorString1 intoString:&xString];
        [aScanner setScanLocation:[aScanner scanLocation]+1];
        
        [aScanner scanUpToString:separatorString2 intoString:&yString];
        [aScanner setScanLocation:[aScanner scanLocation]+1];
        

        CGPoint point;
        point.x = [xString floatValue];
        point.y = [yString floatValue];
        
        [pointsArray addObject:[NSValue valueWithCGPoint:point]];
        
        
    }
    
    
    //首先将字符串解析为CGPoint数组,再将数组初始化UIBeizerPath
    length = [pointsArray count];
    UIBezierPath *bezierPath = [[UIBezierPath alloc] init];
    [bezierPath moveToPoint:[pointsArray[0] CGPointValue] ];
    for (NSUInteger j=1; j< length; j++) {
        [bezierPath addLineToPoint:[pointsArray[j] CGPointValue] ];
    }
    
    return bezierPath;
}




将UIBezierPath存为自己定义格式的字符串,再将字符串转为UIBezierPath

标签:return   file   string   UI   move   解析   value   close   bpa   

原文地址:http://www.cnblogs.com/slgkaifa/p/7207728.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!