我有一个名为“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;
}
答案 0 :(得分:7)
听起来你想要X& Y关闭了动作的发送者。将发件人参数附加到按钮点按方法,然后找到发件人的框架。
- (IBAction)buttonTapped:(UIButton *)sender
{
CGPoint coordinates = sender.frame.origin;
}