【问题标题】:Get x and y coordinate of UIButton [closed]获取 UIButton 的 x 和 y 坐标
【发布时间】:2012-09-17 05:09:47
【问题描述】:

我有一个名为“buttonPressed”的按钮。当我按下按钮时,我需要找到该按钮的 x 和 y 坐标。你有什么想法可以帮助我吗?

    UIButton *circleButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    circleButton.frame = rect;
    circleButton.layer.cornerRadius = 8.0f;
    [circleButton addTarget:self
                     action:@selector(buttonPressed:)
           forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:circleButton];

-(void)buttonPressed:(id)sender
{

}

回答

     - (IBAction)buttonPressed:(UIButton *)sender
     {
       CGPoint coordinates = sender.frame.origin;
       CGFloat x = coordinates.x;
       CGFloat y = coordinates.y;
     }

【问题讨论】:

  • 哪个坐标?你想把它们送到哪里?

标签: ios button uibutton tags coordinates


【解决方案1】:

听起来您希望 X 和 Y 离开动作的发送者。将 sender 参数附加到您的按钮点击方法并找到 sender 的框架。

- (IBAction)buttonTapped:(UIButton *)sender
{
    CGPoint coordinates = sender.frame.origin;
}

【讨论】:

  • 谢谢。问题是:(UIButton *)我有(id)。我没看到:/经验教训
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-13
  • 2015-05-23
  • 2022-11-13
  • 1970-01-01
  • 2021-07-10
相关资源
最近更新 更多