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 回答