IOS得到了位置 [英] IOS get position

查看:117
本文介绍了IOS得到了位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在UIScrollView中有很多按钮,我试图在按钮点击时获得他们的位置。这是我的代码:

I have a lot of button inside a UIScrollView and i am trying to get their position on button click. Here is my code:

UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(2, 0, 262, 748)];
scrollView.contentSize=CGSizeMake(262, 816);
scrollView.bounces=NO;
scrollView.delaysContentTouches=YES;

buton1 = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
buton1.frame = CGRectMake(2, 0, 262, 102);
buton1.bounds = CGRectMake(2, 0, 262.0, 102.0);
[buton1 setTag:1];
[buton1 setImage:[UIImage imageNamed:@"left_gride.png"] forState:UIControlStateNormal];
[buton1 addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
     [scrollView addSubview:buton1];

-(void) buttonClick:(UIButton*)button{
if ([button tag]==1)
{
     NSLog(@"position:%d",buton1.frame.origin.x);
}
else
{
    NSLog(@"position:%d",buton2.frame.origin.y);
}

}

我的代码有什么问题。谢谢。

What is wrong with my code.Thanks.

我也尝试过:

-(void) buttonClick:(UIButton*)button{
if ([button tag]==1)
{
     NSLog(@"position:%d",buton1.frame.origin.x);
}
else
{
    NSLog(@"position:%d",buton2.frame.origin.y);
}

-(void) buttonClick:(UIButton*)button{
if ([button tag]==1)
{
     NSLog(@"position:%d",button.frame.origin.x);
}
else
{
    NSLog(@"position:%d",button2.frame.origin.y);
}

但我得到的相同。位置0;

But i just get the same.Position 0;

推荐答案

您的代码看起来很好。唯一的 - 你在NSLog中使用了错误的格式说明符。由于坐标是浮点数,您应该使用%f 而不是%d

Your code looks fine. The only thing - you use wrong format specifier in NSLog. As coordinates are floating point numbers you should use %f instead of %d:

NSLog(@"position:%f",buton1.frame.origin.x);

这篇关于IOS得到了位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆