2012-05-10 2 views
5

мне удалось получить UIView в оттенках серого, добавив следующий вид сверху: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 раз в секунду. Что я делаю не так?

UPDATE:

ViewController inits в 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

может вы добавляете код, из которого вы инициализируете этот вид серого масштаба? – rishi

+0

добавлен viewcoderler initcode и redrawcode – jalmaas

+0

попробуйте использовать GreyscaleAllView * gray = [[[GreyscaleAllView alloc] initWithFrame: CGRectMake (0,0,320,480)] autorelease]; – rishi

ответ

1

Что вы действительно хотите для того, чтобы subview отображалось только, чтобы перерисовать, когда родительский вид перерисовывается, и в этом случае вы можете переопределить -setNeedsDisplay в родительском v iew и позвоните по телефону -setNeedsDisplay по телефону GreyscaleAllView. Для этого требуется подкласс UIView для вашего UIViewControllerview.

т.е. реализовать 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:

 Смежные вопросы

  • Нет связанных вопросов^_^