我有两个 UIScrollView 实例,我想在它们之间画一条垂直线(不是 100% 高度,更像是 80%)。我怎样才能做到这一点?
最佳答案
使用核心图形。将两个 UIScrollView 嵌套在另一个 View 中,并覆盖父级的 drawRect 方法。参见 Apple 的 UIView documentation对于 drawRect 更多细节和 Core Graphics Context Reference有关绘图的更多信息。
- (void) drawRect: (CGRect) rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
// Draw divider
UIGraphicsPushContext(context);
// Fill the background, this must happen if the view is not opaque.
if (self.opaque)
{
[[UIColor whiteColor ] set];
CGContextFillRect(context, rect);
}
[[UIColor grayColor] set];
CGContextSetLineWidth(context, 1);
CGContextBeginPath(context);
CGContextMoveToPoint(context, rect.size.width * 0.5, 0.1 * rect.size.height);
CGContextAddLineToPoint(context, rect.size.width * 0.5, 0.9 * rect.size.height);
CGContextStrokePath(context);
UIGraphicsPopContext();
}
编辑
为了解决我的疏忽,据我所知,没有一种简单的方法可以在 subview 上绘制。
可以使用与上面相同的基本 View 层次结构来模拟此行为。子类 UIView 以创建不接受任何触摸事件的完全透明 View 。将绘图代码(我在上面的代码中添加了不透明度的条件)放在自定义 View 中,然后在 View 层次结构的前面插入自定义 View 的实例。
关于iphone - 在两个 ScrollView 之间添加垂直线(边框),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4686344/