1

drawRect を使用して図形を描画する非常に単純なプロジェクトがありますが、drawRect 関数が実際に呼び出されているとは思えません。これがコードです。(他のすべては、「シングル ビュー アプリケーション」のデフォルトです。)

-(void)drawRect:(CGRect)aRect{
    NSLog(@"testing...");
    CGContextRef context=UIGraphicsGetCurrentContext();
    CGContextBeginPath(context);
    CGContextMoveToPoint(context, 50, 50);
    CGContextAddLineToPoint(context, 100, 120);
    CGContextAddLineToPoint(context, 20, 100);
    CGContextClosePath(context);

    [[UIColor greenColor] setFill];
    [[UIColor redColor] setStroke];

    CGContextDrawPath(context,kCGPathFillStroke);
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view setNeedsDisplay];
}

読んでくれてありがとう!

4

1 に答える 1

6

をサブクラスに配置しdrawRect:たようです。の do not implement 、do のサブクラス。UIViewControllerUIViewControllerdrawRect:UIView

UIViewの実装でのカスタム サブクラスを作成し、これをビュー コントローラーのプロパティの子として、またはルート ビュー自体としてdrawRect:ビュー階層に追加する必要があります。self.view

于 2012-11-01T09:21:08.137 に答える