Ios UIView以灰度显示。视图未被重绘

Ios UIView以灰度显示。视图未被重绘,ios,uiview,grayscale,Ios,Uiview,Grayscale,通过在顶部添加以下视图,我成功获得了灰度UIView: @interface GreyscaleAllView : UIView @property (nonatomic, retain) UIView *underlyingView; @end @implementation GreyscaleAllView @synthesize underlyingView; - (void)drawRect:(CGRect)rect { CGContextRef context =

通过在顶部添加以下视图,我成功获得了灰度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使用以下命令初始化覆盖视图:

- (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];
}

您真正想要的是子视图仅在父视图重画时才被告知重画,在这种情况下,您可以覆盖父视图中的
-setNeedsDisplay
,并使其在
GreyscaleAllView
上调用
-setNeedsDisplay
。这需要为
UIViewController
view
属性子类化UIView

i、 e.在控制器中执行
loadView
,并设置

self.view = [[CustomView alloc] initWithFrame:frame];
其中,CustomView覆盖了如上所述的设置需要显示:

@implementation CustomView

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

@end

为完整起见,您还应该对
-setNeedsDisplayInRect:

执行相同的操作:

您可以从初始化此灰度视图的位置添加代码吗?添加了viewcontroller initcode并使用GreyscaleAllView*gray=[[GreyscaleAllView alloc]initWithFrame:CGRectMake(0,0320480)]autorelease]重新绘制代码;这应该也没有任何区别。(这是一个iPad项目,所以设置框架只会导致部分视图以灰度显示)当我们使用自定义视图时,我们需要提供框架,然后你需要尝试使用iPad维度,只需尝试一次,然后让我知道它是否有效?