【问题标题】:Filing color like effect in iphone app在 iphone 应用程序中归档颜色效果
【发布时间】:2012-09-05 11:33:19
【问题描述】:

我正在尝试制作一个“感觉应用程序”,其中包含一个图像,该图像会在用户触摸它的位置改变其颜色。我为此实现了UIPanGestureRecognizer 并找到了触摸,但无法更改图像特定部分的颜色。

【问题讨论】:

  • 您要填充的“图像”是什么?矢量还是位图?您如何定义更改“图像的特定部分”的颜色?
  • 是否有任何特定的原因需要平移手势?...这东西可以在触摸的东西中做...
  • 实际上我想在 UI 上上下拖动级别时用不同颜色填充温度计图像。这就是我选择平移手势的原因。
  • 这种效果就像通过触摸并向上移动来填充玻璃杯,然后随着手指在图像上向下移动而耗尽它。

标签: iphone ipad core-animation core-graphics


【解决方案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];
}

希望对你有帮助……

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-11
    • 2011-03-26
    • 2012-01-15
    • 1970-01-01
    • 1970-01-01
    • 2012-05-16
    相关资源
    最近更新 更多