2011-07-19 14 views
1

グリッドに水平および/または垂直線を表示しようとしています。だからUIViewサブクラスGridLinesを作成して、線を描画するのはdrawRect:です。次に、これらのうち2つを作成し(1つは垂直、1つはない)、サブビューとしてプライマリビューに追加します(CanvasUIViewも得られました。 Canvasに追加した他のカスタムサブビューは、適切に表示、削除などされますが、GridLinesオブジェクトは表示されず、drawRect:も呼び出されません。 [Canvas drawRect:](これは現在無効になっています)にこの線画のコードがあると、グリッドが正しく表示されます。UIViewのdrawRectが呼び出されていません

類似の質問を見ると、UIView派生クラスでdrawRectが呼び出されていますが、私の場合はUIViewです。

ここに何か不足していますか?

GridLines.h:

#import <UIKit/UIKit.h> 
@interface GridLines : UIView 
{ 
    BOOL mVertical; 
} 
- (id) initWithFrame: (CGRect) _frame vertical: (BOOL) _vertical; 
@end 

GridLines.m:別のUIViewの派生クラスで

#import "GridLines.h" 
@implementation GridLines 
- (id) initWithFrame: (CGRect) _frame vertical: (BOOL) _vertical 
{ 
    if ((self = [super initWithFrame: _frame])) 
    { 
    mVertical = _vertical; 
    } 

    return self; 
} 

- (void) drawRect: (CGRect) _rect 
{ 
    NSLog(@"[GridLines drawRect]"); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    [[UIColor clearColor] set]; 
    UIRectFill([self bounds]); 

    if (mVertical) 
    { 
    for (int i = 50; i < self.frame.size.width; i += 50) 
    { 
     CGContextBeginPath(context); 
     CGContextMoveToPoint(context, (CGFloat)i, 0.0); 
     CGContextAddLineToPoint(context, (CGFloat)i, self.frame.size.height); 
     [[UIColor grayColor] setStroke]; 
     CGContextSetLineWidth(context, 1.0); 
     CGContextDrawPath(context, kCGPathFillStroke); 
    } 
    } 
    else 
    { 
    for (int j = 50; j < self.frame.size.height; j += 50) 
    { 
     CGContextBeginPath(context); 
     CGContextMoveToPoint(context, 0.0, (CGFloat)j); 
     CGContextAddLineToPoint(context, self.frame.size.width, (CGFloat)j); 
     [[UIColor grayColor] setStroke]; 
     CGContextSetLineWidth(context, 1.0); 
     CGContextDrawPath(context, kCGPathFillStroke); 
    } 
    } 
} 
@end 

- (void) createGrids 
{ 
    mHorizontalLines = [[GridLines alloc] initWithFrame: self.frame vertical: NO]; 
    mVerticalLines = [[GridLines alloc] initWithFrame: self.frame vertical: YES]; 
    [self addSubview: mHorizontalLines]; 
    [self addSubview: mVerticalLines]; 
} 

答えて

1

あなたはframe v bounds問題がある可能性があります。

mHorizontalLines = [[GridLines alloc] initWithFrame: self.frame vertical: NO]; 
mVerticalLines = [[GridLines alloc] initWithFrame: self.frame vertical: YES]; 

frameself.frameが、それはあなたがそのフレームにこれらの新しいビューを設定すると、彼らはselfの矩形にクリップされます、広いよりことをオフセットされた場合には、スーパービューの座標空間に保たれています。

これをself.boundsに変更してください。起源= {0,0}

である必要があります
関連する問題