6

これが私がやりたいことです:

私は UIBezierPath を持っており、それを何らかのメソッドに渡して描画したいと考えています。または、それが作成されたメソッドから単純に描画します。

どのビューで描画する必要があるかを示す方法がわかりません。描画するためのすべてのメソッドは、で開始する必要がありますか

- (void)drawRect:(CGRect)rect { ...} ?

僕にできる

- (void)drawRect:(CGRect)rect withBezierPath:(UIBezierPath*) bezierPath { ... } ??

この関数またはメソッドを別のメソッドから呼び出すにはどうすればよいですか?

4

5 に答える 5

20

drawRect:setNeedsDisplayメッセージまたはsetNeedsDisplayInRect:ビューで自動的に呼び出されるものです。drawRect:直接電話することはありません。

drawRect:ただし、すべての描画操作がメソッド内で行われると言うのは正しいです。典型的な実装は、

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();

    /* Do your drawing on `context` */
}

sを使用しUIBezierPathているため、ベジエ パスの配列を維持する必要があります。これを描画してからsetNeedsDisplay、何かが変更されたときに呼び出す必要があります。

- (void)drawRect:(CGRect)rect {    
    for ( UIBezierPath * path in bezierPaths ) {
        /* set stroke color and fill color for the path */
        [path fill];
        [path stroke];
    }
}

はs のbezierPaths配列ですUIBezierPath

于 2011-06-22T05:01:09.760 に答える
6

まず、パスをivarに保存します

@interface SomeView {
  UIBezierPath * bezierPath;
}
@property(nonatomic,retain) UIBezierPath * bezierPath;
...
@end
....
- (void)someMethod {
     self.bezierPath = yourBezierPath;
     [self setNeedsDisplayInRect:rectToRedraw];
}

-drawRect:

- (void)drawRect:(CGRect)rect {
    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(currentContext, 3.0);
    CGContextSetLineCap(currentContext, kCGLineCapRound);
    CGContextSetLineJoin(currentContext, kCGLineJoinRound);
    CGContextBeginPath(currentContext);
    CGContextAddPath(currentContext, bezierPath.CGPath);
    CGContextDrawPath(currentContext, kCGPathStroke);
}
于 2011-06-22T04:59:37.050 に答える
3

ビューをカスタマイズする必要がある場合は-drawRect:、サブクラスを上書きできます。

- (void)drawRect:(CGRect)rect
{
  // config your context
  [bezierPath stroke];
}

編集:-strokeコードをよりコンパクトにする直接使用。

于 2011-06-22T04:53:39.757 に答える
1

次のようなことができます。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:04:08.510 に答える
1

描画は呼び出されたメソッド内でのみ行われ-drawRect:ます (ビューが によって表示が必要であるとマークされたときに自動的に呼び出されますsetNeedsDisplay)。したがって、drawRect:withBezierPath:メソッドが自動的に呼び出されることはありません。それが実行される唯一の方法は、自分で呼び出す場合です。

ただし、があればUIBezierPath、それを描くのは非常に簡単です。

- (void)drawRect:(CGRect)rect {
  UIBezierPath *path = ...; // get your bezier path, perhaps from an ivar?
  [path stroke];
}

パスを描画するだけであれば、Core Graphics をいじる必要はありません。

于 2011-06-22T05:00:32.573 に答える