Search code examples
ioscalayer

Custom CALayer wont respond to selector


I need a custom layer (extended by new variable and a method) and did it like below. The problem is that the layer won't respond to my method:

-[CALayer drawHourCircleWithDayLightStart:dayLightEnd:animated:]: unrecognized selector sent to instance 0x6a6d560**

It seems like my custom layer is not treated as HourCircleLayer but as CALayer. But do I have to change here?

My implementation looks like this:

@interface HourCircleLayer : CALayer
   @property (strong) UIColor *dayColor;   // default yellowColor
   @property (strong) UIColor *nightColor; // default blueColor
   + (id)layer;
   - (void)drawHourCircleWithDayLightStart:(NSDate *)startDate dayLightEnd:(NSDate *)endDate animated:(BOOL)animated;
@end

@implementation HourCircleLayer
  + (id)layer
  {
     id layer = [CALayer layer];
     [layer setValue:[UIColor yellowColor] forKey:kDayPortionColorKey];
     [layer setValue:[UIColor blueColor]   forKey:kNightPortionColorKey];

     return layer;
  }

  - (void)drawHourCircleWithDayLightStart:(NSDate *)startDate dayLightEnd:(NSDate *)endDate animated:(BOOL)animated
  {
     self.withAnimation = animated;
     [self setDayLightStart:[self hourAngleForDate:startDate]];
     [self setDayLightEnd:[self hourAngleForDate:endDate]];
  }

@end

Solution

  • This is because you initialize CALayer, but not your HourCircleLayer. In your + (id)layer method change:

    id layer = [CALayer layer];
    

    to

    HourCircleLayer layer = [[HourCircleLayer alloc] init];
    ...
    
    return [layer autorelease]; //Use autorelease if not using ARC