我正在使用Objective C语言的VBPieChart。我正在显示分段控件的valueChange上的图表,例如:

- (IBAction)segmentControlAction:(UISegmentedControl *)sender
    {
    switch ([sender selectedSegmentIndex])
    {
        case 0:

            [self showPieChart:NO];
            _tableViewForCount.hidden = NO;
            [_tableViewForCount reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationRight];

            break;
        case 1:

            _tableViewForCount.hidden = YES;

            [self showPieChart:YES];

            break;
        default:
            NSLog(@"None");
            break;
    }
}


showPieChart方法如下:

- (void) showPieChart:(Boolean)visible
{
    VBPieChart *chart = [[VBPieChart alloc] initWithFrame:CGRectMake(0, 0, 250, 250)];
    chart.center = self.view.center;

    // Setup some options:
    [chart setHoleRadiusPrecent:0.3]; /* hole inside of chart */

    // Prepare your data
    NSMutableArray *chartValues = [[NSMutableArray alloc]initWithObjects:                             @{@"name":@"Apples", @"value":@50, @"color":[UIColor redColor]},
                             @{@"name":@"Pears", @"value":@20, @"color":[UIColor blueColor]},
                             @{@"name":@"Oranges", @"value":@40, @"color":[UIColor orangeColor]},
                             @{@"name":@"Bananas", @"value":@70, @"color":[UIColor purpleColor]}, nil
                             ];


    if (visible)
    {
        chart.center = self.view.center;
        // Present pie chart with animation
        [chart setChartValues:chartValues animation:YES duration:0.4 options:VBPieChartAnimationFan];
        [self.view addSubview:chart];
    }
    else
    {
        // remove chart
        [chart removeFromSuperView];
    }
}


由于VBPieChart只是UIView的自定义子类,因此应该可以使用。但是我没有任何成功。

我也尝试过[chart setHidden:YES];[chart setAlpha:0.0];,但还是没有运气。

任何帮助将不胜感激。我只想在用户切换回细分索引0时隐藏/删除图表。

谢谢。

最佳答案

VBPieChart * chart在.h文件上创建此对象,然后

- (void) showPieChart:(Boolean)visible
{
    if(chart == nil)
    {
        chart = [[VBPieChart alloc] initWithFrame:CGRectMake(0, 0, 250, 250)];
        [self.view addSubview:chart];
    }
    chart.center = self.view.center;

    // Setup some options:
    [chart setHoleRadiusPrecent:0.3]; /* hole inside of chart */

    // Prepare your data
    NSMutableArray *chartValues = [[NSMutableArray alloc]initWithObjects:                             @{@"name":@"Apples", @"value":@50, @"color":[UIColor redColor]},
                             @{@"name":@"Pears", @"value":@20, @"color":[UIColor blueColor]},
                             @{@"name":@"Oranges", @"value":@40, @"color":[UIColor orangeColor]},
                             @{@"name":@"Bananas", @"value":@70, @"color":[UIColor purpleColor]}, nil
                             ];


    if (visible)
    {
        chart.center = self.view.center;
        // Present pie chart with animation
        [chart setChartValues:chartValues animation:YES duration:0.4 options:VBPieChartAnimationFan];
        chart.alpa = 1.0
    }
    else
    {
        // remove chart
        chart.alpa = 0.0
    }
}

关于ios - VBPieChart-removeFromSuperView不起作用(隐藏图表),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43535292/

10-10 20:34