Hey guys, I’m trying to build this app for a restaurant and I wanted to know why I was getting the error of:
I tried many solutions, but none worked, can you please help me?
(P.S: I marked the lines with an error like this:
//error
)
My code:
import SwiftUI
struct Home : View {
@State var index = 0
@State var stories = [
Story(id: 0, image: "Biriyani", offset: 0,title: "Hyderabadi Chicken Dum Biriyani"),
Story(id: 1, image: "Gobi65", offset: 0,title: "Cauliflower 65"),
Story(id: 2, image: "Noodles", offset: 0,title: "Vegetable Hakka Noodles"),
Story(id: 3, image: "Paneer", offset: 0,title: "Paneer Tikka Masala"),
Story(id: 4, image: "Kothu", offset: 0,title: "Chicken Kothu Parotta"),
Story(id: 5, image: "Rose Milk", offset: 0,title: "Rose Milk"),
Story(id: 6, image: "Naan", offset: 0,title: "Paneer Butter Masala with Naan and Jeera Rice"),
]
@State var scrolled = 0
@State var index1 = 0
var body: some View {//error
ScrollView(.vertical, showsIndicators: false) {
VStack{
ZStack{
ForEach(stories.reversed()) { story in
HStack{
ZStack(alignment: Alignment(horizontal: .leading, vertical: .bottom)){
Image(story.image)
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: calculateWidth(), height: (UIScreen.main.bounds.height / 1.8) - CGFloat(story.id - scrolled) * 50) .cornerRadius(15)
VStack(alignment: .leading,spacing: 18){ HStack{
Text(story.title)
.font(.title)
.fontWeight(.bold)
.foregroundColor(.white)
Spacer()
}
Text("@Sai Mirras Kitchen")
.font(.caption)
.fontWeight(.bold)
.foregroundColor(.white)
.padding(.vertical,6)
.padding(.horizontal,25)
.background(Color("Background"))
.clipShape(Capsule())
}
.frame(width: calculateWidth() - 40)
.padding(.leading,20)
.padding(.bottom,20)
}
.offset(x: story.id - scrolled <= 2 ? CGFloat(story.id - scrolled) * 30 : 60)
Spacer(minLength: 0)
} .contentShape(Rectangle())
.offset(x: story.offset) .gesture(
DragGesture().onChanged({ (value) in withAnimation{
if value.translation.width < 0 &&
story.id != stories.last!.id{
stories[story.id].offset = value.translation.width
} else{
if story.id > 0{
stories[story.id - 1]
.offset = -(calculateWidth() + 60) + value.translation.width
}
}
}
})
.onEnded({ (value) in
withAnimation{
if value.translation.width < 0{
if -value.translation.width > 180 && story.id != stories.last!.id{
stories[story.id].offset = -(calculateWidth() + 60)
scrolled += 1
} else{
stories[story.id].offset = 0
}
} else{
if story.id > 0{
if value.translation.width > 180{
stories[story.id - 1]
.offset = 0
scrolled -= 1
} else{
stories[story.id - 1]
.offset = -(calculateWidth() + 60)
}
}
}
}
}))
}
.frame(height: UIScreen.main.bounds.height / 1.8)
.padding(.horizontal,25)
.padding(.top,25)
}
}
}
}
func calculateWidth()->CGFloat {
let screen = UIScreen.main.bounds.width - 50
let width = screen - (2 * 30)
return width
}
}
struct Story : Identifiable {
var id : Int
var image : String
var offset : CGFloat
var title : String
}
struct Home_Previews: PreviewProvider {
static var previews: some View {
Home()
}
}
struct MemberHome : View {
@State var index = 0
@State var stories = [
Story(id: 0, image: "Paneer", offset: 0,title: "Standard Membership"),
Story(id: 1, image: "Rose Milk", offset: 0,title: "Bronze Membership"),
Story(id: 2, image: "Silver", offset: 0,title: "Silver Membership"),
Story(id: 3, image: "Gold", offset: 0,title: "Gold Membership"),
Story(id: 4, image: "Naan", offset: 0,title: "Platinum Membership"),
Story(id: 5, image: "Biriyani", offset: 0,title: "Titanium Membership"),
]
@State var scrolled = 0
@State var index1 = 0
var body: some View { //error
ScrollView(.vertical, showsIndicators: false) {
VStack{
ZStack{
ForEach(stories.reversed()) { story in
HStack{
ZStack(alignment: Alignment(horizontal: .leading, vertical: .bottom)){
Image(story.image)
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: calculateWidth(), height: (UIScreen.main.bounds.height / 1.8) - CGFloat(story.id - scrolled) * 50) .cornerRadius(15)
VStack(alignment: .leading,spacing: 18){ HStack{
Text(story.title)
.font(.title)
.fontWeight(.bold)
.foregroundColor(.white)
Spacer()
}
Text("@SMK Memberships")
.font(.caption)
.fontWeight(.bold)
.foregroundColor(.white)
.padding(.vertical,6)
.padding(.horizontal,25)
.background(Color("Background"))
.clipShape(Capsule())
}
.frame(width: calculateWidth() - 40)
.padding(.leading,20)
.padding(.bottom,20)
}
.offset(x: story.id - scrolled <= 2 ? CGFloat(story.id - scrolled) * 30 : 60)
Spacer(minLength: 0)
} .contentShape(Rectangle())
.offset(x: story.offset) .gesture(
DragGesture().onChanged({ (value) in withAnimation{
if value.translation.width < 0 &&
story.id != stories.last!.id{
stories[story.id].offset = value.translation.width
} else{
if story.id > 0{
stories[story.id - 1]
.offset = -(calculateWidth() + 60) + value.translation.width
}
}
}
})
.onEnded({ (value) in
withAnimation{
if value.translation.width < 0{
if -value.translation.width > 180 && story.id != stories.last!.id{
stories[story.id].offset = -(calculateWidth() + 60)
scrolled += 1
} else{
stories[story.id].offset = 0
}
} else{
if story.id > 0{
if value.translation.width > 180{
stories[story.id - 1]
.offset = 0
scrolled -= 1
} else{
stories[story.id - 1]
.offset = -(calculateWidth() + 60)
}
}
}
}
}))
}
.frame(height: UIScreen.main.bounds.height / 1.8)
.padding(.horizontal,25)
.padding(.top,25)
}
}
}
}
func calculateWidth()->CGFloat {
let screen = UIScreen.main.bounds.width - 50
let width = screen - (2 * 30)
return width
}
}