如何在进行自定义绘图时在UIView中启用隐式动画?

时间:2012-01-26 15:28:49

标签: animation uiview custom-draw

我使用以下命令在我的UIView子类中执行自定义绘图:

- (void)drawRect:(CGRect)rect;

如何在绘图更改时启用隐式动画?

1 个答案:

答案 0 :(得分: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];

}