简体   繁体   中英

CALayer/UIView crashes when I touch the UIVIew

Iam using a custom layer for drawing on my UIView . And to invalidate and update the layer I need to set my view to be the delegate of my CALayer . When this is done it draws my text correctly but when the view receives touches the app crashes. What I am a doing wrong?

- (void) layoutSubView {
    if (drawLayer == nil) {
        drawLayer = [[CALayer alloc] init];
        [drawLayer setDelegate:self];
        [[self layer] addSublayer:drawLayer];
    }
    [drawLayer setFrame:[self bounds]]; 
    [drawLayer setNeedsDisplay];
}

Above is my code to setup the CALayer, and below is the overridden drawLayer method.

- (void) drawLayer:(CALayer *)layer 
     inContext:(CGContextRef)ctx {

        if (layer == drawLayer) {
        UIGraphicsPushContext(ctx);
        [self drawTitle:ctx];
        UIGraphicsPopContext();
    }
}

Here is the stack.

在此处输入图像描述

You can't use a UIView object to be the delegate of a layer other than its own layer. Its stated here . You will need to assign some other object as its delegate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM