识别 UIButton 的位置

发布于 2024-12-11 00:03:48 字数 729 浏览 0 评论 0原文

我动态创建了 5 个按钮,如下所示:

float xpos=0,ypos=0;
    for (int i = 0; i < 5; i++)
       {
            but = [UIButton buttonWithType:UIButtonTypeCustom];
            [but setTag:i];
            [but setImage:[UIImage imageNamed:@"btfrnt.png"] forState:UIControlStateNormal];
            [but setFrame:CGRectMake(xpos, ypos, 40, 40)];
             xpos+=80;
            [but addTarget:self action:@selector(checkboxButton:) forControlEvents:UIControlEventTouchUpInside];
            [self.view addSubview:but];
        }

在任何按钮的 Click 事件中,我想找到我单击的按钮的位置....

-(void)checkboxButton:(UIButton*)sender
{
  NSLog(@"%d",xpos);
}

但是,它只显示最后一个按钮的 xpos.. .有什么方法可以通过标签来识别它吗?

I've created 5 buttons dynamically, as follows:

float xpos=0,ypos=0;
    for (int i = 0; i < 5; i++)
       {
            but = [UIButton buttonWithType:UIButtonTypeCustom];
            [but setTag:i];
            [but setImage:[UIImage imageNamed:@"btfrnt.png"] forState:UIControlStateNormal];
            [but setFrame:CGRectMake(xpos, ypos, 40, 40)];
             xpos+=80;
            [but addTarget:self action:@selector(checkboxButton:) forControlEvents:UIControlEventTouchUpInside];
            [self.view addSubview:but];
        }

In Click event of any of the button, I want to find the position of the button which I have clicked....

-(void)checkboxButton:(UIButton*)sender
{
  NSLog(@"%d",xpos);
}

But, it displays only the xpos of the last button...Is there any way to identify it by means of its tag?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

彩虹直至黑白 2024-12-18 00:03:48

试试这个:

-(void)checkboxButton:(UIButton*)sender
{
  NSLog(@"%f  %f",sender.frame.origin.x,sender.frame.origin.y);
}

try this :

-(void)checkboxButton:(UIButton*)sender
{
  NSLog(@"%f  %f",sender.frame.origin.x,sender.frame.origin.y);
}
日久见人心 2024-12-18 00:03:48

我认为 xPos 是一个全局变量 - 当然它包含最后一个按钮的值,这是它最后设置的值。

对于按钮位置,您不需要在全局变量中存储任何内容 - 只需从单击事件处理程序中传递给您的发送者对象获取它 - 它是一个 UIButton 指针,并且 UIButton 对象具有一个框架结构顺便说一下,origin.x 和 origin.y,以及 size.width 和 size.height。

I take it xPos is a global variable - of course it contains the value of the last button, that's the value it was set to last.

For the button position you don't need to store anything in a global variable - just get it from the sender object that is delivered to you in the click event handler - it is a UIButton pointer, and the UIButton object has a frame structure with origin.x and origin.y, as well as a size.width and size.height, by the way.

比忠 2024-12-18 00:03:48

你可以试试这个...

-(void)checkboxButton:(UIButton*)sender {
  UIButton *button = (UIButton *)sender;
  CGRect rect = button.frame;        //You may use sender.frame directly
  NSLog(@"X: %d", rect.origin.x);
  NSLog(@"Y: %d", rect.origin.y);
  NSLog(@"Width: %f", rect.size.width);
  NSLog(@"Height: %f", rect.size.height);
}

You may try this...

-(void)checkboxButton:(UIButton*)sender {
  UIButton *button = (UIButton *)sender;
  CGRect rect = button.frame;        //You may use sender.frame directly
  NSLog(@"X: %d", rect.origin.x);
  NSLog(@"Y: %d", rect.origin.y);
  NSLog(@"Width: %f", rect.size.width);
  NSLog(@"Height: %f", rect.size.height);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文