2011-03-15 20 views
4

私は、ユーザーがランダムCGPathを追加できるUIViewサブクラスを持っています。 CGPathは、UIPanGesturesを処理することによって追加されます。CGViewに合わせてUIViewのサイズを変更する

私はCGViewを含む最小限のrectにUIViewのサイズを変更したいと思います。私のUIViewサブクラスでは、私は、次のような最小サイズを返すようにsizeThatFitsをオーバーライドしています

- (CGSize) sizeThatFits:(CGSize)size { 
    CGRect box = CGPathGetBoundingBox(sigPath); 
    return box.size; 
} 

予想とのUIViewが値にリサイズされるように、これが機能する返されたが、CGPathにも比例して得られた「リサイズ」されますユーザが元々描いていたものとは異なる経路。

Path as drawn

そして、これはリサイズ後のパスを持つ図である:例として、ユーザによって描かれたように、これはパスを持つことが

enter image description here

私はどのようにサイズを変更することができます私のUIViewと "サイズ変更"パス?

+0

ここに問題があります。あなたは解決策を見つけましたか?ありがとう! – valvoline

答えて

6

CGPathGetBoundingBoxを使用します。 Appleのドキュメントから:

グラフィックパス内のすべてのポイントを含む境界ボックスを返します。 バウンディングボックスは、ベジェ曲線および2次曲線の制御点を含む、パス内のすべての点を完全に囲む最小の矩形です( )。

ここでは、小さな概念実証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); 
} 
+0

ライン幅を考慮しない – jjxtra

関連する問題