【问题标题】:Implementing a volume graph on iOS在 iOS 上实现体积图
【发布时间】:2014-08-24 01:00:00
【问题描述】:

我正在尝试实现这样的图形(UIView):

我目前的方法是在 drawRect 内使用 UIBezierPath 绘制圆角矩形,将指向每个路径的指针存储在私有 NSMutableArray 和我的视图控制器发送消息以使用新值更新图形。 (https://gist.github.com/gverri/f238ad17f90013bfc832)

但不知何故,我无法向我的视图发送消息来更改路径的颜色。

.

我仍然是一个相对初学者,我确信我正在尝试以某种方式重新发明轮子。或者只是遗漏了一些明显的东西。

我是否应该在每次更新(1s ~ 2s)时重新绘制图表?!我应该使用特殊的框架来执行此操作吗?

编辑:

经过一番挖掘,我发现我无法保存指向路径的指针。在drawRect 之后,我的指针从数组中消失了。

看来我需要使用 Core Graphics 才能使用这种方法。

有没有更简单的选择?

【问题讨论】:

  • 您想如何发送该消息?显示您尝试过的代码。
  • @rdelmar 这是代码:gist.github.com/gverri/f238ad17f90013bfc832 我需要再次调用 drawRect 吗?
  • 我在该代码中看不到任何将颜色设置为较暗颜色的内容。我不确定这种结构是最好的方法,但这取决于您用来为单元格着色的数据的结构方式。我想我会让每一列成为一个包含 7 个子视图(圆角矩形)的自定义视图,并使用 setNumberOfDarkViews:(NSInteger) num 之类的方法设置这些子视图的背景颜色。然后你可以让自定义视图为它自己的子视图着色。
  • 那不是很贵吗?我们说的是 150 多个子视图每 1~2 秒改变一次背景颜色。
  • 我做了一个小测试应用程序,它似乎可以每 0.2 秒更新一次值(我传递了随机数来表示每列中较暗单元格的数量)。您必须在仪器中分析两种方式,以查看哪种方式更快。

标签: ios objective-c cocoa-touch uiview core-graphics


【解决方案1】:

我制作了一个测试应用程序,它使用带有 7 个圆形矩形子视图的列视图来实现图形。这是 ColumnView 类(UIView 的子视图),

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        NSMutableArray *temp = [NSMutableArray new];
        _offColor = [UIColor colorWithRed:238/255.0 green:230/255.0 blue:238/255.0 alpha:1];
        _onColor = [UIColor colorWithRed:154/255.0 green:102/255.0 blue:155/255.0 alpha:1];
        for (int i=0; i<7; i++) {
            UIView *roundRect = [[UIView alloc] initWithFrame:CGRectMake(0, i*34, frame.size.width -2, (frame.size.height/7) - 4)];
            roundRect.backgroundColor = _offColor;
            roundRect.layer.cornerRadius = 3;
            [temp insertObject:roundRect atIndex:0];
            [self addSubview:roundRect];
        }
        _columns = [NSArray arrayWithArray:temp];
    }
    return self;
}


-(void)setNumberOfDarkRects:(NSInteger) num {
    [self.columns enumerateObjectsUsingBlock:^(UIView *sub, NSUInteger idx, BOOL *stop) {
        if (idx + 1 <= num) {
            sub.backgroundColor = self.onColor;
        }else{
            sub.backgroundColor = self.offColor;
        }

    }];
}

这就是我创建网格并测试更新列的方式,

@interface ViewController ()
@property (strong,nonatomic) NSMutableArray *columns;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.columns = [NSMutableArray new];
    for (int i=0; i<21; i++) {
       ColumnView *column = [[ColumnView alloc] initWithFrame:CGRectMake(i * 22 + 10, 50, 22, 240)];
        [self.view addSubview:column];
        [self.columns addObject:column];
    }
    [self performSelector:@selector(doRandomColoring) withObject:nil afterDelay:2];
}


-(void)doRandomColoring {
    for (ColumnView *col in self.columns) {
        [col setNumberOfDarkRects:arc4random_uniform(8)];
    }
    [self performSelector:@selector(doRandomColoring) withObject:nil afterDelay:.2];
}

【讨论】:

  • 谢谢,它似乎工作。我稍后会尝试对其进行分析。
猜你喜欢
  • 1970-01-01
  • 2013-05-14
  • 2013-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多