Last active
April 5, 2025 23:39
-
-
Save benigumocom/fa3fb7b31f5377d270b8fe82fe538ae3 to your computer and use it in GitHub Desktop.
【SwiftUI】Create Animated Speech Bubble 👉 https://android.benigumo.com/20240827/speech-bubble/
This file contains hidden or 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 SpeechBubble: View { | |
var count: Int | |
var body: some View { | |
HStack(spacing: 0) { | |
Rectangle() | |
.fill(.red) | |
.rotationEffect(.degrees(45)) | |
.frame(width: 20, height: 20) | |
.offset(x: 14) | |
.clipShape(.rect) // * | |
HStack { | |
Image(systemName: "heart.fill") | |
Text("\(count)") | |
} | |
.foregroundStyle(.background) | |
.padding() | |
.background(.red, in: .rect(cornerRadius: 8)) | |
} | |
} | |
} | |
struct AnimatedSpeechBubble: View { | |
@State private var show = false | |
var body: some View { | |
VStack { | |
HStack { | |
Text("お知らせ") | |
.popover(isPresented: $show, arrowEdge: .trailing) { | |
Text("横に出せないの?") | |
.padding(.horizontal) | |
.foregroundStyle(.background) | |
.presentationBackground(.red) | |
.presentationCompactAdaptation(.popover) | |
} | |
Spacer() | |
} | |
.frame(width: 250, height: 50) | |
HStack { | |
Text("お知らせ") | |
if show { | |
SpeechBubble(count: 999) | |
.transition(.scale(scale: 0.25).combined(with: .opacity)) | |
} | |
Spacer() | |
} | |
.frame(width: 250, height: 50) | |
Button(show ? "hide" : "show") { | |
withAnimation { | |
show.toggle() | |
} | |
} | |
.buttonStyle(.borderedProminent) | |
} | |
} | |
} | |
#Preview("bubble") { | |
SpeechBubble(count: 999) | |
} | |
#Preview("animated") { | |
AnimatedSpeechBubble() | |
.padding() | |
.frame(maxWidth: .infinity) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you!