我设法通过在顶部添加以下视图来获得灰度级的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
它可以工作,但除非我手动调用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];
}
我为底层视图添加了这个以重绘:
@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];
}
答案 0 :(得分:1)
您真正想要的是,只有在父视图重绘时才会告知子视图重绘,在这种情况下,您可以在父视图中覆盖-setNeedsDisplay
并在-setNeedsDisplay
上调用GreyscaleAllView
{1}}。这需要为您的UIViewController
的{{1}}属性继承UIView。
即。在控制器中实现view
,然后设置
loadView
如上所述,CustomView会覆盖self.view = [[CustomView alloc] initWithFrame:frame];
:
setNeedsDisplay
为完整起见,您还应对@implementation CustomView
- (void)setNeedsDisplay
{
[grayView setNeedsDisplay]; // grayView is a pointer to your GreyscaleAllView
[super setNeedsDisplay];
}
@end