我已经尝试了这篇文章的前 3 个答案,但它们都有相同的问题\xe2\x80\x94,当模糊视图下面有多种颜色时,它只有 1 种颜色。这是我尝试过的解决方案之一:
\nimport SwiftUI\n\nstruct ContentView: View {\n var body: some View {\n ZStack {\n VStack{\n ForEach(0..<20, id: \\.self){ num in\n Rectangle()\n .frame(height: 20)\n .padding(.vertical, 6)\n }\n }\n Blur(style: .systemThinMaterialLight)\n .mask(\n VStack(spacing: 0) {\n Rectangle()\n .frame(width: 347, height: 139)\n .padding(.top, 0)\n Spacer()\n }\n )\n .allowsHitTesting(false)\n }\n }\n}\n\nstruct Blur: UIViewRepresentable {\n var style: UIBlurEffect.Style = .systemMaterial\n func makeUIView(context: Context) -> UIVisualEffectView {\n return UIVisualEffectView(effect: UIBlurEffect(style: style))\n }\n func updateUIView(_ uiView: UIVisualEffectView, context: Context) {\n …Run Code Online (Sandbox Code Playgroud) 我想用 SwiftUI 创建一个按钮,当我的手指触摸它时就会触发它(就像 UIKit 的触摸而不是触摸内部)。我还希望当我的手指按下按钮时按钮的不透明度变为 0.7。我希望只有当我的手指不再触摸按钮时,按钮的不透明度才变回 1。
我尝试了两种不同类型的按钮样式来创建这样的按钮,但都失败了:
struct ContentView: View {
var body: some View {
Button(action: {
print("action triggered")
}){
Text("Button").padding()
}
.buttonStyle(SomeButtonStyle())
}
}
struct SomeButtonStyle: ButtonStyle {
func makeBody(configuration: Self.Configuration) -> some View {
configuration.label
.background(Color.green)
.opacity(configuration.isPressed ? 0.7 : 1)
.onLongPressGesture(
minimumDuration: 0,
perform: configuration.trigger//Value of type 'SomeButtonStyle.Configuration' (aka 'ButtonStyleConfiguration') has no member 'trigger'
)
}
}
struct SomePrimativeButtonStyle: PrimitiveButtonStyle {
func makeBody(configuration: Configuration) -> some View {
configuration.label
.background(Color.green)
.opacity(configuration.isPressed ? 0.7 : 1)//Value …Run Code Online (Sandbox Code Playgroud) 在 Twitter 应用程序中,当您点击共享按钮,然后点击复制链接按钮时,显示“已复制到剪贴板”的视图将从顶部移入屏幕。它在屏幕顶部停留约 1 秒钟,然后移回到屏幕上并消失。像这样:
我的目标是每当我双击文本视图时在我的 SwiftUI 应用程序中创建相同的效果。但我就是不知道如何实现这种效果。这是我能得到的最好的:
import SwiftUI
struct ContentView: View {
@State private var copied = false
var body: some View {
GeometryReader { gr in
ZStack {
if copied {
Text("Copied to clipboard")
.padding()
.background(Color.blue.cornerRadius(20))
.position(x: gr.frame(in: .local).midX)
.transition(.move(edge: .top))
.animation(Animation.easeInOut(duration: 2).repeatCount(2))
}
VStack {
Text("Copy")
.onTapGesture(count: 2) {
self.copied.toggle()
}
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
}
Run Code Online (Sandbox Code Playgroud)