我在故事板上绘制UILabel时遇到了麻烦;它总是空白的。标签被定义为属性self.rainbowCircle。以下是我的视图控制器中的设置:
self.rainbowCircle.layer.name = @"Rainbow Layer";
self.rainbowCircle.layer.delegate = self;
[self.rainbowCircle.layer setNeedsDisplay];
这里是drawLayer:inContext:
- (void) drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
CGContextSaveGState(ctx);
if ([layer.name isEqualToString:@"Rainbow Layer"]) {
NSLog(@"here");
NSArray *rainbowColorsArray = [NSArray arrayWithObjects:
(id)[UIColor colorWithHue:0.0 saturation:1.0 brightness:1.0 alpha:1.0].CGColor,
(id)[UIColor colorWithHue:1.0*360.0/6.0/360.0 saturation:1.0 brightness:1.0 alpha:1.0].CGColor,
(id)[UIColor colorWithHue:2.0*360.0/6.0/360.0 saturation:1.0 brightness:1.0 alpha:1.0].CGColor,
(id)[UIColor colorWithHue:3.0*360.0/6.0/360.0 saturation:1.0 brightness:1.0 alpha:1.0].CGColor,
(id)[UIColor colorWithHue:4.0*360.0/6.0/360.0 saturation:1.0 brightness:1.0 alpha:1.0].CGColor,
(id)[UIColor colorWithHue:5.0*360.0/6.0/360.0 saturation:1.0 brightness:1.0 alpha:1.0].CGColor,
(id)[UIColor colorWithHue:1.0 saturation:1.0 brightness:1.0 alpha:1.0].CGColor,
nil];
CGColorSpaceRef deviceRGB = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient = CGGradientCreateWithColors(deviceRGB, (__bridge CFArrayRef) (rainbowColorsArray), NULL);
CGContextDrawRadialGradient(ctx, gradient, self.rainbowCircle.center, 0.0, self.rainbowCircle.center, self.rainbowCircle.bounds.size.width/2.0, kCGGradientDrawsBeforeStartLocation);
CGColorSpaceRelease(deviceRGB);
CGGradientRelease(gradient);
} else {
[self drawPalette:self.currentPalette inLayer:layer inContext:ctx];
}
CGContextRestoreGState(ctx);
}
我已经盯着这个很长一段时间了,只是看不到我错过的东西。 NSLog确实显示了。任何指导意见。
答案 0 :(得分:3)
您无法更改UIView图层的委托,它必须是视图本身