2012-05-10 9 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 कहते हैं। (मैं एक यूआईबटन और एक्शन फायर दबा सकता हूं, लेकिन उपस्थिति में कुछ भी नहीं बदलता है) तो इसके लिए उम्मीद की तरह व्यवहार करने के लिए मैं सेट सेकंड्स को प्रत्येक सेकेंड में 60 बार प्रदर्शित करता हूं। मैं क्या गलत कर रहा हूं?

अद्यतन:

ViewController इस के साथ overlayview inits:

- (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

जोड़ा गया नियंत्रक इनिटकोड और रेड्रोकोड – jalmaas

+0

GreyscaleAllView * ग्रे = [[[GreyscaleAllView alloc] initWithFrame का उपयोग करने का प्रयास करें: CGRectMake (0,0,320,480)] autorelease]; – rishi

उत्तर

1

क्या आप वास्तव में चाहते हैं उपरोक्त के लिए केवल तभी निकालने के लिए कहा जा सकता है जब अभिभावक दृश्य को फिर से चलाता है, इस स्थिति में आप माता-पिता v में -setNeedsDisplay ओवरराइड कर सकते हैं GreyscaleAllView पर इसे -setNeedsDisplay पर कॉल करें और इसे कॉल करें। इसके लिए आपके UIViewController की view संपत्ति के लिए UIView subclassing की आवश्यकता है।

यानी नियंत्रक में 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:

संबंधित मुद्दे