55

私はまだCGContextで線を引くのに苦労しています。私は実際に線を引いて描画しましたが、既存の背景が透けて見えるように Rect の背景を透明にする必要があります。ここに私のテストコードがあります:

(void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
    CGContextSetAlpha(context,0.0);
    CGContextFillRect(context, rect);

    CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextSetLineWidth(context, 5.0);
    CGContextMoveToPoint(context, 100.0,0.0);
    CGContextAddLineToPoint(context,100.0, 100.0);
    CGContextStrokePath(context);
}

何か案は?

4

8 に答える 8

75

UIGraphicsGetCurrentContext()通話後CGContextClearRect(context,rect)

編集:わかりました。

行を含むカスタム ビューには、次のものが必要です。

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setBackgroundColor:[UIColor clearColor]];
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextClearRect(context, rect);
    CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextSetLineWidth(context, 5.0);
    CGContextMoveToPoint(context, 100.0,0.0);
    CGContextAddLineToPoint(context,100.0, 100.0);
    CGContextStrokePath(context);
}

私のテストでは、これを非常に基本的な UIViewController として使用しました。

- (void)viewDidLoad {
    [super viewDidLoad];
    UIImageView *v = [[UIImageView alloc] initWithFrame:self.view.bounds];
    [v setBackgroundColor:[UIColor redColor]];
    [self.view addSubview:v];
    TopView *t = [[TopView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:t];
    [v release];
    [t release];
}
于 2010-01-24T01:45:52.330 に答える
43

簡単な方法:

- (id)initWithFrame:(CGRect)frame
{
    if ((self = [super initWithFrame:frame]))
    {       
        self.opaque = NO;
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextClearRect(context, rect);
    //your code
}
于 2012-01-28T14:17:47.600 に答える
4

これは、InterfaceBuilder を使用して手動で追加された UIImage で機能したものです。

- (id)initWithCoder:(NSCoder *)aDecoder {

    if(self = [super initWithCoder:aDecoder]) {
        self.backgroundColor = [UIColor clearColor];
    }

    return self;
}


-(void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();    
    CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextSetLineWidth(context, 5.0);
    CGContextMoveToPoint(context, 100.0,0.0);
    CGContextAddLineToPoint(context,100.0, 100.0);
    CGContextStrokePath(context);
}

David Kanarekの答えは、独自のUIImageViewを手動で作成している場合にのみ機能します。UIView を作成し、Interface Builder を介して手動で追加した場合は、代わりに initWithCoder メソッドを呼び出すこのような別のアプローチが必要になります。

于 2014-11-22T22:18:51.003 に答える
3

私は同じ問題を抱えています。init Method is -(id)initWithFrame:(CGRect)rect. In this methodを上書きしますself.background = [UIColor clearColor]; が、xib ファイルでこのビューを使用します!!! それは init メソッドを呼び出します

-(id)initWithCoder:(NSCoder*)aDecoder;

そのため、すべての init メソッドを上書きして、BackgroundColor を設定すると問題なく動作します。

于 2012-06-22T08:24:34.690 に答える
2
CGContextClearRect(context,rect) 

提供されたコンテキストがウィンドウまたはビットマップ コンテキストである場合、Quartz は効果的に四角形をクリアします。他のコンテキスト タイプの場合、Quartz はデバイスに依存する方法で四角形を塗りつぶします。ただし、ウィンドウまたはビットマップ コンテキスト以外のコンテキストでは、この関数を使用しないでください。

于 2011-09-26T09:25:56.170 に答える
0

ここで質問を理解するのに問題がありますが、描画している「トップ」ビューを通して「バックグラウンド」UIView を表示できない場合、1 つの解決策は、topView.backgroundColor = [UIColor clearColor];私がこの同じ問題を抱えていた (と思います) ことです。それは私のために。

于 2011-07-29T15:16:13.340 に答える