Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 调整UIView的大小以适合CGPath_Ios_Uiview_Cgpath - Fatal编程技术网

Ios 调整UIView的大小以适合CGPath

Ios 调整UIView的大小以适合CGPath,ios,uiview,cgpath,Ios,Uiview,Cgpath,我有一个UIView子类,用户可以在其上添加随机CGPath。CGPath是通过处理UIPANGesture添加的 我想将UIView调整为包含CGPath的最小矩形。在我的UIView子类中,我已重写sizeThatFits以返回最小大小: - (CGSize) sizeThatFits:(CGSize)size { CGRect box = CGPathGetBoundingBox(sigPath); return box.size; } 这将按预期工作,UIView的大小

我有一个UIView子类,用户可以在其上添加随机CGPath。CGPath是通过处理UIPANGesture添加的

我想将UIView调整为包含CGPath的最小矩形。在我的UIView子类中,我已重写sizeThatFits以返回最小大小:

- (CGSize) sizeThatFits:(CGSize)size {
    CGRect box = CGPathGetBoundingBox(sigPath);
    return box.size;
}
这将按预期工作,UIView的大小将调整为返回的值,但CGPath也会按比例“调整大小”,从而产生与用户最初绘制的路径不同的路径。例如,这是具有用户绘制的路径的视图:

这是调整大小后具有路径的视图:


如何调整UIView的大小而不“调整”路径?

使用CGPathGetBoundingBox。来自Apple文档:

返回包含图形路径中所有点的边界框。这个 边界框是完全包围所有点的最小矩形 在路径中,包括Bézier曲线和二次曲线的控制点

这里是一个小的概念证明drawRect方法。希望它能帮助你

- (void)drawRect:(CGRect)rect {

    //Get the CGContext from this view
    CGContextRef context = UIGraphicsGetCurrentContext();

    //Clear context rect
    CGContextClearRect(context, rect);

    //Set the stroke (pen) color
    CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);

    //Set the width of the pen mark
    CGContextSetLineWidth(context, 1.0);

    CGPoint startPoint = CGPointMake(50, 50);
    CGPoint arrowPoint = CGPointMake(60, 110);

    //Start at this point
    CGContextMoveToPoint(context, startPoint.x, startPoint.y);
    CGContextAddLineToPoint(context, startPoint.x+100, startPoint.y);
    CGContextAddLineToPoint(context, startPoint.x+100, startPoint.y+90);
    CGContextAddLineToPoint(context, startPoint.x+50, startPoint.y+90);
    CGContextAddLineToPoint(context, arrowPoint.x, arrowPoint.y);
    CGContextAddLineToPoint(context, startPoint.x+40, startPoint.y+90);
    CGContextAddLineToPoint(context, startPoint.x, startPoint.y+90);
    CGContextAddLineToPoint(context, startPoint.x, startPoint.y);

    //Draw it
    //CGContextStrokePath(context);

    CGPathRef aPathRef = CGContextCopyPath(context);

    // Close the path
    CGContextClosePath(context);

    CGRect boundingBox = CGPathGetBoundingBox(aPathRef);
    NSLog(@"your minimal enclosing rect: %.2f %.2f %.2f %.2f", boundingBox.origin.x, boundingBox.origin.y, boundingBox.size.width, boundingBox.size.height);
} 

这里有些问题。你找到解决办法了吗?谢谢不考虑线宽