ios - ScrollView 上绘制的线条不是滚动的?

标签 ios objective-c uiscrollview drawrect

在我的应用程序中,我将 UIScrollView 子类化并在 drawRect 方法中绘制水平线

我将 ContentSize 的高度设置为大于 scrollView 的高度。我用下面的方法画线

但我的问题是我画的线没有随 View 滚动。

-(id)initWithFFrame:(CGRect)rect
{
  _gap=36;
}
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    int end=0;
    NSLog(@"called");
    int x=self.frame.size.width;
    while (end<self.contentSize.height) {
        CGContextRef context=UIGraphicsGetCurrentContext();
        CGContextSetLineWidth(context, 1);
        CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
        CGContextMoveToPoint(context, 0, end);
        CGContextAddLineToPoint(context, x, end);
        CGContextStrokePath(context);
        end=end+_gap;
    }
}

我可以注意到滚动条在滚动时上下移动,但线条没有移动。

最佳答案

问题是这些行是 UIScrollView 的一部分,而不是它的内容。

您需要创建一个新的 UIView 子类,在那里绘制内容,并将其添加到您的 UIScrollView

关于ios - ScrollView 上绘制的线条不是滚动的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16279761/

相关文章:

ios - 使用 AVFoundation 框架进行音频处理

c# - MonoTouch - 来自 Pointer 的 CGImage 使应用程序崩溃 - iPhone

iOS - 何时在主线程中调用 UI 更改函数

iphone - NSURLConnection 因 URL 无效而挂起

ios - 如何编码或隐藏 iOS 应用程序文件?

ios - 在快速下拉 UITableView 时为 TableView 的 headerView 设置动画

ios - 从 ScrollView 连续滚动到嵌套 Collection View

ios - 快速查询到 Firebase Firestore 嵌套数组

ios - 图像的 Swift 类型注释

iphone - 两个 UIScrollViews 得到 [__NSCFConstantString _isDecompressing] 错误