Cocoa 在CALayer中,调用drawInContext:但不更新屏幕
我试图画一个自定义的CALayer,然后作为子层添加到其他地方。自定义CALayer类只有一个变量:Cocoa 在CALayer中,调用drawInContext:但不更新屏幕,cocoa,calayer,quartz-2d,Cocoa,Calayer,Quartz 2d,我试图画一个自定义的CALayer,然后作为子层添加到其他地方。自定义CALayer类只有一个变量: float xScale = 0.5f; 并重写drawInContext:方法: -(void) drawInContext:(CGContextRef)ctx { NSLog(@"Method called with xScale %f", xScale); CGContextSetFillColorWithColor(ctx, [NSColor
float xScale = 0.5f;
并重写drawInContext:
方法:
-(void) drawInContext:(CGContextRef)ctx {
NSLog(@"Method called with xScale %f", xScale);
CGContextSetFillColorWithColor(ctx, [NSColor redColor].CGColor);
CGContextAddRect(ctx, CGRectMake(100 * xScale, 100, 50, 50));
CGContextFillPath(ctx);
}
在屏幕上画一个红方块。在NSView的其他地方初始化该类时:
heartLayer = [[HeartCALayer alloc] init];
heartLayer.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
[self.layer addSublayer:heartLayer];
[heartLayer setNeedsDisplay];
正方形绘制在正确的位置。但是,当我要求图层重新绘制时
heartLayer.xScale = 1.0f;
[heartLayer setNeedsDisplay];
调用drawInContext:
方法,更新xScale
变量,但正方形不会更改其位置。我不知道为什么屏幕没有更新。这与作为子层添加的CALayer有关吗?图形上下文是否以某种方式无效?它与卡拉耶斯的隐性动画有关吗?我到处都找遍了,不知所措:(
谢谢,肯尼斯
很抱歉回答我自己的问题:也许你会比我浪费更少的时间: 问题是CALayer类的隐式动画。本文将对此进行讨论 我向自定义CALayer类添加了以下重写方法:
- (id<CAAction>)actionForLayer:(CALayer *)layer forKey:(NSString *)key {
return (id)[NSNull null];
}
-(id)actionForLayer:(CALayer*)层forKey:(NSString*)键{
返回(id)[NSNull];
}
我的方块更新正确