質問

I have created a CALayer subclass like this:

#import <QuartzCore/QuartzCore.h>

@interface SHBaseLayer : CALayer

@end

and implementation:

#import "SHBaseLayer.h"

@implementation SHBaseLayer

- (void)drawInContext:(CGContextRef)theContext
{
  CGContextSetStrokeColorWithColor(theContext, [UIColor blueColor].CGColor);
  CGMutablePathRef thePath = CGPathCreateMutable();
  CGPathMoveToPoint(thePath,NULL,15.0f,15.f);
  CGPathAddCurveToPoint(thePath,
                        NULL,
                        15.f,250.0f,
                        295.0f,250.0f,
                        295.0f,15.0f);
  CGContextBeginPath(theContext);
  CGContextAddPath(theContext, thePath);
  CGContextSetLineWidth(theContext, 5);

  CGContextDrawPath(theContext, kCGPathStroke);
}

@end

and I am adding this layer in a view controller view's layer and calling setNeedsDisplay but nothing is showing when i run the app.

this is my viewDidLoad of the ViewController.

- (void)viewDidLoad
{
  [super viewDidLoad];

  SHBaseLayer *l = [SHBaseLayer layer];
  [self.view.layer addSublayer:l];
  [l setNeedsDisplay];
}

what is it that I am doing wrong here?

役に立ちましたか?

解決

You need to set the frame of the layer. The default frame is { 0,0,0,0 }, which is never going to work.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top