Created
February 24, 2024 09:16
-
-
Save VAndrJ/327d2a051979105751d3c6c2254e864f to your computer and use it in GitHub Desktop.
SwiftUI zIndex MRE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ContentView.swift | |
// PosIndexMRE | |
// | |
// Created by VAndrJ on 24.02.2024. | |
// | |
import SwiftUI | |
struct ContentView: View { | |
@State private var ids: [UUID] = (0...87).map { _ in UUID() } | |
@State private var selectedId: UUID? | |
var body: some View { | |
ScrollView { | |
LazyVGrid(columns: [GridItem(.adaptive(minimum: 110, maximum: 220), spacing: 8)], spacing: 8) { | |
ForEach(ids, id: \.self) { id in | |
ZStack { | |
Rectangle() | |
.foregroundStyle(.green) | |
FloatingView(isToggled: selectedId == id) | |
} | |
.aspectRatio(contentMode: .fit) | |
.zIndex(selectedId == id ? 1 : 0) | |
.onTapGesture { | |
selectedId = id | |
} | |
} | |
} | |
} | |
} | |
} | |
struct FloatingView: View { | |
let isToggled: Bool | |
@State private var offset = 0.0 | |
var body: some View { | |
if isToggled { | |
Circle() | |
.frame(width: 100, height: 100) | |
.offset(x: 0, y: offset) | |
.animation(.linear(duration: 1), value: offset) | |
.onAppear() { | |
offset = 1000 | |
} | |
.onDisappear() { | |
offset = 0 | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment