【问题标题】:How to force redraw of a CALayer during an animation that changes its bounds如何在更改其边界的动画期间强制重绘 CALayer
【发布时间】:2012-12-17 15:07:43
【问题描述】:
我有一个托管图层的 NSView。其中我有一个包含 drawInContext 方法的 CALayer。 needsDisplayOnBoundsChange 参数设置为 true,如果我调整视图大小,那么在动画期间,图层确实会调整大小并重新绘制。
但是,我想独立于视图设置图层大小的动画。我可以设置一个动画来执行此操作,但是在动画期间不会重绘图层。相反,似乎为开始帧和结束帧拍摄了快照,并且随着另一帧的淡入而淡出。更糟糕的是,随着动画的进行,两张图像都被扭曲到调整大小的帧中。
如何在调整大小动画期间强制 CALayer 重绘自身?
谢谢,
提姆
【问题讨论】:
标签:
macos
cocoa
core-animation
【解决方案1】:
这可能太旧了,但解决方案应该不会太难。我不知道是否有更好的方法,但您可以使用 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];
}