SwiftUI/SwiftUI(Basic)

[58] List Swipe Actions

Toughie 2023. 5. 12. 01:46

⭐️List Swipe Actions⭐️

굉장히 마음에 드는 모디파이어다..!

원래 기존에는 delete밖에 없었는데, 이제는 leading과 trailing에 원하는 버튼을 다 넣을 수 있다.

 

//  Created by Toughie on 2023/05/12.
//

import SwiftUI

struct ListSwipeActionsPrac: View {
    
    
    @State var fruits: [String] = [
    "Apple", "Orange", "Banana", "Watermelon"
    ]
    
    var body: some View {
        List {
            ForEach(fruits, id: \.self) {
            //fruit in 대신 $0으로 클로저 최적화
                Text($0)
                //스와이프 액션 추가!
                    .swipeActions(
                    //leading, trailing 선택 가능
                        edge: .trailing,
                        //풀 스와이프 액션을 통해 execute 실행 여부 선택 가능
                        allowsFullSwipe: false) {
                            Button("Check") {
                                
                            }
                            .tint(Color.red.opacity(0.8))
                            Button("Buy") {
                                
                            }
                            .tint(Color.green.opacity(0.8))
                            Button("Sell") {
                                
                            }
                            .tint(Color.blue.opacity(0.8))
                        }
                        .swipeActions(
                            edge: .leading,
                            allowsFullSwipe: true) {
                                Button("share") {
                                    fruits.append("WOW")
                                }
                                .tint(Color.purple.opacity(0.8))

                            }
            }
//            .onDelete(perform: delete)
        }
    }
    
//    func delete(indexSet: IndexSet) {
//        fruits.remove(atOffsets: indexSet)
//    }
}