c-villain / SwipeActions

Swipe actions for any view, swipe menu based on SwiftUI, full swiping and RTL languages supporting, iOS 13+, add your own views to swipes
https://t.me/swiftui_dev
MIT License
234 stars 27 forks source link

Adding @State to trigger indications of swipe #13

Closed Dave181295 closed 5 months ago

Dave181295 commented 1 year ago

Thanks for the lib, it would be Nice to trigger a UI element for the swipe behavior of any rows by passing a bool state , like the EditButton from the original List, this will help as the user for now doesn't know if he can swipe any cell.

c-villain commented 1 year ago

Hi, @Dave181295 ! Thx for your suggestion for improvement! Actually, you may use smth like this:

import SwiftUI

public extension View {
    @ViewBuilder
    func swipeHint(_ isActive: Bool = false, hintOffset: CGFloat) -> some View {
        if isActive {
            modifier(SwipeHintModifier(hintOffset: hintOffset))
        } else {
            self
        }
    }
}

public struct SwipeHintModifier: ViewModifier {
    let hintOffset: CGFloat
    @State private var offset: CGFloat = 0

    public func body(content: Content) -> some View {
        content
            .offset(x: -offset)
            .onAppear {
                    withAnimation(.easeInOut.delay(0.3)) {
                        offset = hintOffset
                    }
                    withAnimation(.easeInOut.delay(1.3)) {
                        offset = 0
                    }
            }
    }
}

and add this modifier to our content, for example, like this way:

ForEach(range, ...) {
    YourCell()
        ...
        .addFullSwipeAction(
            menu: .slided,
            swipeColor: .red,
            state: $state) {
                Leading {
                    ...
                }
                Trailing {
                    ...
                }
            }
        .swipeHint(cell == range.first, hintOffset: 120.0) // for trailing
        .swipeHint(cell == range[1] , hintOffset: -120.0) // for leading
    ...
}
c-villain commented 5 months ago

added in 0.3.5