SwiftUI-基础-1

2020-10-21  本文已影响0人  丿小七

参考:SwiftUI 教程

基础用法

Text

Customize the Text View 自定义TextView
VCtack:竖向布局
HStack:横向布局
Spacer:空白

    VStack(alignment: .leading) {
        Text("Turtle Rock")
            .font(.title)
        HStack {
            Text("Joshua Tree National Park")
                .font(.subheadline)
            Spacer()
            Text("California")
                .font(.subheadline)
        }
    }
    .padding()
截屏2020-10-21 15.37.56.png

Image

Create a Custom Image View 图片
clipShape 切割方式

    var body: some View {
        
        VStack {
            Image("timg-2")
            Text("上图:原图")
            Spacer()
            Text("下图:圆形切割")
            Image("timg-2")
                .clipShape(Circle())
                .overlay(Circle().stroke(Color.white, lineWidth: 8))
                .shadow(radius: 20)
        }
        .padding(.vertical, 90.0)
    }
截屏2020-10-21 18.00.48.png

Use UIKit and SwiftUI Views Together 将UIKit与SwiftUI结合使用

UIViewRepresentable

struct MapView: UIViewRepresentable {
    func makeUIView(context: Context) -> MKMapView {
        MKMapView(frame: .zero)
    }
    
    func updateUIView(_ uiView: MKMapView, context: Context) {
        let coordinate = CLLocationCoordinate2D(latitude: 22.011286, longitude: 112.166868)
        let span = MKCoordinateSpan(latitudeDelta: 2.0, longitudeDelta: 2.0)
        let region = MKCoordinateRegion(center: coordinate, span: span)
        uiView.setRegion(region, animated: true)
    }
}

Compose the Detail View 将独立的SwiftUI组合在一起

    var body: some View {
        VStack {
            MapView()
                .frame(height: 300)
                .edgesIgnoringSafeArea(.top)
            
            CircleImage()
                .offset(y: -100)
                .padding(.bottom, -100)
            
            VStack(alignment: .leading) {
                Text("Turtle Rock")
                    .font(.title)
                
                HStack {
                    Text("Joshua Tree National Park")
                        .font(.subheadline)
                    Spacer()
                    Text("California")
                        .font(.subheadline)
                }
            }
            .padding()
            
            Spacer()
        }
    }
截屏2020-10-21 20.49.53.png
上一篇 下一篇

猜你喜欢

热点阅读