1

ユーザーがタッチした場所まで色が変わる画像を持つ「フィーリングアプリ」を作ろうとしています。そのために実装UIPanGestureRecognizerしてタッチを見つけましたが、画像の特定の部分の色を変更できませんでした。

4

1 に答える 1

1

このコードブロックを試してください....

色を変更したい場合は stokeColor を変更し、ブラシのサイズを変更したい場合はコメント行を参照してください

#pragma mark touches stuff...

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    lastTouch = [touch locationInView:imageView];

   }

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    currentTouch = [touch locationInView:imageView];

    CGColorRef strokeColor = [UIColor brownColor].CGColor;
    UIGraphicsBeginImageContext(imageView.frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [imageView.image drawInRect:CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height)];
    CGContextSetLineCap(context, kCGLineCapRound);
    CGContextSetLineWidth(context, 10);  // u can change the brush size here

       CGContextSetStrokeColorWithColor(context, strokeColor); // u need to change this to required color

            CGContextSetBlendMode(context,kCGBlendModeDarken ); // try the various blend modes

    CGContextBeginPath(context);
    CGContextMoveToPoint(context, lastTouch.x, lastTouch.y);
    CGContextAddLineToPoint(context, currentTouch.x, currentTouch.y);
    CGContextStrokePath(context);
    imageView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    lastTouch = [touch locationInView:imageView];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    lastTouch = [touch locationInView:imageView];
}

これがあなたに役立つことを願っています...

于 2012-09-05T12:05:19.537 に答える