Skip to content

Instantly share code, notes, and snippets.

@pennisi
Last active May 16, 2022 20:01
Show Gist options
  • Save pennisi/a17f1ff2f45aa850a3779159a4d0ff16 to your computer and use it in GitHub Desktop.
Save pennisi/a17f1ff2f45aa850a3779159a4d0ff16 to your computer and use it in GitHub Desktop.
SwiftUI custom Transition thingy
//
// ContentView.swift
// TestTest
//
// Created by Sandro Pennisi on 16.05.22.
//
import SwiftUI
struct WhateverModifier: ViewModifier {
let height: Double
func body(content: Content) -> some View {
content
.mask {
Rectangle()
.frame(maxHeight: height)
}
}
}
struct ContentView: View {
@State var expanded = false
var body: some View {
VStack(alignment: .leading) {
Text("Hi I am some great cool text and I'm awesome. Hi I am some great cool text and I'm awesome. Hi I am some great cool text and I'm awesome. Hi I am some great cool text and I'm awesome. ")
Button {
expanded.toggle()
} label: {
Text("Tap me")
}
if expanded {
Image("img")
.resizable()
.aspectRatio(contentMode: .fit)
.transition(.modifier(active: WhateverModifier(height: 0), identity: WhateverModifier(height: .infinity)))
}
Text("Hi I am some great cool text and I'm awesome. Hi I am some great cool text and I'm awesome. Hi I am some great cool text and I'm awesome. Hi I am some great cool text and I'm awesome. Hi I am some great cool text and I'm awesome. Hi I am some great cool text and I'm awesome. ")
}
.animation(.default, value: expanded)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment