Skip to content

Instantly share code, notes, and snippets.

@VAndrJ
Created February 24, 2024 09:16
Show Gist options
  • Save VAndrJ/327d2a051979105751d3c6c2254e864f to your computer and use it in GitHub Desktop.
Save VAndrJ/327d2a051979105751d3c6c2254e864f to your computer and use it in GitHub Desktop.
SwiftUI zIndex MRE
//
// 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