1

アプリでUIBezierPathを使用して描画することができました。ここで、UIColor変数を追加して、ユーザーがボタンを押すだけで別の色を選択できるようにしました。ユーザーが色を変更して新しい色で描画を開始すると、すでに描画されているすべてのパスが新しい色になります。

すでに描画されているパスの色を変更しないようにオブジェクトに指示するにはどうすればよいですか?

これが私のコードです。コードの例は大歓迎です!:)

(UIColor変数をappDelegateに保存していて、別のviewControlleからrefreshメソッドを呼び出しています。

 - (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {

    AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];


    self.backgroundColor = [UIColor whiteColor];
    myPath = [[UIBezierPath alloc] init];
    myPath.lineCapStyle = kCGLineCapRound;
    myPath.miterLimit = 0;
    myPath.lineWidth = 5;

    brushPattern = app.currentColor;

}
   return self;
}
-(void)refresh {
    AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];

    brushPattern = app.currentColor;
}



- (void)drawRect:(CGRect)rect {
     [brushPattern setStroke];
     for (UIBezierPath *_path in pathArray)
     [_path strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
   myPath = [[UIBezierPath alloc] init];
   myPath.lineWidth = 5;

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

 -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

 }
4

1 に答える 1

-1

そのためのデモコードを1つ見つけました。

それがあなたに役立つならば、これをチェックしてください。

スムーズラインリンク

于 2013-04-09T05:12:41.540 に答える