0

CGContextFillRects で使用する CGRect の配列を保存しようとしていますが、minorPlotLines 配列に割り当てた CGRect 変数が保存されていないようです。ここのオブジェクトが自分自身を描画するまでに、minorPlotLines は空です! 誰が何が起こっているのか知っていますか?

@interface GraphLineView () {
    int numberOfLines;
    CGRect *minorPlotLines;
}


@end


@implementation GraphLineView


- (instancetype) initWithFrame: (CGRect) frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Init code
        [self setupView];
    }
    return self;
}

- (instancetype) initWithCoder: (NSCoder *) aDecoder {
    if(self == [super initWithCoder:aDecoder]){
        [self setupView];
    }
    return self;
}

- (void) dealloc {
    free(minorPlotLines);
}

- (void) setupView {

    numberOfLines = 40;
    minorPlotLines = malloc(sizeof(struct CGRect)*40); 

    for(int x = 0; x < numberOfLines; x += 2){
        //minorPlotLines[x] = *(CGRect*)malloc(sizeof(CGRect));
        minorPlotLines[x] = CGRectMake(x*(self.frame.size.width/numberOfLines), 0, 2, self.frame.size.height);

       // minorPlotLines[x+1] = *(CGRect*)malloc(sizeof(CGRect));
        minorPlotLines[x+1] = CGRectMake(0, x*(self.frame.size.height/numberOfLines), self.frame.size.width, 2);

    }

    [self setNeedsDisplay];
}

- (void) drawRect:(CGRect)rect {
    // Drawing code
    [super drawRect:rect];

    for(int x = 0; x < numberOfLines; x += 2){
        NSLog(@"R %d = %f", x, minorPlotLines[x].origin.x);
        NSLog(@"R %d = %f", x+1, minorPlotLines[x+1].origin.y);
    }

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [[UIColor yellowColor] CGColor]);
    CGContextFillRects(context, minorPlotLines, numberOfLines);

}
4

1 に答える 1

1

私はあなたのコードを(コンテンツを構築し、minorPlotLines後でコンテンツを読み戻すために)別のプロジェクトに引っ張ってみました。

minorPlotLines配列を作成している時点で(つまり で) 、実際にゼロ以外のフレームがあることを確認します-setupView。クラスが部分的にしか構築されていないときに初期段階の UI クラス読み込みコールバックが呼び出されることはよくあることです (例: UIViewController クラスの callback -viewDidLoad)。特にレイアウトはゲームの比較的後半に発生します。-setupViewメソッドはメソッド内で直接呼び出される-initため、フレームワークはまだクラスにレイアウトを提供していないため、使用可能なフレームがありません (つまり、フレームは効果的にと同等CGRectZero)。

于 2016-06-08T22:27:07.117 に答える