0
- (void)drawRect:(CGRect)rect
{
CGFloat red[4] = {1.0f, 0.0f, 0.0f, 1.0f};
CGContextSetStrokeColor(c, red);
CGContextBeginPath(c);
CGContextMoveToPoint(c, 150.0f, 200.0f);
CGContextSetLineWidth(c, 5.0f);
CGContextAddLineToPoint(c, 50,300);
CGContextAddLineToPoint(c, 260,300);
CGContextClosePath(c);
}

これが私のviewwillappear

-(void)viewWillAppear:(BOOL)animated
{
    [self.view setNeedsDisplay];         
}

ただし、ビューは線を描画できず、白い空白だけです。

4

3 に答える 3

1

これを試してみましたか?

- (void)drawRect:(CGRect)rect
    [super drawRect:rect];

    CGContextRef context    = UIGraphicsGetCurrentContext();

    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);

    // Draw them with a 2.0 stroke width so they are a bit more visible.
    CGContextSetLineWidth(context, 2.0);


    CGContextMoveToPoint(context, 0,0); //start at this point

    CGContextAddLineToPoint(context, 20, 20); //draw to this point

    // and now draw the Path!
    CGContextStrokePath(context);
}
于 2012-11-22T04:08:53.607 に答える
1

サブクラスdrawRectでメソッドを使用しているようです。UIViewControllerただし、メソッドはサブクラスdrawRectにのみ適用されます。UIView

私が正しければ、次のことを行います。それ以外の場合は、@Anoop Vaidya の回答を参照してください。それはうまくいくはずです。

をサブクラス化しUIView、すべての作業を行いdrawRect、そのビューをView Controllerに追加します。

@interface myView : UIView
{

}
@end

@implementation myView

- (void)drawRect:(CGRect)rect{

}

@end

    -(void)viewWillAppear:(BOOL)animated
{
    myView *view1  = [[myView alloc]init];
    self.view =  view1;        
}
于 2012-11-22T04:24:23.787 に答える
1

このdrawRect チュートリアルを使用すると、有利なスタートを切ることができます。あなたの問題について、描画しているUIViewが表示されているビューであることを確認してください.UIViewをオーバーライドしているか、このビューの上に他のビューがあるため、変更を見ることができません. また、ビューを表示する前にビューを初期化するのを忘れた可能性があります。

于 2012-11-22T05:36:53.310 に答える