2012-09-02 5 views
1

버튼이 모두 UICellViews에 있습니다. 이 버튼을 누르면 일부 동작이 시작됩니다. 상황은 지금까지 잘 작동했습니다.UITable 안에 UIButton 또는 UILabel의 위치를 ​​어떻게 얻을 수 있습니까?

이 단추 중 하나의 위치를 ​​화면의 보이는 영역에 비례하여 어떻게 얻을 수 있습니까? 이를 달성하는 방법에 대한 아이디어 (또는 자습서)가 있습니까?

+0

* "화면의 보이는 영역과 관련하여"*라고 말하면 창과의 상대적인 의미입니까? – sch

+0

yea 정확하게 내가 무엇을 의미하는지 –

답변

6

-convertRect:toView:UIView입니다. 그걸로 부족의 경우 (

CGRect frame = someView.frame; 
CGPoint topLeftCorner = frame.origin; 

:

- (void)loadView 
{ 
    [super loadView]; 

    self.view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame]; 

    UIView *parentView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 300, 300)]; 
    [self.view addSubview:parentView]; 

    UIView *childView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)]; 
    [parentView addSubview:childView]; 

    CGRect rect1 = [childView convertRect:childView.bounds toView:self.view]; 
    // 200, 200, 100, 100 => correct 

    // Other tests for the discussion of @H2CO3's answer. 

    CGRect rect2 = [childView.superview convertRect:childView.frame toView:self.view]; 
    // 200, 200, 100, 100 => correct 
    CGRect rect3 = [childView convertRect:childView.frame toView:self.view]; 
    // 300, 300, 100, 100 => wrong 
} 
+0

그래서 이걸 사용하여 내부에있는 tableview의 정확한 위치를 얻습니다. –

+0

@user - 아마도'fromView'가 아니라'toView'를 의미 할 것입니다. – sch

+0

그건 그렇고, 좌표는 음수입니다. 버튼이 위로 스크롤되어 원점이 위쪽 스크린 부분의 바깥 쪽이 아닐 경우 –

2

당신은 그것의 superview에 그것의 위치를 ​​얻을 수있는 뷰의 frame 속성을 사용할 수 있습니다 : 여기

CGRect frameInWindow = [button convertRect:button.bounds toView:viewController.view]; 

조금 테스트입니다 즉, 슈퍼 뷰뿐만 아니라 다른 뷰와 관련된 뷰의 프레임을 얻으려는 경우 다양한 변환 방법을 사용할 수 있습니다.

CGRect relativeFrame = [view convertRect:view.bounds toView:anotherView]; 

자세한 내용은 UIView class reference을 참조하십시오.

+0

빠른 답장을 보내 주셔서 감사합니다. 나는 그것을 바로 시도한다. –

+0

또한 그렇게해서는 안된다 :'[view.superView convertRect : view.frame toView : anotherView]; 또는 '[view convertRect : view.bounds toView : anotherView];'? – sch

+0

제 수정 된 답변을 참조하십시오. 도움을 받으려면 – sch

관련 문제