前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SwiftUI-GeometryReader

SwiftUI-GeometryReader

作者头像
YungFan
发布2020-07-20 09:53:46
1.9K0
发布2020-07-20 09:53:46
举报
文章被收录于专栏:学海无涯学海无涯

介绍

  • 官方解释:A container view that defines its content as a function of its own size and coordinate space.一个容器View,根据其自身大小和坐标定义其内容。
  • 解读
    • 本身也是一个容器类型的View
    • 可以自己决定内容的大小位置
  • 定义
代码语言:javascript
复制
public struct GeometryReader<Content>: View where Content: View {
    public var content: (GeometryProxy) -> Content
    
    @inlinable public init(@ViewBuilder content: @escaping (GeometryProxy) -> Content) 

    public typealias Body = Never
}

GeometryProxy

GeometryReader中有一个GeometryProxy,它也是一个结构体,定义如下:

代码语言:javascript
复制
public struct GeometryProxy {

    public var size: CGSize { get }

    public subscript<T>(anchor: Anchor<T>) -> T { get }

    public var safeAreaInsets: EdgeInsets { get }

    public func frame(in coordinateSpace: CoordinateSpace) -> CGRect
}

通过GeometryProxy可以拿到父容器布局的相关信息,这个是它能做到自己决定内容的大小与位置的核心,主要作用如下:

  1. 可以通过size: CGSize获取宽和高。
  2. 可以通过safeAreaInsets获取安全区域相关的信息。
  3. 可以通过frame()获取屏幕坐标系的 x 和 y 坐标。
代码语言:javascript
复制
struct ContentView: View {
    
    var body: some View {
        ScrollView {
            ZStack {
                VStack {
                    RoundedRectangle(cornerRadius: 20)
                        .foregroundColor(Color.green)
                        .frame(width: 300, height: 300)
                    
                    RoundedRectangle(cornerRadius: 20)
                        .foregroundColor(Color.red)
                        .frame(width: 300, height: 300)
                    
                    RoundedRectangle(cornerRadius: 20)
                        .foregroundColor(Color.purple)
                        .frame(width: 300, height: 300)
                    
                }
                
                GeometryReader { geometry in
                    VStack {
                        // 尺寸
                        Text("Geometry width: \(geometry.size.width)")
                        Text("Geometry height: \(geometry.size.height)")
                        
                        // 坐标
                        Text("Geometry X: \(geometry.frame(in: .global).origin.x)")
                        Text("Geometry Y: \(geometry.frame(in: .global).origin.y)")
                        Text("Geometry minX: \(geometry.frame(in: .global).minX)")
                        Text("Geometry maxX: \(geometry.frame(in: .global).maxX)")
                        
                        // 安全区域
                        Text("Geometry safeAreaInsets.bottom: \(geometry.safeAreaInsets.bottom)")
                        Text("Geometry safeAreaInsets.top: \(geometry.safeAreaInsets.top)")
                    }
                }
            }
        }.edgesIgnoringSafeArea(.all) // 观察有无此句的内容变化
    }
}

案例一

GeometryReader的基本用法是读取父 View 建议的大小,然后使用它来布局我们的 View。 例如使用GeometryReader做一个自适应比例的彩色矩形。

代码语言:javascript
复制
struct FlowRectangle: View {
    
    var body: some View {
        // 这里用的全部是比例,尺寸是在外面初始化的时候设置的
        GeometryReader { proxy in
            VStack(spacing: 0) {
                Rectangle()
                    .fill(Color.red)
                    .frame(height: 0.5 * proxy.size.height)
                
                HStack(spacing: 0) {
                    Rectangle()
                        .fill(Color.green)
                        .frame(width: 0.5 * proxy.size.width)
                    
                    VStack(spacing: 0) {
                        Rectangle()
                            .fill(Color.blue)
                            .frame(height: 0.25 * proxy.size.height)
                        
                        Rectangle()
                            .fill(Color.yellow)
                            .frame(height: 0.25 * proxy.size.height)
                    }
                    .frame(width: 0.5 * proxy.size.width)
                }
            }
        }
    }
}

struct ContentView: View {
    
    let screenRect = UIScreen.main.bounds
    
    var body: some View {
        VStack {
            // 对比一下3种方式的效果
            FlowRectangle()
                .frame(width: 100, height: 200)
          
            FlowRectangle()
                .frame(width: 200, height: 100)
            
            FlowRectangle()
                .frame(width: screenRect.width, height: screenRect.width)
        }
    }
}

案例二

在一个VStack中垂直摆放两个元素TextDIYRectangle,其中DIYRectangle的大小为父容器VStack显示完Text后剩余空间的1/3。

代码语言:javascript
复制
struct ContentView: View {
    
    var body: some View {        
        VStack {
            Text("下面是一个矩形")
            
            DIYRectangle()
            
        }.frame(width: 150, height: 100)
         .border(Color.black)
    }
}

struct DIYRectangle: View {
    
    var body: some View {  
        // geometry的类型为GeometryProxy
        GeometryReader { geometry in
            Rectangle()
                .foregroundColor(Color.blue)
                .frame(width: geometry.size.width * 0.3, height: geometry.size.height * 0.3, alignment: .center)
        }      
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 介绍
  • GeometryProxy
  • 案例一
  • 案例二
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档