Programing

수퍼 뷰의 수퍼 뷰와 관련하여 UIView의 위치를 ​​가져옵니다.

lottogame 2020. 10. 18. 08:23
반응형

수퍼 뷰의 수퍼 뷰와 관련하여 UIView의 위치를 ​​가져옵니다.


UIButtons를 정렬 한 UIView가 있습니다. 해당 UIButton의 위치를 ​​찾고 싶습니다.

나는 buttons.frame그것이 나에게 위치를 줄 것이라는 것을 알고 있지만 그것은 즉각적인 감독에 대해서만 나에게 위치를 줄 것입니다.

UIButtons superview의 superview와 관련하여 해당 버튼의 위치를 ​​찾을 수있는 방법이 있습니까?

예를 들어 "firstView"라는 이름의 UIView가 있다고 가정합니다.

그런 다음 다른 UIView, "secondView"가 있습니다. 이 "SecondView"는 "firstView"의 하위보기입니다.

그런 다음 "secondView"에 대한 하위보기로 UIButton이 있습니다.

->UIViewController.view
--->FirstView A
------->SecondView B
------------>Button

이제 "firstView"와 관련하여 해당 UIButton의 위치를 ​​찾을 수있는 방법이 있습니까?


이것을 사용할 수 있습니다 :

목표 -C

CGRect frame = [firstView convertRect:buttons.frame fromView:secondView];

빠른

let frame = firstView.convert(buttons.frame, from:secondView)

문서 참조 :

https://developer.apple.com/documentation/uikit/uiview/1622498-convert


요청한대로 계층 구조의 버튼에 국한되지는 않지만 시각화하고 이해하기가 더 쉽다는 것을 알았습니다.

여기에서 : 원본 소스

여기에 이미지 설명 입력

ObjC :

CGPoint point = [subview1 convertPoint:subview2.frame.origin toView:viewController.view];

빠른:

let point = subview1.convert(subview2.frame.origin, to: viewControll.view)

Swift 3 업데이트

    if let frame = yourViewName.superview?.convert(yourViewName.frame, to: nil) {

        print(frame)
    }

여기에 이미지 설명 입력


프레임 : (X, Y, 너비, 높이).

따라서 너비와 높이는 슈퍼 슈퍼 뷰에서도 변경되지 않습니다. 다음과 같이 X, Y를 쉽게 얻을 수 있습니다.

X = button.frame.origin.x + [button superview].frame.origin.x;
Y = button.frame.origin.y + [button superview].frame.origin.y;

다음과 같이 쉽게 슈퍼 슈퍼 뷰를 얻을 수 있습니다.

secondView-> [버튼 수퍼 뷰]
firstView-> [[버튼 수퍼 뷰] 수퍼 뷰]

그러면 버튼의 위치를 ​​알 수 있습니다.

이것을 사용할 수 있습니다 :

xPosition = [[button superview] superview].frame.origin.x;
yPosition = [[button superview] superview].frame.origin.y;

여러 뷰가 쌓여 있고 관심있는 뷰 사이에 가능한 뷰를 알 필요가 없거나 알 필요가없는 경우 다음을 수행 할 수 있습니다.

static func getConvertedPoint(_ targetView: UIView, baseView: UIView)->CGPoint{
    var pnt = targetView.frame.origin
    if nil == targetView.superview{
        return pnt
    }
    var superView = targetView.superview
    while superView != baseView{
        pnt = superView!.convert(pnt, to: superView!.superview)
        if nil == superView!.superview{
            break
        }else{
            superView = superView!.superview
        }
    }
    return superView!.convert(pnt, to: baseView)
}

where targetView would be the Button, baseView would be the ViewController.view.
What this function is trying to do is the following:
If targetView has no super view, it's current coordinate is returned.
If targetView's super view is not the baseView (i.e. there are other views between the button and viewcontroller.view), a converted coordinate is retrieved and passed to the next superview.
It continues to do the same through the stack of views moving towards the baseView.
Once it reaches the baseView, it does one last conversion and returns it.
Note: it doesn't handle a situation where targetView is positioned under the baseView.


UIView extension for converting subview's frame (inspired by @Rexb answer).

extension UIView {

    // there can be other views between `subview` and `self`
    func getConvertedFrame(fromSubview subview: UIView) -> CGRect? {
        // check if `subview` is a subview of self
        guard subview.isDescendant(of: self) else {
            return nil
        }

        var frame = subview.frame
        if subview.superview == nil {
            return frame
        }

        var superview = subview.superview
        while superview != self {
            frame = superview!.convert(frame, to: superview!.superview)
            if superview!.superview == nil {
                break
            } else {
                superview = superview!.superview
            }
        }

        return superview!.convert(frame, to: self)
    }

}

// usage:
let frame = firstView.getConvertedFrame(fromSubview: buttons.frame)

참고 URL : https://stackoverflow.com/questions/17572304/get-position-of-uiview-in-respect-to-its-superviews-superview

반응형