自定义CALayer不会响应选择器

时间:2012-06-19 07:38:22

标签: ios calayer

我需要一个自定义图层(由新变量和方法扩展),并且如下所示。 问题是图层不会响应我的方法:

  

- [CALayer drawHourCircleWithDayLightStart:dayLightEnd:animated:]:无法识别的选择器发送到实例0x6a6d560 **

似乎我的自定义图层不被视为HourCircleLayer而是被视为CALayer。 但我必须在这里改变吗?

我的实现如下:

@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

2 个答案:

答案 0 :(得分:1)

这是因为您初始化CALayer,而不是HourCircleLayer。在+ (id)layer方法更改:

id layer = [CALayer layer];

HourCircleLayer layer = [[HourCircleLayer alloc] init];
...

return [layer autorelease]; //Use autorelease if not using ARC

答案 1 :(得分:1)

第二种方法不是类方法,所以你应该这样做

HourCircleLayer *hourCircleLayer = [[HourCircleLayer alloc] init];
[hourCircleLayer drawHourCircleWithDayLightStart:dayLightEnd:animated:];