如何在更改其边界的动画过程中强制重画CALayer



我有一个托管NSView的层。其中我有一个CALayer,它包括一个drawInContext方法。needsDisplayOnBoundsChange参数设置为true,如果我调整视图的大小,那么在动画过程中,层确实会调整大小并重新绘制。

但是,我希望独立于视图设置层大小的动画。我可以设置一个动画来执行此操作,但是在动画过程中不会重新绘制层。相反,似乎是为开始帧和结束帧拍摄快照,其中一个随着另一个的淡入而淡出。更糟糕的是,随着动画的进行,这两个图像都会失真到调整大小的帧中。

如何在调整动画大小的过程中强制CALayer重新绘制自己?

谢谢,

Tim

这可能太旧了,但解决方案不应该太难。我不知道是否有更好的方法,但你可以使用CADisplayLink来设置你的层需要重新绘制。

- (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];
}

最新更新