如何在UIScrollView中绘制垂直线 [英] how to draw vertical line in UIScrollView

查看:79
本文介绍了如何在UIScrollView中绘制垂直线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UIScrollView,我想画一条垂直线,其高度为UIScrollView contentHeight.如何在不牺牲性能的情况下轻松做到这一点?我正在考虑使用一个非常大的高度的UIVIew(因为我们不知道scrollView的contentHeight是什么)并将其添加为UIScrollView的子视图.有没有比这更好的方法了?

I have a UIScrollView and I want to draw a vertical line that has a height of the UIScrollView contentHeight. How do I do this easily and without sacrificing performance? I am thinking of using a UIVIew with a very-very large height (because we don't know what the contentHeight of the scrollView will be) and adding it as a subview of the UIScrollView. Is there a better approach than this?

该行基本上是10像素宽的灰色,从scrollView的顶部到底部.

The line is basically 10px width, gray color, that spans from the top to the bottom of the scrollView.

这是我现在拥有的,基本上是在重写UIScrollView drawRect:

Here's what I have as of now, I am basically overriding the UIScrollView drawRect:

- (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);     
    }

}

推荐答案

嗯,最简单的方法就是添加UIView并设置背景颜色.

Ummm, the easiest way would be too just add a UIView and set the background color.

-(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知道最大高度.

You don't need a "very very large height" because you know the maximum height via the scrollView.

这篇关于如何在UIScrollView中绘制垂直线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆