10

UIView に異なるストロークと塗りつぶし色で複数の UIBezierPath を描画したいと思います。

ここにコードがあります

- (void)drawRect:(CGRect)rect {

    context = UIGraphicsGetCurrentContext();

    [[UIColor grayColor] setFill]; 
    [[UIColor greenColor] setStroke];

    UIBezierPath *aPath = [[UIBezierPath alloc] init]; 
    [aPath moveToPoint:CGPointMake(227,34.25)];
    [aPath addLineToPoint:CGPointMake(298.25,34.75)];
    [aPath addLineToPoint:CGPointMake(298.5,82.5)];
    [aPath addLineToPoint:CGPointMake(251,83)];
    [aPath addLineToPoint:CGPointMake(251,67.5)];
    [aPath addLineToPoint:CGPointMake(227.25,66.75)];   
    [aPath closePath]; 
    aPath.lineWidth = 2;
    [aPath fill]; 
    [aPath stroke];

    UIBezierPath *aPath2 = [[UIBezierPath alloc] init];
    [aPath2 moveToPoint:CGPointMake(251.25,90.5)];
    [aPath2 addLineToPoint:CGPointMake(250.75,83.25)];
    [aPath2 addLineToPoint:CGPointMake(298.5,83)];
    [aPath2 addLineToPoint:CGPointMake(298.5,90.25)];
    [aPath2 closePath];
    aPath2.lineWidth = 2;
    [aPath2 fill]; 
    [aPath2 stroke];
    [paths addObject:aPath2];

問題は、ストロークと塗りつぶしの色が現在のコンテキストに設定されていることです。同じ CGContextRef で異なる色の異なる UIBezierPath を描画することは可能ですか?

または、各 UIBezierPath を個別の UIView に描画する必要がありますか?

4

3 に答える 3

24

追加する必要があります

[desiredStrokeColor setStroke];
[desiredFillColor setFill];

これらは、このコンテキストでさらに使用する必要がある新しい色であることを示しています。を呼び出すたびに、これを行う必要があります。

[aNewPath fill];
[aNewPath stroke];

パスがそれらの色で描画されるようにします。

ベジエ パスごとに新しいビューを使用する必要はありません。

于 2011-06-21T19:59:45.637 に答える
8

これを使って

int count = 0;
for(UIBezierpath *_paths in pathArray)
{
   UIColor *_color = [delegate1.colorArray objectAtIndex:q];
   [_color setStroke];
   [_path strokeWithBlendMode:kCGBlendModeNormal alpha:1.0]; 
   count++;
}

タッチで、パスと色を 2 つの配列に保存し、上記のように drawrect で使用するようになりました。

于 2012-08-01T11:09:38.013 に答える
5

UIColor *setStroke; を定義するだけです。.h ファイルで、呼び出す前にこの strokeColor オブジェクトを設定します[myPath strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];

 - (void)drawRect:(CGRect)rect
    {
        [strokeColor setStroke]; // this method will choose the color from the receiver color object (in this case this object is :strokeColor)
        for(UIBezierPath *_path in pathArray)
            [myPath strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
    }

    #pragma mark - Touch Methods
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        myPath=[[UIBezierPath alloc]init];
        myPath.lineWidth = currentSliderValue;

        UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
        [myPath moveToPoint:[mytouch locationInView:self]];
        [pathArray addObject:myPath];
    }
    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
        [myPath addLineToPoint:[mytouch locationInView:self]];
        [self setNeedsDisplay];

    }
于 2012-11-27T08:01:55.120 に答える