0

ロジックに応じて表示/非表示にするいくつかのビューを含むViewControllerベースのアプリがあります。フレーム/ボーダーの形にするために、UIViewのサイズになる長方形を描きたいと思います。

長方形の描画に問題があります。次のコードで処理できることは理解していますが、このメソッドが呼び出されない、またはトリガーされない理由がわかりません。また、(void)drawRect:(CGRect)rectメソッドがどこにも生成されていないので、自分で配置しました。ここで何が欠けているのかわかりません。

- (void)drawRect:(CGRect)rect;
{   
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetRGBStrokeColor(context, 1.0, 1.0, 0.0, 1.0); // yellow line

    CGContextBeginPath(context);

    CGContextMoveToPoint(context, 50.0, 50.0); //start point
    CGContextAddLineToPoint(context, 250.0, 100.0);
    CGContextAddLineToPoint(context, 250.0, 350.0);
    CGContextAddLineToPoint(context, 50.0, 350.0); // end path

    CGContextClosePath(context); // close path

    CGContextSetLineWidth(context, 8.0); // this is set from now on until you explicitly change it

    CGContextStrokePath(context); // do actual stroking

    CGContextSetRGBFillColor(context, 0.0, 1.0, 0.0, 0.5); // green color, half transparent
    CGContextFillRect(context, CGRectMake(20.0, 250.0, 128.0, 128.0)); // a square at the bottom left-hand corner
}
4

3 に答える 3

2

単純な長方形の境界線を追加するだけの場合は、ViewControllerに対して次の手順を実行しますviewWillAppear:

- (void) viewWillAppear:(BOOL)animated
{
   //Simple border on the main view
   self.view.layer.borderColor = [UIColor redColor].CGColor;
   self.view.layer.borderWidth = 2;

   //Or a simple rectangle to place at x,h within the main view 
   UIView *test = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
   test.backgroundColor = [UIColor redColor];
   [self.view addSubview:test];
}

お役に立てれば!

于 2012-06-15T21:04:00.013 に答える
0

推測するだけですが、UIView自分自身を再表示するように指示しますか?

[myUIView setNeedsDisplay];

そうして初めて、drawRect:getが呼び出されます。

于 2012-06-15T21:01:23.133 に答える
0

まず、のYourViewサブクラスであるclass()を作成しますUIView。viewControllerにコードを実装します。

- (void)viewDidLoad
{
   YourView *temp = [[YourView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];

    [self.view addSubview:temp];
}

- (void)drawRect:(CGRect)rectmethod( )をYourView.mファイルに書き込みます。このようにしてみてください。お役に立てればと思います。

于 2012-06-16T07:41:31.213 に答える