0
votes

I'm trying to animate a custom property, and as I've seen on several sources it seems this would be the way to go, but I'm missing something. Here's the CALayer subclass:

@implementation HyNavigationLineLayer

@dynamic offset;

- (instancetype)initWithLayer:(id)layer
{
    self = [super initWithLayer:layer];

    if (self) {

        HyNavigationLineLayer * other = (HyNavigationLineLayer*)layer;
        self.offset = other.offset;
    }

    return self;
}

-(CABasicAnimation *)makeAnimationForKey:(NSString *)key
{
    // TODO
    return nil;
}

- (id<CAAction>)actionForKey:(NSString *)event
{
    if ([event isEqualToString:@"offset"]) {
        return [self makeAnimationForKey:event];
    }

    return [super actionForKey:event];
}

+ (BOOL)needsDisplayForKey:(NSString *)key
{
    if ([key isEqualToString:@"offset"]) {
        return YES;
    }

    return [super needsDisplayForKey:key];
}

- (void)drawInContext:(CGContextRef)ctx
{
    NSLog(@"Never gets called");
}

@end

I believe this is the only relevant method on my view:

@implementation HyNavigationLineView

+ (Class)layerClass
{
    return [HyNavigationLineLayer class];
}

@end

And, finally, in my view controller:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Instantiate the navigation line view
    CGRect navLineFrame = CGRectMake(0.0f, 120.0f, self.view.frame.size.width, 15.0f);
    self.navigationLineView = [[HyNavigationLineView alloc] initWithFrame:navLineFrame];

    // Make it's background transparent
    self.navigationLineView.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.0f];
    self.navigationLineView.opaque = NO;

    [[self.navigationLineView layer] addSublayer:[[HyNavigationLineLayer alloc] init]];
    [self.view addSubview:self.navigationLineView];
}

The thing is that the drawInContext method is not called at all, although layerClass is. Am I missing something to make the layer draw?

1

1 Answers

0
votes

Solved it. Need to call setNeedsDisplay

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Instantiate the navigation line view
    CGRect navLineFrame = CGRectMake(0.0f, 120.0f, self.view.frame.size.width, 15.0f);
    self.navigationLineView = [[HyNavigationLineView alloc] initWithFrame:navLineFrame];

    // Make it's background transparent
    self.navigationLineView.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.0f];
    self.navigationLineView.opaque = NO;

    [[self.navigationLineView layer] addSublayer:[[HyNavigationLineLayer alloc] init]];
    [self.view addSubview:self.navigationLineView];
    [self.navigationLineView.layer setNeedsDisplay];
}