Color property in a subclass of CALayer

It drives me crazy. I am trying to change the custom property "mycolor" of a subclass of CALayer, this is my code:

#import "Circle" @interface Circle() @property (nonatomic, strong) UIColor * mycolor; @property (nonatomic) float initial_angle; @property (nonatomic) float end_angle; @end @implementation Circle @synthesize mycolor; @synthesize initial_angle, end_angle; - (id) init { self = [super init]; if (self) { CGFloat red = (CGFloat)random()/(CGFloat)RAND_MAX; CGFloat blue = (CGFloat)random()/(CGFloat)RAND_MAX; CGFloat green = (CGFloat)random()/(CGFloat)RAND_MAX; self.mycolor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0]; self.backgroundColor = [UIColor clearColor].CGColor; } return self; } -(void) changeColor { CGFloat red = (CGFloat)random()/(CGFloat)RAND_MAX; CGFloat blue = (CGFloat)random()/(CGFloat)RAND_MAX; CGFloat green = (CGFloat)random()/(CGFloat)RAND_MAX; self.mycolor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0]; } - (void) animate { float final = arc4random() % 360; float afrom = self.initial_angle; self.initial_angle = final; CABasicAnimation * anim = [CABasicAnimation animationWithKeyPath:@"initial_angle"]; anim.fromValue = [NSNumber numberWithFloat:afrom]; anim.toValue = [NSNumber numberWithFloat:final]; anim.duration = 5; anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; [self addAnimation:anim forKey:@"initial_angle"]; final = arc4random() % 360; afrom = self.end_angle; self.end_angle = final; anim = [CABasicAnimation animationWithKeyPath:@"end_angle"]; anim.fromValue = [NSNumber numberWithFloat:afrom]; anim.toValue = [NSNumber numberWithFloat:final]; anim.duration = 5; anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; [self addAnimation:anim forKey:@"end_angle"]; } - (void) drawInContext:(CGContextRef)ctx { CGContextMoveToPoint(ctx, 50, 50); CGContextAddArc(ctx, 50, 50, 50, initial_angle*M_PI/180, end_angle*M_PI/180, 0); CGContextClosePath(ctx); NSLog(@"%@", self.mycolor); CGContextSetFillColorWithColor(ctx, self.mycolor.CGColor); CGContextFillPath(ctx); } + (BOOL)needsDisplayForKey:(NSString*)key { if ([key isEqualToString:@"initial_angle"]|| [key isEqualToString:@"end_angle"]|| [key isEqualToString:@"mycolor"]) { return YES; } else { return [super needsDisplayForKey:key]; } } @end 

changeColor and animate are public methods. The animation is called every second and changes color every time the user clicks on the circle.

Everything works fine, except that in "drawInContext" mycolor is always null. I do not know why.

0
source share
3 answers

Have you tried to register mycolor immediately after setting it?

Your code looks healthy, so there is definitely something strange there. Is it possible that you created a custom setter or getter method for mycolor somewhere and did not implement it correctly?

I assume you are using ARC - did you try to install mycolor ivar directly and not through setter? (in ARC, this should be good - it will not flow or is unexpectedly released)

+1
source
 @synthesize mycolor = _mycolor; _mycolor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0]; 

try using _mycolor in the init method and drawInContext.

  CGContextSetFillColorWithColor(ctx, self.mycolor.CGColor); 

I think in drawInContext you can just use an instance variable instead of a property.

0
source

The correct initializer for CALayer is +layer , you should override the +layer method instead of -init , which may not even be called at all.

0
source

All Articles