Created
October 5, 2022 17:09
-
-
Save haneenmahd/c1a176b32f9da498af37faddaa80752d to your computer and use it in GitHub Desktop.
Dynamic Island in SwiftUI (Closing and Open the Dynamic Island)
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
import SwiftUI | |
struct DynamicView: View { | |
let width = 0.95 * UIScreen.main.bounds.width | |
let height: CGFloat = 200 | |
let x = UIScreen.main.bounds.width / 2 | |
let y: CGFloat = 110 | |
let closedWidth = 0.6 * UIScreen.main.bounds.width | |
let closedHeight: CGFloat = 40 | |
let closedY: CGFloat = 30 | |
let dynamicIslandAnimation: Animation = .spring(response: 0.7, dampingFraction: 0.7, blendDuration: 0.75) | |
@State private var capsuleClosed = false | |
var waveIcon: some View { | |
HStack(spacing: 3) { | |
Rectangle() | |
.fill(.gray.opacity(0.5)) | |
.frame(width: 4, height: 15) | |
.cornerRadius(30) | |
Rectangle() | |
.fill(.gray.opacity(0.5)) | |
.frame(width: 4, height: 20) | |
.cornerRadius(30) | |
Rectangle() | |
.fill(.gray.opacity(0.5)) | |
.frame(width: 4, height: 25) | |
.cornerRadius(30) | |
Rectangle() | |
.fill(.gray.opacity(0.5)) | |
.frame(width: 4, height: 18) | |
.cornerRadius(30) | |
Rectangle() | |
.fill(.gray.opacity(0.5)) | |
.frame(width: 4, height: 15) | |
.cornerRadius(30) | |
Rectangle() | |
.fill(.gray.opacity(0.5)) | |
.frame(width: 4, height: 20) | |
.cornerRadius(30) | |
} | |
} | |
var capsuleView: some View { | |
VStack { | |
HStack { | |
HStack(spacing: 20) { | |
Image("music-thumbnail") | |
.resizable() | |
.scaledToFit() | |
.frame(width: 70, height: 70) | |
.cornerRadius(20) | |
if !capsuleClosed { | |
VStack(alignment: .leading) { | |
Text("Entropy") | |
.font(.headline) | |
Text("Beach Bunny") | |
.foregroundColor(.secondary) | |
} | |
} | |
} | |
Spacer() | |
if capsuleClosed { | |
Image(systemName: "lines.measurement.horizontal") | |
} else { | |
waveIcon | |
} | |
} | |
.padding(.vertical, 10) | |
Group { | |
if !capsuleClosed { | |
HStack { | |
Text("2:50") | |
ProgressView(value: 0.75, total: 1) | |
.tint(.white) | |
Text("-0:51") | |
} | |
.font(.caption.bold()) | |
.foregroundColor(.secondary) | |
.padding(.vertical, 5) | |
HStack { | |
Spacer() | |
Spacer() | |
HStack(spacing: 20) { | |
Button { | |
// do nothing | |
} label: { | |
Image(systemName: "backward.fill") | |
} | |
Button { | |
// do nothing | |
} label: { | |
Image(systemName: "pause.fill") | |
.font(.largeTitle.bold()) | |
} | |
Button { | |
// do nothing | |
} label: { | |
Image(systemName: "forward.fill") | |
} | |
} | |
.font(.title.bold()) | |
Spacer() | |
Button { | |
} label: { | |
Image(systemName: "airpodspro") | |
.font(.title2) | |
} | |
} | |
.tint(.white) | |
} | |
} | |
} | |
.padding(.horizontal, 20) | |
.frame(width: capsuleClosed ? closedWidth : width, height: capsuleClosed ? closedHeight : height) | |
.background(.black) | |
.cornerRadius(50) | |
.position(x: x, y: capsuleClosed ? closedY: y) | |
.shadow(color: .black.opacity(0.5), radius: 20, x: 0, y: 20) | |
} | |
var body: some View { | |
ZStack { | |
Image("wallpaper") // get the assets here: https://github.com/haneenmahd/DynamicIsland-assets | |
.resizable() | |
.scaledToFill() | |
capsuleView | |
.onTapGestue { | |
withAnimation(dynamicIslandAnimation) { | |
capsuleClosed.toggle() | |
} | |
} | |
} | |
.preferredColorScheme(.dark) | |
.ignoresSafeArea() | |
} | |
} | |
struct DynamicView_Previews: PreviewProvider { | |
static var previews: some View { | |
DynamicView() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment