2012-01-26 55 views

回答

0

由於沒有其他建議,我繼續使用圖層委託方法。無論如何,這也許是唯一的選擇。

所以在MyView的我創建了一個新的CALayer屬性:

-(CALayer*)stylesLayer 
{ 

    if (_stylesLayer == nil) { 

     CALayer *aLayer = [CALayer layer]; 
     aLayer.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height); 

     if ([self respondsToSelector:@selector(setContentScaleFactor:)]) 
     { 
      aLayer.contentsScale = self.contentScaleFactor; 
     } 


     _stylesLayer = aLayer; 
     [_stylesLayer retain]; 
     [self.layer addSublayer:aLayer]; 
     [aLayer release]; 
    } 

    return _stylesLayer; 
} 

而在MyViewController我實現,做繪圖的委託方法:

-(void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context 
{ 

    // custom drawing code here 

} 

現在,當某個屬性的變化我更新MyViewController的圖層,它隱式地進行淡入淡出動畫。

-(void)setStyles:(NSArray *)styles 
{ 

    [styles retain]; 
    [_styles release]; 
    _styles = nil; 
    _styles = styles; 

    [self.myView.stylesLayer setNeedsDisplay]; 

}