1

I have a layer-hosting NSView. Within that I have a CALayer which includes a drawInContext method. The needsDisplayOnBoundsChange parameter is set to true, and and if I resize the view then the layer is indeed resized and redrawn during correctly during the animation.

However, I'd like to animate the size of the layer independently of the view. I can set up an animation to do this, however the layer isn't redrawn during the animation. Rather, is seems that a snap shot is taken for the start and end frames and one is faded out as the other is faded in. Worse, both images are distorted into the resizing-frame as the animation progresses.

How can I force the CALayer to redraw itself during the resize animation?

Thanks,

Tim

4

1 回答 1

0

这可能太旧了,但解决方案不应该太难。我不知道是否有更好的方法,但您可以使用 aCADisplayLink将您的图层设置为需要重绘。

- (void)updateContinuously
{
    if(!self.displayLink) {
        self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateLayer)];
        [self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
    }
}

- (void)stopUpdatingContinuously
{
    [self.displayLink invalidate];
    self.displayLink = nil;
}

- (void)update
{
    // This will fire every time when the display is redrawn
    [self setNeedsDisplay];
}

- (void)updateContinuouslyForTime:(NSTimeInterval)seconds
{
    // Create an NSTimer that will remove the displayLink when the time is over
    NSTimer *stopTimer = [NSTimer scheduledTimerWithTimeInterval:seconds target:self selector:@selector(stopUpdatingContinuously) userInfo:nil repeats:NO];
    [[NSRunLoop mainRunLoop] addTimer:stopTimer forMode:NSRunLoopCommonModes];
}
于 2013-07-25T14:07:49.507 回答