0

これを実行すると、位置が少しずれて表示されます。これは、drawInContext 関数を備えた単純な CALayer サブクラスであり、View Controller に配置されます。

レイヤーを作成して追加すると、コントローラー コード内のこのコードで実行されます。これが実行されるので、レイヤーが作成されます。しかし、位置が正しくありません。縦向きの方がうまくいくようですが、回転を検出する方法も必要です(ただし、それはすぐには問題になりません)。

- (void)viewDidLoad
{
    [super viewDidLoad];

    Canvas *c = [Canvas layer];

    self.canvas = c;
    [self.view.layer addSublayer: self.canvas];
    //c.frame = self.view.bounds;
    self.canvas.anchorPoint = CGPointMake(0, 0);
    self.canvas.position = CGPointMake(0, 0);
    self.canvas.bounds = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);


    self.canvas.backgroundColor = [UIColor lightGrayColor].CGColor;

    [self.canvas setNeedsDisplay];


}

主な問題は実際の描画にあるようで、コードは次のとおりですが、needsDisplay を呼び出しても実際には実行されません。

- (void)drawInContext:(CGContextRef)ctx {
    [super drawInContext: ctx];

    CGContextSaveGState(ctx);
    UIGraphicsPushContext(ctx);

//    CGRect b = self.bounds;

    [self drawOrigin];

    UIGraphicsPopContext();
    CGContextRestoreGState(ctx);

}

- (void) drawOrigin {


    CGPoint centre = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);

    UIBezierPath *hl = [UIBezierPath bezierPath];
    [hl moveToPoint: centre];
    [hl addLineToPoint: CGPointMake(centre.x, 0)];

    [hl moveToPoint: centre];
    [hl addLineToPoint: CGPointMake(centre.x, self.bounds.size.height)];

    [hl moveToPoint: centre];
    [hl addLineToPoint: CGPointMake(0, centre.y)];

    [hl moveToPoint: centre];
    [hl addLineToPoint: CGPointMake(self.bounds.size.width, centre.y)];

    [hl stroke];
}

このコードを他に探す場所がわかりません。アイデアや方向性は大歓迎です。

4

0 に答える 0