美文网首页
iOS坐标转换(func convert(_ point: CG

iOS坐标转换(func convert(_ point: CG

作者: xiAo__Ju | 来源:发表于2017-01-16 17:14 被阅读0次

    结论

    // 把redView坐标系上的点greenViewOrigin转换为blueView的坐标系上的点为point
    let point = redView.convert(greenViewOrigin, to: blueView)

    // 把blueView坐标系上的点greenViewOrigin转换为redView的坐标系上的点为point1
    let point1 = redView.convert(greenViewOrigin, from: blueView)

    func convert(:to: ) -> CGPoint func convert(: from:) -> CGPoint
    view为redView的子视图 let result = point - view.frame.origin let result = point + view.frame.origin
    view不为redView的子视图 let result = point + redView.frame.origin - view.frame.origin let result = point - redView.frame.origin + view.frame.origin
    绿色视图和蓝色视图为红色视图的子视图
           // 把redView上的点greenViewOrigin相对blueView结果为
           let point = redView.convert(greenViewOrigin, to: blueView)
           print("point=\(point)")
    
            // 把blueView上的点greenViewOrigin相对redView结果为
           let point1 = redView.convert(greenViewOrigin, from: blueView)
           print("point1=\(point1)")
    
           let point2 = redView.convert(blueViewOrigin, to: greenView)
           print("point2=\(point2)")
    
           let point3 = redView.convert(blueViewOrigin, from: greenView)
           print("point3=\(point3)")
           
           print("\n\n\n\n\n")
           
           let rect = redView.convert(greenView.frame, to: blueView)
           print("rect=\(rect)")
           
           let rect1 = redView.convert(greenView.frame, from: blueView)
           print("rect1=\(rect1)")
    

    输出

    屏幕快照 2017-01-16 下午5.42.51.png
    func convert(_ rect: CGRect, to view: UIView?) -> CGRect
    同
    func convert(_ point: CGPoint, to view: UIView?) -> CGPoint
    原rect的宽高不变
    
    func convert(_ rect: CGRect, from view: UIView?) -> CGRect
    同
    func convert(_ point: CGPoint, from view: UIView?) -> CGPoint
    原rect的宽高不变
    

    注意

    以上方法中view为空时相对keywindow

    相关文章

      网友评论

          本文标题:iOS坐标转换(func convert(_ point: CG

          本文链接:https://www.haomeiwen.com/subject/ngrpbttx.html