【问题标题】:how to draw vertical line in UIScrollView如何在 UIScrollView 中绘制垂直线
【发布时间】:2012-07-03 20:14:44
【问题描述】:

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

这条线基本上是 10px 宽,灰色,从 scrollView 的顶部到底部。

这就是我现在所拥有的,我基本上覆盖了 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);     
    }

}

【问题讨论】:

    标签: iphone objective-c ios ipad


    【解决方案1】:

    嗯,最简单的方法就是添加一个 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];
    }
    

    您不需要“非常非常大的高度”,因为您通过滚动视图知道最大高度。

    【讨论】:

    • 这将比仅仅画线要多得多,因为你会实例化很多不需要的东西。
    • 这就是为什么这是“最简单的”。 :P
    • 我同意 j_mcnally.. 我认为 Core Graphics 会更优化?
    • 哦,顺便说一下,UIScrollView 没有 viewDidLoad.. 你是说在有 UIScrollView 的 UIViewController 中执行此操作吗?如果是这样,那不是我想要的。我需要这个在 UIScrollView 中
    • @j_mcnally 实际上,这比覆盖 drawRect 并使用 CoreGraphics 的方式快得多。这一切都与 CoreAnimation 图层系统的工作方式有关。
    【解决方案2】:
    - (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 的控制器中

    我给你的代码画了一条水平线,但你应该能够适应它。

    【讨论】:

    • 我需要调用[super drawRect]吗?另外,如果我的 UIScrollLView 中有一个 BOOL 来确定是否应该绘制这条线,该怎么办?这行得通吗?我可以在drawRect中放一个if语句吗?我只是不确定何时会调用 drawRect
    • drawRect 在我的经验中绘制视图时被调用我不需要调用 super 我完全按照我粘贴的方式进行操作,抱歉我没有时间为你修改它但它应该很简单.您可以将所有代码包装成一个 if 语句来检查您的 BOOL。我使用代码在表格单元格之间绘制自定义插入分隔符。
    猜你喜欢
    • 1970-01-01
    • 2011-05-26
    • 1970-01-01
    • 2015-04-04
    • 2018-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-27
    相关资源
    最近更新 更多