简体   繁体   中英

How to get button x,y coordinate in a textfield

I have 4 different buttons for every corner of the screen. Just need to show their coordinates for every device changes in a text field or a label.

“x0” - button x coordinate on the screen “y0” - button y coordinate on the screen

i should show like x = "x0" ; y = "y0"

There is an easy built-in way to log frames. Use NSStringFromCGRect(frame); to show the coordinates of the button.

Although you can directly print a CGRect using NSStringFromCGRect , here is a sample of what you are looking for. I'm just putting button1's coordinates in text field.

UIButton *button1, *button2, *button3, *button4;

CGFloat button1X = button1.frame.origin.x;
CGFloat button1Y = button1.frame.origin.y;

CGFloat button2X = button2.frame.origin.x;
CGFloat button2Y = button2.frame.origin.y;

CGFloat button3X = button3.frame.origin.x;
CGFloat button3Y = button3.frame.origin.y;

CGFloat button4X = button4.frame.origin.x;
CGFloat button4Y = button4.frame.origin.y;

NSString *button1Coords = [NSString stringWithFormat:@"x = \"%f\" x = \"%f\"", button1X, button1Y];

self.textField.text = button1Coords;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM