我是SwiftUI的新手,正在尝试查看文件的预览。当我运行文件时,它说构建成功,但未显示预览。我一直在寻找解决该问题的方法,并尝试了诸如更新Xcode,创建新文件等操作。我该如何解决?
这是我的代码。
import SwiftUI
struct ChooseGoalsView: View {
@State var goals = goalCardData
@State var active = false
@State var activeIndex = -1
@State var activeView = CGSize.zero
var body: some View {
ZStack {
Color.black.opacity(Double(self.activeView.height/500))
.animation(.linear)
.edgesIgnoringSafeArea(.all)
ScrollView {
VStack(spacing: 25) {
HStack {
Image(uiImage: #imageLiteral(resourceName: "back"))
.foregroundColor(Color("Dark Grey"))
Text("Choose")
.font(.custom("PlayfairDisplay-Bold", size: 36))
.foregroundColor(Color("Dark Blue"))
Text("a category")
.font(.custom("PlayfairDisplay-Regular", size: 36))
.foregroundColor(Color("Grey"))
}
.frame(maxWidth: .infinity, alignment: .leading)
.padding(.leading, 30)
.padding(.top, 30)
.blur(radius: active ? 20 : 0)
ForEach(goals.indices, id: \.self) { index in
GeometryReader { geometry in
GoalsView(
show: self.$goals[index].show,
goal: self.goals[index],
active: self.$active,
index: index,
activeIndex: self.$activeIndex,
activeView: self.$activeView
)
.offset(y: self.goals[index].show ? -geometry.frame(in: .global).minY : 0)
.opacity(self.activeIndex != index && self.active ? 0 : 1)
.scaleEffect(self.activeIndex != index && self.active ? 0.5 : 1)
.offset(x: self.activeIndex != index && self.active ? screen.width : 0)
}
.frame(height: 280)
.frame(maxWidth: self.goals[index].show ? .infinity : screen.width - 60)
.zIndex(self.goals[index].show ? 1 : 0)
}
}
.frame(width: screen.width)
.animation(.spring(response: 0.5, dampingFraction: 0.6, blendDuration: 0))
}
.statusBar(hidden: active ? true : false)
.animation(.linear)
}
}
}
#if DEBUG
struct ChooseGoalsView_Previews: PreviewProvider {
static var previews: some View {
ChooseGoalsView()
}
}
#endif
struct GoalsView: View {
@Binding var show: Bool
var goal: GoalsCards
@Binding var active: Bool
var index: Int
@Binding var activeIndex: Int
@Binding var activeView: CGSize
var body: some View {
ZStack(alignment: .top) {
VStack(alignment: .leading) {
Text(goal.description)
.font(.custom("PlayfairDisplay-Regular", size: 20))
.foregroundColor(Color("Dark Text"))
Button(action: {}) {
Text("Get Involved")
.background(Color(goal.color))
.font(.custom("PlayfairDisplay-Bold", size: 18))
.foregroundColor(Color("Dark Text"))
.offset(x: screen.width - 200, y: 50)
}
}
.padding(30)
.frame(maxWidth: show ? .infinity : screen.width - 60, maxHeight: show ? .infinity : 280, alignment: .top)
.offset(y: show ? 375 : 0)
.background(Color.white)
.clipShape(RoundedRectangle(cornerRadius: 30, style: .continuous))
.shadow(color: Color.black.opacity(0.2), radius: 20, x: 0, y: 20)
.opacity(show ? 1 : 0)
VStack {
HStack(alignment: .top) {
Text(goal.title)
.font(.custom("PlayfairDisplay-Bold", size: 34))
.foregroundColor(.white)
Spacer()
VStack {
Image(systemName: "xmark")
.font(.system(size: 16, weight: .medium))
.foregroundColor(.white)
}
.frame(width: 36, height: 36)
.background(Color.black)
.clipShape(Circle())
.opacity(show ? 1 : 0)
}
Spacer()
Image(uiImage: goal.image)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(maxWidth: .infinity)
.frame(height: 140, alignment: .top)
}
.padding(show ? 30 : 20)
.padding(.top, show ? 30 : 0)
.frame(maxWidth: show ? .infinity : screen.width - 60, maxHeight: show ? 375 : 280)
.background(Color(goal.color))
.clipShape(RoundedRectangle(cornerRadius: 30, style: .continuous))
.shadow(color: Color(goal.color).opacity(0.3), radius: 20, x: 0, y: 20)
.gesture(
show ?
DragGesture().onChanged { value in
guard value.translation.height < 300 else { return }
guard value.translation.height > 0 else { return }
self.activeView = value.translation
}
.onEnded { value in
if self.activeView.height > 50 {
self.show = false
self.active = false
self.activeIndex = -1
}
self.activeView = .zero
}
: nil
)
.onTapGesture {
self.show.toggle()
self.active.toggle()
if self.show {
self.activeIndex = self.index
} else {
self.activeIndex = -1
}
}
}
.frame(height: show ? screen.height : 280)
.scaleEffect(1 - self.activeView.height / 1000)
.rotation3DEffect(Angle(degrees: Double(self.activeView.height / 10)), axis: (x: 0, y: 10.0, z: 0))
.hueRotation(Angle(degrees: Double(self.activeView.height)))
.animation(.spring(response: 0.5, dampingFraction: 0.6, blendDuration: 0))
.gesture(
show ?
DragGesture().onChanged { value in
guard value.translation.height < 300 else { return }
guard value.translation.height > 0 else { return }
self.activeView = value.translation
}
.onEnded { value in
if self.activeView.height > 50 {
self.show = false
self.active = false
self.activeIndex = -1
}
self.activeView = .zero
}
: nil
)
.edgesIgnoringSafeArea(.all)
}
}
struct GoalsCards: Identifiable {
var id = UUID()
var title: String
var image: UIImage
var color: UIColor
var show: Bool
var description: String
}
var goalCardData = [
GoalsCards(title: "One", image: #imageLiteral(resourceName: "Frame 1"), color: #colorLiteral(red: 0.4941176471, green: 0.8, blue: 0.6235294118, alpha: 1), show: false, description: "This is the first goal."),
GoalsCards(title: "Two", image: #imageLiteral(resourceName: "Frame 2"), color: #colorLiteral(red: 0.8156862745, green: 0.9529411765, blue: 0.8666666667, alpha: 1), show: false, description: "This is the second goal.")]
let screen = UIScreen.main.bounds