fla*_*ghi 3 iphone position ipad ios
我在UIScrollView中有很多按钮,我试图在按钮点击上获得他们的位置.这是我的代码:
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];
Run Code Online (Sandbox Code Playgroud)
和
-(void) buttonClick:(UIButton*)button{
if ([button tag]==1)
{
NSLog(@"position:%d",buton1.frame.origin.x);
}
else
{
NSLog(@"position:%d",buton2.frame.origin.y);
}
Run Code Online (Sandbox Code Playgroud)
}
我的代码有什么问题.谢谢.
我也尝试过:
-(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);
}
Run Code Online (Sandbox Code Playgroud)
但我得到的相同.位置0;
你的代码看起来很好.唯一的 - 你在NSLog中使用了错误的格式说明符.由于坐标是浮点数,您应该使用%f而不是%d:
NSLog(@"position:%f",buton1.frame.origin.x);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11911 次 |
| 最近记录: |