我在UIScrollView中有很多按鈕,我試圖在按鈕單擊時獲得它們的位置。這裏是我的代碼:IOS獲取位置
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);
}
}
有什麼不對我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);
}
但我剛剛得到的same.Position 0;
請寫下你的期望和發生了什麼,如果你執行此代碼。 – GorillaPatch 2011-03-25 10:18:57
我預計將返回的位置,我得到的是位置0. – flaviusilaghi 2011-03-25 10:20:44