我正在尝试为底部工作表创建一个视图。当我们点击按钮底部时,它将显示一个带有按钮的视图。
我可以修改底板的颜色,高度,拐角半径,背景视图必须模糊的程度。
struct BottomSheetView: View {
@Binding var showBottomSheet: Bool
@Binding var bgColor: Color
@Binding var cornerRadius: CGFloat
@Binding var bottomSheetRatio: CGFloat
@Binding var blurPoint: CGFloat
var body: some View {
GeometryReader { geometry in
ZStack {
VStack {
Button(action: {
self.showBottomSheet.toggle()
}){
Text("click here")
.padding()
}
Spacer()
}
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
.blur(radius: self.showBottomSheet ? self.blurPoint : 0)
.onTapGesture {
if self.showBottomSheet {
self.showBottomSheet = false
}
}
Rectangle()
.fill(self.bgColor)
.cornerRadius(self.cornerRadius)
.offset(y: self.showBottomSheet ? geometry.size.height * self.bottomSheetRatio : geometry.size.height + 200)
.animation(.spring())
}
}
}
}
在上面的代码中
VStack {
Button(action: {
self.showBottomSheet.toggle()
}){
Text("click here")
.padding()
}
Spacer()
}
此VStack应该用其他一些View替换,我要从该View中传递绑定值。
有没有办法做到这一点?预先感谢。
答案 0 :(得分:0)
var ensurelog = function(req,res,next) {
passport.authenticate('local', function(err, user, info) {
console.log(user);
})(req, res, next);
};
Router.post("/login", ensurelog)
,您需要在View Ininit中传递内容 喜欢
struct BottomSheetView<Content: View>: View {
var content: Content
...
var body: some View {
content //do anything you want with content
}
}
答案 1 :(得分:0)
我已经通过ViewModifier
首先,我创建了一个ViewModifier结构。其中将包含init()
struct BottomSheetModifier: ViewModifier {
var showBottomSheet: Bool
var blurPoint: CGFloat
var bgColor: Color
var cornerRadius: CGFloat
var bottomSheetRatio: CGFloat
init(showBottomSheet: Bool, blurPoint: CGFloat, bgColor: Color, cornerRadius: CGFloat, bottomSheetRatio: CGFloat) {
self.showBottomSheet = showBottomSheet
self.blurPoint = blurPoint
self.bgColor = bgColor
self.cornerRadius = cornerRadius
self.bottomSheetRatio = bottomSheetRatio
}
func body(content: Content) -> some View {
GeometryReader { geometry in
ZStack {
content
.blur(radius: self.showBottomSheet ? self.blurPoint : 0)
RoundedRectangle(cornerRadius: self.cornerRadius)
.fill(self.bgColor)
.offset(y: self.showBottomSheet ? geometry.size.height * self.bottomSheetRatio : geometry.size.height + 200)
.animation(.spring())
}
}
}
}
第二,我为extension
创建了一个View
,它将具有一个将所有值作为参数的函数,并在其中初始化了修饰符。
extension View {
func bottomSheet(showBottomSheet: Bool, blurPoint: CGFloat, bgColor: Color, cornerRadius: CGFloat, bottomSheetRatio: CGFloat) -> some View {
modifier(BottomSheetModifier(showBottomSheet: showBottomSheet, blurPoint: blurPoint, bgColor: bgColor, cornerRadius: cornerRadius, bottomSheetRatio: bottomSheetRatio))
}
}
第三,我创建了一个视图。为此,我添加了一些元素,并在此处用所需的底纸尺寸,颜色和其他值简单地调用了修饰符。
struct DemoBottomSheetView: View {
@Binding var showBottomSheet: Bool
var body: some View {
VStack(spacing: 20) {
Text("welcome to bottom sheet")
Button(action: {
self.showBottomSheet.toggle()
}){
Text("Click Me")
.padding()
.background(Color.blue)
.foregroundColor(.white)
.cornerRadius(10)
}
Spacer()
}
.onTapGesture {
if self.showBottomSheet {
self.showBottomSheet = false
}
}
.bottomSheet(showBottomSheet: self.showBottomSheet, blurPoint: 4, bgColor: .red, cornerRadius: 25, bottomSheetRatio: 0.5)
}
}
最终输出