2011-03-25 3 views
3

나는 UIScrollView 안쪽에 많은 단추가 있고 단추 누르기에 그들의 위치를 ​​얻는 것을 시도하고있다. 여기 내 코드입니다 : 내 code.Thanks 뭐가 문제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); 
} 

}

.

는 또한 시도했다 :

-(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을 얻을;

+1

이 코드를 실행하면 예상 한 것과 발생한 결과를 적어주십시오. – GorillaPatch

+0

나는 그 위치를 되 찾을 것으로 예상하고 내가 얻는 것은 모두 0 위입니다. – flaviusilaghi

답변

7

코드가 정상적으로 보입니다. 유일하게 - NSLog에서 잘못된 형식 지정자를 사용합니다. 좌표가 부동 소수점되기 때문에 당신은 %d 대신 %f를 사용한다 : 당신이 scrollRectToVisible:animated: 방법을 사용하는 경우

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

mg. 많이 감사합니다. 효과가있었습니다. – flaviusilaghi

+0

이제 다른 문제가 있습니다. 버튼을 스크롤해도 위치는 바뀌지 않습니다. 왜 그럴까요? – flaviusilaghi

+1

버튼 위치가 scrollview 내용의 좌표 공간에 있기 때문에 스크롤 할 때 실제로 변경되지 않습니다. 화면 좌표를 변환하려는 경우 (예 : convertPoint : toView : 메소드 사용) – Vladimir

0

이 트릭이 문서는 말한다 때문에이 방법 스크롤 컨텐츠보기를되도록하는 구형에 의해 정의 된 영역 스크롤보기 안에서 볼 수 있습니다. 영역이 벌써 표시되고있는 경우, 메서드는 아무것도 실시하지 않습니다.

CGRectMake(buton1.frame.origin.x, buton1.frame.origin.y, yourScrollView.frame.width, yourScrollView.frame.height)으로 스크롤하십시오.

세 번째 매개 변수에는 퍼지 인수를 사용할 수 있습니다.

관련 문제