我设法通过在顶部添加以下视图来获得灰度级的UIView:
@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
Run Code Online (Sandbox Code Playgroud)
它工作正常,但除非我手动调用setNeedsDisplay,否则内容不会更新.(我可以按下UIButton并触发动作,但外观没有任何变化)因此,为了表现得像预期的那样,我每秒调用setNeedsDisplay 60次.我究竟做错了什么?
更新:
viewcontroller在overlayview中使用:
- (void)viewDidLoad
{
[super viewDidLoad];
GreyscaleAllView *grey = [[[GreyscaleAllView alloc] initWithFrame:self.view.frame] autorelease];
grey.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
grey.userInteractionEnabled = NO;
[self.view addSubview:grey];
} …Run Code Online (Sandbox Code Playgroud)