Using CALayer Delegate

前端 未结 8 1122
滥情空心
滥情空心 2020-12-02 08:44

I have a UIView whose layers will have sublayers. I\'d like to assign delegates for each of those sublayers, so the delegate method can tell the layer what to draw. My que

8条回答
  •  孤街浪徒
    2020-12-02 09:16

    I personally voted for Dave Lee's solution above as being the most encapsulating, particularly where you have multiple layers. However; when I tried it on IOS 6 with ARC I got errors on this line and suggesting that I need a bridged cast

    // [_view performSelector: selector withObject: layer withObject: (id)context];
    

    I therefore amended Dave Lee's drawLayer method from his re-delegating delegate class to employ NSInvocation as below. All usage and ancillary functions are identical to those Dave Lee posted on his earlier excellent suggestion.

    -(void) drawLayer: (CALayer*) layer inContext: (CGContextRef) context
    {
        NSString* methodName = [NSString stringWithFormat: @"draw%@Layer:inContext:", layer.name];
        SEL selector = NSSelectorFromString(methodName);
    
        if ( ![ _view respondsToSelector: selector])
        {
            selector = @selector(drawLayer:inContext:);   
        }
    
        NSMethodSignature * signature = [[_view class] instanceMethodSignatureForSelector:selector];
        NSInvocation * invocation = [NSInvocation invocationWithMethodSignature:signature];
    
        [invocation setTarget:_view];             // Actually index 0    
        [invocation setSelector:selector];        // Actually index 1    
    
        [invocation setArgument:&layer atIndex:2];
        [invocation setArgument:&context atIndex:3];
    
        [invocation invoke];
    
    }
    

提交回复
热议问题