-3

我有一个名为“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;
     }
4

1 回答 1

9

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

- (IBAction)buttonTapped:(UIButton *)sender
{
    CGPoint coordinates = sender.frame.origin;
}
于 2012-09-25T16:35:30.997 回答