简体   繁体   English

当我的手指移动时,如何填充图像的透明区域

[英]how can i fill the transparent area of the image when my finger moved on

I wanna draw the transparent area with brush, but my code work not very well.I think someone can help me here. 我想用画笔绘制透明区域,但我的代码工作得不是很好。我想有人可以帮助我。 My code : 我的代码:

// Handles the start of a touch

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGRect bounds = [self bounds];
    UITouch *touch = [[event touchesForView:self] anyObject];

    if (![image isPointTransparent:[touch locationInView:self]]
       || ![image isPointTransparent:[touch previousLocationInView:self]]) 
    {
        return;
    }

firstTouch = YES;

    // Convert touch point from UIView referential to OpenGL one (upside-down flip)

location = [touch locationInView:self];
location.y = bounds.size.height - location.y;
}

// Handles the continuation of a touch.

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{  
    CGRect bounds = [self bounds];
    UITouch *touch = [[event touchesForView:self] anyObject];

    if (![image isPointTransparent:[touch locationInView:self]] 
        || ![image isPointTransparent:[touch previousLocationInView:self]]) 
    {
        return;
    }

// Convert touch point from UIView referential to OpenGL one (upside-down flip)
if (firstTouch) 
    {
    firstTouch = NO;
        previousLocation = [touch previousLocationInView:self];
    previousLocation.y = bounds.size.height - previousLocation.y;
} 
    else 
    {
    location = [touch locationInView:self];
    location.y = bounds.size.height - location.y;
    previousLocation = [touch previousLocationInView:self];
    previousLocation.y = bounds.size.height - previousLocation.y;
}

// Render the stroke
[self renderLineFromPoint:previousLocation toPoint:location];
}

// Handles the end of a touch event when the touch is a tap.

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGRect bounds = [self bounds];
    UITouch *touch = [[event touchesForView:self] anyObject];

    if (![image isPointTransparent:[touch locationInView:self]] || ![image isPointTransparent:[touch previousLocationInView:self]]) 
    {  
        return;
    }

    if (firstTouch) 
    {
       firstTouch = NO;
       previousLocation = [touch previousLocationInView:self];
       previousLocation.y = bounds.size.height - previousLocation.y;
       [self renderLineFromPoint:previousLocation toPoint:location];
}
}

The important thing to know is that you should do your actual drawing in the drawRect: of your UIView . 重要的是要知道你应该在你的UIViewdrawRect:做你的实际绘图。 So the renderLineFromPoint:toPoint: method in your code should just be building up an array of lines and telling the view to redraw each time, something like this: 因此,代码中的renderLineFromPoint:toPoint:方法应该只是构建一个行数组并告诉视图每次重绘,如下所示:

- (void)renderLineFromPoint:(CGPoint)from toPoint:(CGPoint)to
{
  [lines addObject:[Line lineFrom:from to:to]];
  [self setNeedsDisplay];
}

This assumes that you have a Line class which has 2 CGPoint properties. 这假设您有一个具有2个CGPoint属性的Line类。 Your drawRect: may look something like this: 你的drawRect:可能看起来像这样:

- (void)drawRect:(CGRect)rect
{
  CGContextRef context = UIGraphicsGetCurrentContext();
  CGContextSetRGBStrokeColor(context, 0.0f, 0.0f, 0.0f, 1.0f);
  for (Line *line in lines) {
    CGContextMoveToPoint(context, line.from.x, line.from.y);
    CGContextAddLineToPoint(context, line.to.x, line.to.y);
    CGContextStrokePath(context);
  }
}

If you do it like this (without OpenGL) there is no need to flip the y-axis. 如果你这样做(没有OpenGL),就不需要翻转y轴。

The only thing left is to implement the isPointTransparent: method. 唯一剩下的就是实现isPointTransparent:方法。 It's difficult to know from your code how this should work. 从您的代码中了解这应该如何工作很难。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检测用户何时将手指放在UIWebView中的图像上? - How can I detect when a user holds their finger over an image in a UIWebView? 如何在按下按钮时播放音频,而当我将手指从同一按钮上移开时如何立即停止播放? - How can I make audio play when the button is pressed and immediately stop when I take my finger off the same button? 如何填写特定区域的图像颜色? - how to fill color in image in particular area? CGImageCreateWithMask工作得很好,但在我生成的图像中,蒙面区域是黑色的,如何将其设置为白色? - CGImageCreateWithMask works great but the masked out area is black in my resulting image, how can I set it to be white? 如何通过手指获取UIScrollView分页? - How can I get UIScrollView paging by finger? 如何在iOS中对黑色透明图像进行颜色填充? - How to color-fill a black and transparent image in iOS? 应在特定区域使用鼠标移动图像 - image should be moved using mouse in a particular area 当手指在iPhone上移动时,如何在图像上绘制一条线 - How to draw a line on top of an image when finger moves on iPhone 如何使我的 canvas 区域适合 iphone 浏览器? - How can i make my canvas area fit to the iphone browser? 移动后,UINavigationBar下的区域不再可触摸 - When moved, the area under a UINavigationBar is no longer touchable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM