简体   繁体   中英

iPhone clear UIView alpha at touch location

I want to present an image in a UIView , because I have to customize (void)drawRect:(CGRect)rect . I have to clear the view's alpha where the user "rubs" the screen, so the subview behind this view will become visible.
If I use a solid color it works fine, but when I try to draw an image and clear the view just where the user touched the screen, the image is not drawn. Can you help me, how should I draw the UIImage , to be drawn in the background and to become transparent in a particular rect , please?

This is my code which works with solid color (as it is now):

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSaveGState(context);

    CGContextSetFillColorWithColor( context, [UIColor greenColor].CGColor );
    CGContextFillRect( context, rect );

    CGContextSetFillColorWithColor( context, [UIColor clearColor].CGColor );
    CGContextSetBlendMode(context, kCGBlendModeClear);
    CGContextFillRect(context, CGRectMake(lastTouch.x, lastTouch.y, 30, 30));

    CGContextRestoreGState(context);
}

There's a good control on github for that purpose already. check out:

https://github.com/akopanev/iOS-Scratch-n-See

I used it as a starting point for a "scratch game" app already. The delegate receives also the information how much of the screen/image is already cleared (ie "scratched away").

Good luck!

I'm not sure to understand your problem... but if the problem is that the drawRect is not called when the user touch the screen... try this:

put a

[self setNeedsDisplay];

in your touch event... a UIGestureRecognizer or overriding the UITouches methods

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM