0

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

-[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
4

2 回答 2

1

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

id layer = [CALayer layer];

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

return [layer autorelease]; //Use autorelease if not using ARC
于 2012-06-19T07:44:41.473 回答
1

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

HourCircleLayer *hourCircleLayer = [[HourCircleLayer alloc] init];
[hourCircleLayer drawHourCircleWithDayLightStart:dayLightEnd:animated:];
于 2012-06-19T08:23:26.843 回答