I made a carousel based on a YouTube video I found (https://www.youtube.com/watch?v=4Gw5lDXJ04g&t=416s). I implemented everything, it shows my images but when I scroll to the right it's not snapping on to the image
I expected it to snap onto the image, but it doesn't. This is my SnapCarousel.swift
import SwiftUI
struct SnapCarousel<Content : View,T: Identifiable>: View{
var content: (T) -> Content
var list: [T]
// Properties...
var spacing: CGFloat
var trailingSpace: CGFloat
@Binding var index: Int
init(spacing: CGFloat = 15,trailingSpace: CGFloat = 100,index: Binding<Int>,items: [T],@ViewBuilder content: @escaping (T)->Content)
{
self.list = items
self.spacing = spacing
self.trailingSpace = trailingSpace
self._index = index
self.content = content
}
//offset
@GestureState var offset: CGFloat = 0
@State var currentIndex: Int = 0
var body: some View {
GeometryReader{ proxy in
let width = proxy.size.width - (trailingSpace - spacing)
let adjustMentWidth = (trailingSpace / 2) - spacing
HStack(spacing: spacing){
ForEach(list){item in
content(item)
.frame(width: proxy.size.width - trailingSpace)
}
.padding(.horizontal,spacing)
.offset(x: (CGFloat(currentIndex) * -width) (currentIndex != 0 ? adjustMentWidth : 0) offset)
.gesture(
DragGesture()
.updating($offset, body: { value, out, _ in
out = value.translation.width
})
.onEnded({ value in
//Update current index for scroll
let offsetX = value.translation.width
//convert the translation into progress (0-1)
//round the value based on the currentIndex
let progress = -offsetX / width
let roundIndex = progress.rounded()
//setting max and min
currentIndex = max(min(currentIndex Int(roundIndex), list.count - 1), 0)
//updating index
currentIndex = index
})
)
}
}
//animation when offset = 0
.animation(.easeInOut, value: offset == 0)
}
}
struct SnapCarousel_Previews: PreviewProvider {
static var previews: some View {
MotorDetail(landmark: landmarks[0])
}
}
this is how I load it in my MotorDetail (my view). I get all the images next to eachother and I can scroll but its not snapping onto it.
VStack (alignment: .leading, spacing: 12) {} .frame(maxWidth: .infinity,alignment: .leading) .padding()
// Snap Carousel....
SnapCarousel (trailingSpace: 27,index: $currentIndex, items: posts)
{post in
GeometryReader{proxy in
let size = proxy.size
Image(post.postImage)
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: size.width)
.cornerRadius(12)
.padding(.top, -200)
}
}
.padding(.vertical,80)
}
.frame(maxHeight: .infinity, alignment: .top)
.onAppear{ for index in 1...5{
posts.append(Post(postImage: "\(String(landmark.name))" "_overview\(index)"))}
I suppose it's due to the
.offset(x: (CGFloat(currentIndex) * -width) (currentIndex != 0 ? adjustMentWidth : 0) offset)
in my SnapCarousel, but I can't find what the problem is
CodePudding user response:
you have not added the .onChanged
modifier on your DragGesture,
either remove this line currentIndex = index
from .onEnded
modifier on DragGesture
Or
and add .onChanged
modifier in DragGesture and update the value of index
.