0

I am trying to programmatically create buttons for a interactive element that is part of my app. The interface looks like a doughnut chart, and if the user touches one of the regions in the doughnut chart, it will highlight that region and all of the others. I am not so concerned right now with the colors, I am trying to get the seven buttons to appear. Here is a code sample of how I am trying to create the buttons:

var radius = (Frame.Width - 38) / 2.0f;
var innerRadius = radius - 32;
var diameter = radius * 2;

energyCircleView = new UIView();
energyCircleView.BackgroundColor = UIColor.White;
energyCircleView.Frame = new RectangleF(19, confirmEntryButton.Frame.Top - 16 - (diameter), diameter, diameter);
energyCircleView.Layer.CornerRadius = radius;
energyCircleView.Layer.MasksToBounds = true;

var center = new PointF(energyCircleView.Frame.X + radius, energyCircleView.Frame.Y + radius);
var ninety = MathHelper.TwoPi * (90.0f / 360.0f);
var seventh = MathHelper.TwoPi * (1.0f / 7.0f) * 360.0f;

for (int i = 0; i < 7; i++)
{
    var startAngle = ninety + (i * seventh);
    var endAngle = ninety + ((i + 1) * seventh);

    var shapePath = new CGPath();
    shapePath.AddArc(center.X, center.Y, radius, startAngle, endAngle, false);
    shapePath.AddArc(center.X, center.Y, innerRadius, endAngle, startAngle, true);

    var shapeLayer = new CAShapeLayer();
    shapeLayer.Path = shapePath;

    var button = new UIButton();
    button.Frame = new RectangleF(0, 0, diameter, diameter);
    button.BackgroundColor = MyColors[i];
    button.Layer.Mask = shapeLayer;
    button.Layer.MasksToBounds = true;

    energyButtons.Add(button);
}

foreach (UIButton button in energyButtons)
{
    energyCircleView.AddSubview(button);
}
4

1 回答 1

1

您正在添加按钮作为视图的子视图。但是您不显示视图本身。

添加以下代码以显示energyCircleView:

this.View.AddSubview(energyCircleView);
于 2013-09-06T05:51:21.157 回答