如何从按钮事件获取自定义按钮坐标?

时间:2014-07-07 13:46:55

标签: xamarin.ios

我有方法,在我的坐标上创建按钮。

private void DrawButtonsOnGraphics (List<Tuple<float, float>> listOfData, UIColor colorOfGraph, float radius)
        {
            foreach (var item in listOfData) {
                UIButton button = new UIButton (UIButtonType.Custom);
                button.BackgroundColor = UIColor.Black;
                button.Frame = new RectangleF (item.Item1 - radius / 2, item.Item2 - radius / 2, radius, radius);
                button.TouchUpInside += GraphicButtonClick;
                AddSubview (button);
            }
        }   

我有处理点击的事件。

void GraphicButtonClick (object sender, EventArgs e)
        {           
            if (_cliked != true) {
                view = new UIView(new RectangleF(item.Item1 - 23,item.Item2 - 25,45,23));
                labelText = new UILabel( new RectangleF(10,10,30,20));
                labelText.Text = ((225-item.Item2)/2).ToString();
                labelText.TextColor = UIColor.White;
                labelText.TextAlignment = UITextAlignment.Center;
                view.AddSubview(labelText);
                view.BackgroundColor = UIColor.Black;
                AddSubview(view);
            }
            else {
                view.RemoveFromSuperview();
            }
        }

我需要从我的按钮创建subview upper。在wpf我可以从EventArgs获得它。我怎么能在iOs,xamarin中做到这一点? Thnanks

1 个答案:

答案 0 :(得分:0)

将发件人转换为UIButton然后使用它的Frame属性来获取坐标

UIButton btn = ((UIButton)sender);
double x = btn.Frame.X;
couble y = btn.Frame.Y;