Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to make a subview tint a portion of the screen on the iPhone?
    text
    copied!<p>I would like to write a UIView subclass that, among other things, colors whatever's underneath it a certain color. This is what I've come up with, but it unfortunately doesn't seem to work correctly:</p> <pre><code>#import &lt;UIKit/UIKit.h&gt; @class MyOtherView; @interface MyView : UIView { MyOtherView *subview; } @end @implementation MyView - (id)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { frame.origin.x += 100.0; frame.origin.y += 100.0; frame.size.width = frame.size.height = 200.0; subview = [[MyOtherView alloc] initWithFrame:frame]; [self addSubview:subview]; [subview release]; } return self; } - (void)drawRect:(CGRect)rect { // Draw a background to test out on UIImage *image = [UIImage imageNamed:@"somepic.png"]; [image drawAtPoint:rect.origin]; const CGContextRef ctx = UIGraphicsGetCurrentContext(); [[UIColor blueColor] setFill]; rect.size.width = rect.size.height = 200.0; CGContextFillRect(ctx, rect); } @end @interface MyOtherView : UIView @end @implementation MyOtherView - (void)drawRect:(CGRect)rect { // This should tint "MyView" but it doesn't. const CGContextRef ctx = UIGraphicsGetCurrentContext(); CGContextSaveGState(ctx); CGContextSetBlendMode(ctx, kCGBlendModeScreen); [[UIColor redColor] setFill]; CGContextFillRect(ctx, rect); CGContextRestoreGState(ctx); } @end </code></pre> <p>I want "MyOtherView" to color "MyView" red where it overlaps, but instead it just draws an opaque red block onto it. However, this seems work fine if I copy the -drawRect: function from "MyOtherView" and append it to the one in "MyView" (this took me quite a headache to finally realize). Anyone know what I'm doing wrong? Is it even possible to do this, or should I be approaching it differently?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload