我正在尝试制作一个“感觉应用”,其中的图像会将其颜色更改为用户触摸它的位置。我为此实现了UIPanGestureRecognizer
并找到了触摸,但无法更改图像特定部分的颜色。
答案 0 :(得分: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];
}
我希望这能帮到你......