JWAutumn / ACarousel

A carousel view for SwiftUI | SwiftUI 旋转木马效果
MIT License
381 stars 78 forks source link

Passing in binding for index disables animations. #6

Open josephmannis opened 3 years ago

josephmannis commented 3 years ago

Example:

    ...Other code

    @State var currIndex: Int = 0

    var body: some View {
        ACarousel(items, id: \.id, index: $currIndex, ...) { ... }
    }

In this case, the animations will not occur properly. Demo:

https://drive.google.com/file/d/1asAYFM6B3ZREpgqhD4Kr3behYqe2bsy9/view?usp=sharing

miguel-arrf commented 2 years ago

Yes, the problem still persists and this turns the package unusable for many situations, which is unfortunate given its quality... I hope that this can get fixed!

norrisboat commented 2 years ago

@josephmannis @miguel-arrf It's related to the id supplied into ACarousel. Try using just strings your items and make id: \.self and it should work

e.g

ACarousel(["1","2","3","4"], id: \.self, index: $currIndex,...){ ... }

e.g with struct

struct Item: Identifiable, Hashable {
    let id = UUID()
    let name: String
}

ACarousel([Item(name:"t"), Item(name:"e"), Item(name:"s"), Item(name:"t")], id: \.name, index: $currIndex,...){ ... }

For some reason, I haven't figured out yet using id: \.self doesn't work even though the struct conforms to Identifiable

dpyy commented 1 year ago

@josephmannis @miguel-arrf It's related to the id supplied into ACarousel. Try using just strings your items and make id: \.self and it should work

e.g

ACarousel(["1","2","3","4"], id: \.self, index: $currIndex,...){ ... }

e.g with struct

struct Item: Identifiable, Hashable {
    let id = UUID()
    let name: String
}

ACarousel([Item(name:"t"), Item(name:"e"), Item(name:"s"), Item(name:"t")], id: \.name, index: $currIndex,...){ ... }

For some reason, I haven't figured out yet using id: \.self doesn't work even though the struct conforms to Identifiable

Using your code I get

ForEach<Array<Item>, Int, ModifiedContent<ModifiedContent<ModifiedContent<ZStack<TupleView<(ModifiedContent<_ShapeView<RoundedRectangle, Color>, _AppearanceActionModifier>, ModifiedContent<Text, _PaddingLayout>)>>, _FrameLayout>, _FrameLayout>, _ScaleEffect>>: the ID 7 occurs multiple times within the collection, this will give undefined results! ForEach<Array<Item>, Int, ModifiedContent<ModifiedContent<ModifiedContent<ZStack<TupleView<(ModifiedContent<_ShapeView<RoundedRectangle, Color>, _AppearanceActionModifier>, ModifiedContent<Text, _PaddingLayout>)>>, _FrameLayout>, _FrameLayout>, _ScaleEffect>>: the ID 0 occurs multiple times within the collection, this will give undefined results!

in the console, is this ok? I guess that's how wrap works in this library?