UIView in shades of gray. View is not redrawn

I managed to get the grayscale UIView by adding the following top view:

@interface GreyscaleAllView : UIView

@property (nonatomic, retain) UIView *underlyingView;

@end

@implementation GreyscaleAllView

@synthesize underlyingView;

- (void)drawRect:(CGRect)rect {

    CGContextRef context = UIGraphicsGetCurrentContext();

    // draw the image
    [self.underlyingView.layer renderInContext:context];

    // set the blend mode and draw rectangle on top of image
    CGContextSetBlendMode(context, kCGBlendModeColor);
    CGContextSetRGBFillColor(context, 0.0, 0.0, 0.0, 1.0);
    CGContextFillRect(context, rect);
    [super drawRect:rect];
}

@end

It works, but the content is not updated unless I manually call setNeedsDisplay. (I can press UIButton, and the action fires, but nothing changes in appearance). Therefore, so that it behaves as expected, I call setNeedsDisplay 60 times per second. What am I doing wrong?

UPDATE:

The view controller in the overlayview view with this:

- (void)viewDidLoad
{
    [super viewDidLoad];

    GreyscaleAllView *grey = [[[GreyscaleAllView alloc] initWithFrame:self.view.frame] autorelease];
    grey.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    grey.userInteractionEnabled = NO;    
    [self.view addSubview:grey];
}

I added this for subscripts to redraw:

@implementation GreyscaleAllView

- (void)setup {

    self.userInteractionEnabled = FALSE;
    self.contentMode = UIViewContentModeRedraw;

    [self redrawAfter:1.0 / 60.0 repeat:YES];

}

- (void)redrawAfter:(NSTimeInterval)time repeat:(BOOL)repeat {

    if(repeat) {
        // NOTE: retains self - should use a proxy when finished
        [NSTimer scheduledTimerWithTimeInterval:time target:self selector:@selector(setNeedsDisplay) userInfo:nil repeats:YES];
    }

    [self setNeedsDisplay];
}
+5
source share
1 answer

, , - , subview , , -setNeedsDisplay -setNeedsDisplay GreyscaleAllView. UIView UIViewController view.

. loadView

self.view = [[CustomView alloc] initWithFrame:frame];

CustomView setNeedsDisplay, :

@implementation CustomView

- (void)setNeedsDisplay
{
    [grayView setNeedsDisplay]; // grayView is a pointer to your GreyscaleAllView
    [super setNeedsDisplay];
}

@end

-setNeedsDisplayInRect:

+1

All Articles