1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PureSwiftUIによるコードスライス

Last updated at Posted at 2020-03-12

PureSwiftUIとは?

PureSwiftUI is a Swift package designed to enhance the experience of writing SwiftUI code. An additional goal is to make these changes as intuitive as possible, requiring little to no experience with the package to begin using its advantages in ernest.

SwiftUIコードの記述体験を向上させるために設計されたSwiftのパッケージ。最近筆者がよく使ってる。
PureSwiftUI github
作者twitter

SwiftUI vs PureSwiftUI 1

既存のSwiftUIでも十分なコーディングができるから大丈夫だと思う方、下記を見ればいかに、SwiftUIとPureSwiftUIの違いが劇的か理解できる。

//...

private let width: Double = 200
private let height: Int = 100
private let xOffset: Int = 10
private let yOffset: Double = 10
private let rotation: Int = 20
private let scale: Double = 1.1
private let opacity: Float = 0.9

//...

// native SwiftUI

Text("PureSwiftUI")
    .font(Font.title.bold())
    .foregroundColor(.white)
    .frame(width: CGFloat(width), height: height)
    .background(Color.red)
    .clipShape(Capsule())
    .overlay(Capsule().stroke(Color.black, lineWidth: 4))
    .rotationEffect(.degrees(Double(rotation)))
    .offset(x: CGFloat(xOffset), y: CGFloat(yOffset))
    .scaleEffect(CGFloat(scale))
    .opacity(Double(opacity))

// with PureSwiftUI

TitleText("PureSwiftUI", .white, .bold)
    .frame(width, height)
    .clipCapsuleWithStroke(.black, lineWidth: 4, fill: Color.red)
    .rotate(rotation.degrees)
    .offset(xOffset, yOffset)
    .scale(scale)
    .opacity(opacity)

// native SwiftUI
Image("my-image")
    .resizable()
    .scaledToFill() // or scaledToFit
    .frame(width: 200, height: 100)

// with PureSwiftUI
Image("my-image")
    .resizedToFill(200, 100) // or resizedToFit

SwiftUI vs PureSwiftUI 2

下記のようなViewを作成するとなれば、どれほどSwiftUIとPureSwiftUIは違うのか。

MihaRAM (@MiharaRAM) Tweeted:
https://t.co/dCebroi86m
https://twitter.com/MiharaRAM/status/1238205957282213888?s=20

PureSwiftUIでのソースコード

SwiftUIでのソースコード

import SwiftUI

private let boxSize = 300

struct ContentView: View {
    @State private var open = false
    var body: some View {
        VStack() {
            ZStack {
                Text("SwiftUI\nぴえん\n🥺")
                .font(Font.title.bold())
                .align(.center)
                Group {
                    VStack(spacing: 0) {
                        Spacer()
                        .frame(boxSize, boxSize / 2)
                        .background(Color.blue)
                        .borderColor(Color(white: 0.1))
                        .relativeYOffsetIf(self.open, -0.8)
           
                        Spacer()
                        .frame(boxSize, boxSize / 2)
                        .background(Color.blue)
                        .borderColor(Color(white: 0.1))
                        .relativeYOffsetIf(self.open, 0.8)
                    }
                    HStack(spacing: 0) {
                        Spacer()
                        .frame(boxSize / 2, boxSize)
                        .background(Color.blue)
                        .borderColor(Color(white: 0.1))
                        .relativeXOffsetIf(self.open, -0.8)
                  
                        Spacer()
                        .frame(boxSize / 2, boxSize)
                        .background(Color.blue)
                        .borderColor(Color(white: 0.1))
                        .relativeXOffsetIf(self.open, 0.8)
    
                    }
                }
                .shadowIf(open, color: Color.black.opacity(0.6), radius: 5)
            }
            .cutoutRectangle()
            Button(action: {
                withAnimation(.easeInOut(duration: 2)) {
                    self.open.toggle()
                }
            }) {
                Text("Toggle")
                    .font(Font.title)
                    .frame(150, 50)
                    .foregroundColor(.white)
                    .clipRoundedRectangleWithStroke(10, .black, lineWidth: 2, fill: Color.blue)
            }
        }
    }
}


struct ContentPien_Previews: PreviewProvider {
    struct ContentPien_Harness: View {
    
        var body: some View {
            
            ContentView().previewDevice(.iPhone_8)
        }
    }
    static var previews: some View {
            ContentPien_Harness()
        }
}

PureSwiftUIを使用することによって、かなりコードスライスできたことは、はっきりわかる。

結論

PureSwiftUIは神。

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?