Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在iOS上实现体积图_Ios_Objective C_Cocoa Touch_Uiview_Core Graphics - Fatal编程技术网

在iOS上实现体积图

在iOS上实现体积图,ios,objective-c,cocoa-touch,uiview,core-graphics,Ios,Objective C,Cocoa Touch,Uiview,Core Graphics,我正在尝试实现如下图形(UIView): 我目前的方法是在drawRect中使用UIBezierPath绘制圆角矩形,将指向每个路径的指针存储在私有NSMutableArray中,并在我的视图控制器中发送消息以用新值更新图形。() 但不知何故,我无法向视图发送更改路径颜色的消息 我仍然是一个相对的初学者,我确信我正试图以某种方式重新发明轮子。或者只是遗漏了一些明显的东西 我是否应该在每次更新(1s~2s)时重新绘制图表?!我应该使用一个特殊的框架来做这件事吗 编辑: 经过一点挖掘,我发现我无

我正在尝试实现如下图形(UIView):

我目前的方法是在drawRect中使用UIBezierPath绘制圆角矩形,将指向每个路径的指针存储在私有NSMutableArray中,并在我的视图控制器中发送消息以用新值更新图形。()

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

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

我是否应该在每次更新(1s~2s)时重新绘制图表?!我应该使用一个特殊的框架来做这件事吗

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

看来我需要使用核心图形来实现这种方法


有更简单的替代方案吗?

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

-(id)initWithFrame:(CGRect)frame{
if(self=[super initWithFrame:frame]){
NSMUTABLEARRY*temp=[NSMUTABLEARRY new];
_offColor=[UIColor color withred:238/255.0绿色:230/255.0蓝色:238/255.0 alpha:1];
_onColor=[UIColor COLOR WITH RED:154/255.0绿色:102/255.0蓝色:155/255.0 alpha:1];

对于(int i=0;i您试图如何发送该消息?显示您尝试过的代码。@rdelmar代码如下:我是否需要再次调用drawRect?我在该代码中没有看到任何将颜色设置为较暗颜色的内容。我不确定此结构是否是最佳方式,但这取决于您用于为单元格着色的数据的结构。我想知道k我将使每个列成为一个具有7个子视图(圆角矩形)的单个自定义视图,并使用setNumberOfDarkViews:(NSInteger)之类的方法设置这些子视图的背景色num。然后你可以让自定义视图为自己的子视图着色。这不是很昂贵吗?我们说的是150多个子视图每1~2秒更改背景颜色。我制作了一个小测试应用程序,它似乎可以每0.2秒更新一次值(我在传递每列中较暗单元格的随机数)。您必须在仪器中分析两种方式,以查看哪种方式更快。谢谢,它似乎有效。我稍后将尝试分析它。
- (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];
}