1

I'm scrolling large views(screen width) horizontally inside a scrollview. I would like the views to automatically center and not be in a scenario where the two views are both partially visible.

I'm already using GeometryReader's data to perform 3D rotation animation for the views. I've tried to add the UIScreen's position to the card.position( ) with a ternary operator.

struct WorkoutCard: View {

var numberOfWorkouts : [Int] = [0, 1, 2, 3]

var deviceCentre = (CGPoint(x: CGFloat(UIScreen.main.bounds.midX), y: CGFloat(UIScreen.main.bounds.midY)))

var body: some View {
        ZStack {

    ScrollView (.horizontal, showsIndicators: false) {

        HStack {


   ForEach(self.numberOfWorkouts.reversed(), id: \.self) { index in

                    GeometryReader { geometry in

                        Card(index).position(geometry.frame(in: .global).maxY == 0 ? self.deviceCentre : self.position() as! CGPoint )
                            .rotation3DEffect(Angle(degrees: Double(geometry.frame(in: .global).minX) / 4 ) ,
                                                     axis: (x: 0, y: 10 , z: 0))

                        }.frame( width: 414, height : 896)

            }
}

    }
          }.edgesIgnoringSafeArea(.all)
}
 }

I want the card view to center itself when not scrolling to avoid 2 card views showing.

arthas
  • 680
  • 1
  • 8
  • 16
  • Possible duplicate of [Is there any way to make a paged ScrollView in SwiftUI?](https://stackoverflow.com/questions/56893240/is-there-any-way-to-make-a-paged-scrollview-in-swiftui) – superpuccio Aug 16 '19 at 16:43
  • When you say "[...] I would like the views to automatically centre and not be in a scenario where the two views are both partially visible. [...]" you're actually saying that you'd need a ScrollView with pagination. With UIKit it's pretty easy, but unfortunately SwiftUI doesn't provide such feature (at least not yet). – superpuccio Aug 16 '19 at 16:45
  • @superpuccio, I think I need to adjust it via offest or frame using the geometry reader and touchended on the drag gesture. Apps like Apple music, use this in their scroll views so when a touch is ended items auto-position themselves – arthas Aug 18 '19 at 21:36
  • 1
    In order to get what you want you'd need the scroll view delegate methods, like scrollViewDidScroll or scrollViewDidEndDecelerating and so on (I think you'd like to reposition your views at the end of the scrolling to always have a view centred, right?). But, at the moment, SwiftUI doesn't have any scroll view delegate thing or stuff like that. – superpuccio Aug 19 '19 at 14:15
  • That definitely would be the best-case scenario. – arthas Aug 19 '19 at 18:21

0 Answers0