如何在 SwiftUI 中显示消息气球? [英] How to show a message balloon in SwiftUI?

查看:22
本文介绍了如何在 SwiftUI 中显示消息气球?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此组件通常在您连接到 Airpods 时显示.这会在屏幕顶部以吐司/气球/药丸形状显示一条消息,带有一个小图像(图标)和一个标题,它会在几秒钟内消失,类似于 Android 端的吐司.

This component usually shows when you connect to Airpods. That shows a message in a toast/balloon/pill shape at the top of the screen, with a small image (icon) and a title, it would dismiss in a few seconds, similar to the toast on the Android side.

这是一个内置的视图控件吗?查看我的屏幕截图了解更多详情.

Is this a built-in view control? Check my screenshot for more details.

截图1

推荐答案

非内置但易于制作

import SwiftUI
struct ToastParentView: View {
    @State var showToast: Bool = false
    var body: some View {
        VStack{
            Button(action: {
                showToast.toggle()
            }, label: {
                Text("show toast")
            })
            List(){
                ForEach(0...20, id: \.self, content: { idx in
                    Text("\(idx)")
                })
            }
        }.toast(showToast: $showToast, position: .top, toastContent: {
            VStack{
                Text("Hello World!!")
                Text("Headline").font(.headline)
            }
        })
    }
}
extension View {
    func toast<T:View>(showToast: Binding<Bool>, duration: TimeInterval = 5, position: ToastView<T>.ToastPosition = .top, @ViewBuilder toastContent: @escaping () -> T) -> some View {
        modifier(ToastView(showToast: showToast, toastContent: toastContent(), duration: duration, position: position))
        
    }
}
struct ToastView<T: View>: ViewModifier {
    @Binding var showToast: Bool
    let toastContent: T
    @State var timer: Timer?
    let duration: TimeInterval
    let position: ToastPosition
    func body(content: Content) -> some View {
        GeometryReader{ geo in
            ZStack{
                content
                if showToast{
                    RoundedRectangle(cornerRadius: 25)
                        .foregroundColor(Color(UIColor.systemBackground))
                        .shadow(radius: 10)
                        .overlay(toastContent
                                    .minimumScaleFactor(0.2))
                        .frame(maxWidth: geo.size.width*0.6, maxHeight: 55 ,alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
                    
                        .position(x: geo.size.width/2, y: getYPosition(height: geo.size.height, safeAreaInset: geo.safeAreaInsets))
                        .onAppear(){
                            timer = Timer.scheduledTimer(withTimeInterval: duration, repeats: false, block: {_ in
                                showToast = false
                            })
                        }
                        .onDisappear(){
                            print("onDisappear")
                            timer?.invalidate()
                            timer = nil
                        }
                    
                }
            }.onTapGesture {
                showToast = false
            }
        }
        
    }
    enum ToastPosition{
        case top
        case bottom
        case middle
    }
    func getYPosition(height: CGFloat, safeAreaInset: EdgeInsets) -> CGFloat{
        var result: CGFloat = 0
        if position == .top{
            result = 0 + safeAreaInset.top
        }else if position == .bottom{
            result = height - safeAreaInset.bottom
        }else if position == .middle{
            result = height/2
        }
        return result
    }
}

这篇关于如何在 SwiftUI 中显示消息气球?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆