gpt4 book ai didi

ios - 在SwiftUI中创建自定义alertSheet

转载 作者:行者123 更新时间:2023-12-01 21:23:50 25 4
gpt4 key购买 nike

Swift 5.x iOS 13/14
试图构建一个自定义操作表,并弄不清楚为什么当我告诉它简单地从底部滑动时,它从左滑动然后向上滑动,或者至少我以为是我要的。

import SwiftUI

struct ContentView: View {
@State private var showingCustomSheet = false
var body: some View {
ZStack {
Button(action: {
self.showingCustomSheet.toggle()
}) {
Text("Fire")
.font(.largeTitle)
.foregroundColor(Color.red)
}
if showingCustomSheet {
CustomAlertSheet(showingCustomSheet: $showingCustomSheet)
}
}
}
}

struct CustomAlertSheet: View {
@Binding var showingCustomSheet: Bool
@State var showEscape = false
var body: some View {
VStack {
Spacer().onAppear {
withAnimation(.linear(duration: 2)) {
showEscape.toggle()
}
}
if showEscape {
Rectangle()
.fill(Color.red)
.frame(width: 256, height: 128)
.transition(.move(edge: .bottom))
.animation(.default)
}
}
}
}

最佳答案

这是一个解决方案-删除了多余的内容和一些小的修补程序...经过Xcode 12 / iOS 14测试。
demo

struct ContentView: View {
@State private var showingCustomSheet = false

var body: some View {
ZStack {
Button(action: {
self.showingCustomSheet.toggle()
}) {
Text("Fire")
.font(.largeTitle)
.foregroundColor(Color.red)
}
CustomAlertSheet(showingCustomSheet: $showingCustomSheet)
}
}
}

struct CustomAlertSheet: View {
@Binding var showingCustomSheet: Bool

var body: some View {
VStack {
Spacer()
if showingCustomSheet {
Rectangle()
.fill(Color.red)
.frame(width: 256, height: 128)
.transition(.move(edge: .bottom))
}
}
.animation(.default, value: showingCustomSheet)
}
}

关于ios - 在SwiftUI中创建自定义alertSheet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63598123/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com