如何使用ios中的滑块更改条形图值

时间:2012-11-20 05:10:08

标签: ios slider uislider bar-chart

我在iOS中的条形图应用程序中遇到问题。我使用DSBarChart和滑块控件创建了一个条形图。我希望在更改滑块时动态更改图表。我怎么做?这是我到目前为止的示例代码。

- (void)sliderValueDidChange:(UISlider *)sender {
    if((int)slider.value % 10 == 0) {
        positionLabel.text = [NSString stringWithFormat:@"%d",(int)slider.value];
        myValue =  (int)slider.value;
    }

    NSDictionary *dict =[NSDictionary dictionaryWithObjectsAndKeys:
                         [NSNumber numberWithInt:10], @"0",
                         [NSNumber numberWithInt:20], @"1",
                         [NSNumber numberWithInt:15], @"2",
                         [NSNumber numberWithInt:25], @"3",
                         [NSNumber numberWithInt:30], @"4",
                         [NSNumber numberWithInt:20], @"5",
                         [NSNumber numberWithInt:15], @"6",
                         nil];

    DSBarChart *chrt = [[DSBarChart alloc] initWithFrame:ChartView.bounds
                        color:[UIColor greenColor]
                        andDictionary:dict];
    chrt.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    chrt.bounds = ChartView.bounds;
    [ChartView addSubview:chrt];
}

2 个答案:

答案 0 :(得分:0)

DSBarChart尚未动态绑定到控件!但有一种解决方法。触发滑块的值更改后,可以从视图中删除现有的DSBarchart实例,使用更改的值创建新图表并再次将其添加到视图中。目前,DSBarChart处于pre-alpha状态。它没有做太多。我会在找到时间时扩展功能。

答案 1 :(得分:0)

您可以做的一件事是在barChart上创建自定义视图。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

if (lineView) {
    [lineView removeFromSuperview];

    NSSet *allTouchesSet = [event allTouches]; 
    NSArray *allTouches = [NSArray arrayWithArray:[allTouchesSet allObjects]];   
    int count = [allTouches count];

    if (count  == 1) {

        UITouch *touch = [[event allTouches] anyObject];
        tapPoint = [touch locationInView:self];
        NSLog(@"tapPoint: %@",NSStringFromCGPoint(tapPoint));

        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(tapPoint.x, 0.0, 1, 320.0)];
        lineView.backgroundColor = [UIColor whiteColor];
        [parentView addSubview:lineView];


    }
}

}