0

UIScrollViewがあり、UIScrollViewcontentHeightの高さの垂直線を描画したいと思います。パフォーマンスを犠牲にすることなく、これを簡単に行うにはどうすればよいですか?高さが非常に大きいUIVIewを使用して(scrollViewのcontentHeightがわからないため)、UIScrollViewのサブビューとして追加することを考えています。これよりも良いアプローチはありますか?

線は基本的に10px幅、灰色で、scrollViewの上部から下部にまたがっています。

これが私が今持っているものです、私は基本的にUIScrollViewdrawRectをオーバーライドしています:

- (void)drawRect:(CGRect)rect
{
    if (shouldDrawVerticalLineForProfile){
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGColorRef separatorColor = [UIColor colorWithRed:47.0/255.0 green:47.0/255.0 
                                                     blue:47.0/255.0 alpha:1.0].CGColor;

        // Add at bottom
        CGPoint startPoint = CGPointMake(30, 0);
        CGPoint endPoint = CGPointMake(30, 10000);

        CGContextSaveGState(context);
        CGContextSetLineCap(context, kCGLineCapSquare);
        CGContextSetStrokeColorWithColor(context, separatorColor);
        CGContextSetLineWidth(context, 5.0);
        CGContextMoveToPoint(context, startPoint.x + 0.5, startPoint.y + 0.5);
        CGContextAddLineToPoint(context, endPoint.x + 0.5, endPoint.y + 0.5);
        CGContextStrokePath(context);
        CGContextRestoreGState(context);     
    }

}
4

2 に答える 2

5

うーん、最も簡単な方法は、UIView を追加して背景色を設定することです。

-(void)viewDidLoad {
   [super viewDidLoad];

   UIView *verticalBar = [[[UIView alloc] init] autorelease];
   [verticalBar setBackgroundColor:[UIColor grayColor]];
   [verticalBar setFrame:CGRectMake(0,0,10,[scrollView contentSize].height)];

   [[self view] addSubview:verticalBar];
}

scrollView を介して最大の高さを知っているため、「非常に大きな高さ」は必要ありません。

于 2012-07-03T20:18:25.417 に答える
0
- (void)drawRect:(CGRect)rect {
  CGContextRef ctx = UIGraphicsGetCurrentContext();
  CGContextSetStrokeColorWithColor(ctx, [UIColor colorWithRed: 0.0 green:0.0 blue:0.0 alpha:.6].CGColor);
  CGContextSetLineWidth(ctx, 1);

  CGContextBeginPath(ctx);
  CGContextMoveToPoint(ctx, 0, CGRectGetMaxY(rect) - 2);
  CGContextAddLineToPoint(ctx, CGRectGetMaxX(rect), CGRectGetMaxY(rect) - 2);
  CGContextStrokePath(ctx);
  CGContextSetLineWidth(ctx, 2);
  CGContextSetStrokeColorWithColor(ctx, [UIColor colorWithRed: .34 green:.35 blue:.352 alpha:.6].CGColor);

  CGContextBeginPath(ctx);
  CGContextMoveToPoint(ctx, 0, CGRectGetMaxY(rect));
  CGContextAddLineToPoint(ctx, CGRectGetMaxX(rect), CGRectGetMaxY(rect));
  CGContextStrokePath(ctx);




}

これを UIScrollView のコントローラーに追加します

私があなたに与えたコードは水平線を描画しますが、あなたはそれを適応させることができるはずです.

于 2012-07-03T20:27:51.343 に答える