【问题标题】:For loop in drawRectdrawRect中的for循环
【发布时间】:2014-02-22 11:45:22
【问题描述】:

我需要在视图控制器中绘制图表。我已经创建了一个滚动视图和一个视图作为我的 self.view 的子视图,具有自动布局。我的数据来自一个包含数千个 NSNumber 的数组(核心数据)。现在,这是我使用的代码:

//viewController.m
@implementation viewController{
    NSArray *array;
    UIScrollView *scrollView;
    graphView *embedGraphView; //graphView is a subclass of UIView
}

- (void)viewDidLoad{
    array = @[...]; //thousand of NSNumber from Core Data Model

    //Auto layout
    scrollView = [[UIScrollView alloc] init];
    scrollView.delegate = self;
    scrollView.alwaysBounceHorizontal = YES;
    [self.view addSubview:scrollView];
    scrollView.translatesAutoresizingMaskIntoConstraints = NO;

    embedGraphView = [[graphView alloc] init];
    [embedGraphView setBackgroundColor:[UIColor whiteColor]];
    embedGraphView.array = array;
    embedGraphView.maxValue = [[array valueForKeyPath:@"@max.doubleValue"]doubleValue];
    [scrollView addSubview:embedGraphView];
    embedGraphView.translatesAutoresizingMaskIntoConstraints = NO;

    NSDictionary *metrics = @{@"width":@(array.count)};
    NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(scrollView, embedGraphView);

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[scrollView]|" options:0 metrics:0 views:viewsDictionary]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[scrollView]|" options:0 metrics:0 views:viewsDictionary]];
    [scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[embedGraphView(width)]|" options:0 metrics:metrics views:viewsDictionary]];
    [scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[embedGraphView(scrollView)]|" options:0 metrics:0 views:viewsDictionary]];
}

//graphView.h
@interface graphView : UIView
@property (strong, nonatomic) NSArray *array;
@property double maxValue;

//graphView.m
@implementation graphView
- (void)drawRect:(CGRect)rect{
    CGContextRef ctx = UIGraphicsGetCurrentContext();  
    CGContextSetLineWidth(ctx, 0.5);
    CGContextSetStrokeColorWithColor(ctx, [[UIColor blueColor] CGColor]);
    CGContextMoveToPoint(ctx, 0, self.frame.size.height - [self.array[0]doubleValue] / self.maxValue * (self.frame.size.height-10));

    if ((float)self.array.count == self.frame.size.width){
        for (NSInteger i = 1; i < self.array.count; i++)
        {
            CGContextAddLineToPoint(ctx, i, self.frame.size.height - [self.array[i]doubleValue] / self.maxValue * (self.frame.size.height-10));
        }
    }
    CGContextDrawPath(ctx, kCGPathStroke);
}

我遇到的问题是,由于 drawRect: 方法中的 for 循环,我的视图控制器需要几秒钟才能加载。我不想使用viewDidAppear 方法来显示 embedGraphView,因为我可能还需要在 iPhone 旋转时重新绘制它。为了解决这个问题,有哪些解决方案?我可以使用 GCD,图像缓存吗?怎么做?

谢谢。

【问题讨论】:

  • 你有没有考虑过使用类似code.google.com/p/core-plot
  • 您是否尝试过更改代码生成设置? “自动矢量化器”应该会有所帮助。

标签: ios for-loop drawrect cgcontext


【解决方案1】:

每次调用drawRect: 时,您都会绘制整个图表。只绘制在rect传入的范围内的部分。

【讨论】:

  • 我更改了graphView.m 中的代码,以便仅绘制屏幕范围内的图形的一部分。为了更新我的平局,我在viewController.mscrollViewDidScroll: 方法中添加了[embedGraphView setNeedsDisplay];。然而,结果并不完美:视图控制器加载速度很快,但图形的滚动现在很慢......
猜你喜欢
  • 2011-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-18
  • 2021-07-30
  • 1970-01-01
  • 2022-10-20
  • 2013-11-05
相关资源
最近更新 更多