2

グループ化されたテーブル セルを drawRect メソッドで描画しようとしています。次の結果が得られますが、1 つ問題があります。外側の境界線をより暗くしたいのですが、これを達成できないようです。これは私の図面の問題であると確信しています。

セルの外側の境界線ではなく、セル間の中央の線の色が好きです。

編集:

-(void)drawRect:(CGRect)rect
{
    const float kLineWidth = 3.0;

    UIColor *topLineColor = [UIColor whiteColor];
    UIColor *bottomLineColor = [UIColor colorWithRed:225.0f/255.0f green:225.0f/255.0f blue:225.0f/255.0f alpha:1.0f];
    UIColor *backgroundColor = [UIColor colorWithRed:242.0f/255.0f green:242.0f/255.0f blue:242.0f/255.0f alpha:1.0f];

    CGColorRef bottomSeparatorColorRef = [bottomLineColor CGColor];
    CGColorRef topSeparatorColorRef = [topLineColor CGColor];

    CGContextRef context = UIGraphicsGetCurrentContext();


    UIRectCorner corners = 0;

    switch(self.position) {

        case OTCellBackgroundViewPositionTop:
            corners = UIRectCornerTopLeft | UIRectCornerTopRight;
            break;

        case OTCellBackgroundViewPositionMiddle:
            break;

        case OTCellBackgroundViewPositionBottom:
            corners = UIRectCornerBottomLeft | UIRectCornerBottomRight;
            break;

        default:
            break;
    }

    [backgroundColor setFill];
    [topLineColor setStroke];

    UIBezierPath *bezierPath =   [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corners cornerRadii:CGSizeMake(10.0f, 10.0f)];

    [bezierPath fill];
    [bezierPath stroke];
    [bezierPath setLineWidth:3.0f];

    if (self.position == OTCellBackgroundViewPositionTop) {
        // Draw the Bottom Line
        CGContextSetStrokeColorWithColor(context, bottomSeparatorColorRef);
        CGContextSetLineWidth(context, kLineWidth);
        CGContextSetLineCap(context, kCGLineCapSquare);
        CGContextMoveToPoint(context, 0.0, rect.size.height);
        CGContextAddLineToPoint(context, rect.size.width, rect.size.height);
        CGContextStrokePath(context);   
    }

    if (self.position == OTCellBackgroundViewPositionBottom) {
        // Draw the Top Line
        CGContextSetStrokeColorWithColor(context, topSeparatorColorRef);
        CGContextSetLineWidth(context, kLineWidth);
        CGContextSetLineCap(context, kCGLineCapSquare);
        CGContextMoveToPoint(context, 0.0, 0.0);
        CGContextAddLineToPoint(context, rect.size.width, 0);
        CGContextStrokePath(context);
    }
}
4

3 に答える 3

0

このようなものがより扱いやすいかもしれないと思います。bezierPath のサイズなどを微調整して、正しく設定する必要があります。

UIRectCorner corners = 0;

switch(position) {
  case OTCellBackgroundViewPositionTop:
    corners = UIRectCornerTopLeft | UIRectCornerTopRight;
    break;

  case OTCellBackgroundViewPositionMiddle:
    break;

  case OTCellBackgroundViewPositionBottom:
    corners = UIRectCornerBottomLeft | UIRectCornerBottomRight;
    break;
}

[backgroundColor setFill];
[topLineColor setStroke];

UIBezierPath *bezierPath =   [UIBezierPath bezierPathWithRoundedRect:rect
                                                   byRoundingCorners:corners 
                                                         cornerRadii:CGSizeMake(3.f, 3.f)];

[bezierPath fill];
[bezierPath stroke];

// Now stroke over the lines in a different colour.

編集

あなたが投稿したコードを見てください。

  1. 一番上の線の色を白に設定しました。したがって、白です。必要な灰色に設定するつもりだったのかもしれません。

  2. 描画中のアウトラインがクリッピングされています。これを解決するには、描画している線の幅の半分だけ四角形を挿入します。

    rect = CGRectInsetRect(rect, 0.5f * kLineWidth, 0.5f * kLineWidth);
    
  3. 線をストロークする前にストローク幅を設定する必要があります。そうしないと、デフォルトが使用されます

    [bezierPath setLineWidth:kLineWidth];
    [bezierPath stroke];
    
于 2012-11-04T00:47:14.107 に答える
0

最上位セルのコードを見てみましょう。まず、セル全体の輪郭を表すパスを作成します。次に、背景を塗りつぶします。次に、2 つの側面と上部をなでます。次に、セルの下部をストロークします-良いです。次に、他の部分を実行する前に、作成した完全なアウトライン パスを追加してストロークします。悪いことです。

作成したパスを削除し、そのパスをストロークしないでください。背景、トップライン、ボトムラインのコードだけで十分です。

最後に行う余分なストロークは、最後の行に使用した最後の色で完全なセルのアウトラインを再描画することです. これはあなたのトップラインストロークをカバーしています.

if-elseちなみに、次のセットアップを使用する必要があります。

if (_position == OTCellBackgroundViewPositionTop) {
    // draw top cell
} else if (_position == OTCellBackgroundViewPositionMiddle) {
    // draw middle cell
} else if (_position == OTCellBackgroundViewPositionBottom) {
    // draw bottom cell
)

この種の構造は、実行可能なパスが 1 つだけ必要な場合に常に使用する必要があります。あなたが持っていた構造は、複数のパスが実行される可能性がある場合、または実行されることが望ましい場合に適しています。

于 2012-11-04T00:39:17.540 に答える
0

トップセパレーターを白に設定しています。

UIColor *topLineColor = [UIColor whiteColor];
CGColorRef topSeparatorColorRef = [topLineColor CGColor];
// Top Line
CGContextSetStrokeColorWithColor(context, topSeparatorColorRef);

暗い色に設定するだけです。

于 2012-11-04T00:38:04.633 に答える