iOS Development

ios – create a swipe down gesture in swiftUI?

Spread the love


I wish to create a swipe down gesture that runs a operate when detected.

.gesture(
            DragGesture()
                .onChanged { worth in
                    if worth.translation.peak > 0 && abs(worth.translation.peak) > abs(worth.translation.width) {
                        addTask()
                    }
                }
        )

that is the closest i obtained however with this implementation it makes the swipeActions attatched to the forEach sluggish and laggy.
is there a greater manner ?

Full code:

import SwiftUI
import SwiftData
import UserNotifications

// Customized Yellow
let yellowCustom = Coloration(purple: 1, inexperienced: 0.811, blue: 0, opacity: 1.0)

struct ContentView: View {
//SwiftData
@Setting(.modelContext) var modelContext
@Question(kind: [SortDescriptor(Task.complete), SortDescriptor(Task.date, order: .reverse)]) var job: [Task]
@State non-public var path = [Task]()
let firstLaunchKey = "hasLaunchedBefore"


var physique: some View {
    NavigationStack(path: $path) {
        Checklist {
            ForEach(job) { job in
                TasksView(job: job)
                    .strikethrough(job.full)
                    .foregroundColor(job.full ? .grey : .major)
                
                //Performed Gesture
                    .swipeActions(edge: .main) {
                        if job.full {
                            Button("Not Performed") {
                                notDone(job: job)
                            }
                            .tint(.yellow)
                        } else {
                            Button("Performed") {
                                accomplished(job: job)
                            }
                            .tint(.inexperienced)
                        }
                    }
            }
            //Delete Gesture
            .onDelete(carry out: deleteTask)
        }
        .animation(.default, worth: job)
        
        //addTask Gesture
        .gesture(
            DragGesture()
                .onChanged { worth in
                    if worth.translation.peak > 0 && abs(worth.translation.peak) > abs(worth.translation.width) {
                        addTask()
                    }
                }
        )
        .navigationTitle("Duties")
    }
    .onAppear {
        requestNotificationPermission()
        checkFirstLaunch()
    }
}

// addTask
func addTask() {
    let job = Job()
    isEditing = true
    modelContext.insert(job)
}

// deleteTask
func deleteTask(_ indexSet: IndexSet) {
    for index in indexSet {
        let job = job[index]
        modelContext.delete(job)
    }
}

// swipeDone
func accomplished(job: Job) {
    job.full = true
}

// swipeNotDone
func notDone(job: Job) {
    job.full = false
}

//Notifications permission
func requestNotificationPermission() {
    UNUserNotificationCenter.present().requestAuthorization(choices: [.alert, .badge, .sound]) { success, error in
        if success {
            print("Permission granted")
        } else if let error = error {
            print(error.localizedDescription)
        }
    }
}

//Checks if consumer launched the app earlier than
func checkFirstLaunch() {
    if !UserDefaults.customary.bool(forKey: firstLaunchKey) {
        // First launch, initialize duties
        initializeTasksWithCustom()

        // Set the flag to true so this does not run once more
        UserDefaults.customary.set(true, forKey: firstLaunchKey)
    }
}


//Information duties
func initializeTasksWithCustom() {
    let customTask1 = Job()
    customTask1.title = "swipe proper to mark us uncompleted"
    customTask1.full = true

    let customTask2 = Job()
    customTask2.title = "Swipe proper to finish"

    let customTask3 = Job()
    customTask3.title = "Swipe left to delete"

    modelContext.insert(customTask1)
    modelContext.insert(customTask2)
    modelContext.insert(customTask3)

    strive? modelContext.save()
}
}

//Kind Boolean
extension Bool: Comparable {
public static func <(lhs: Self, rhs: Self) -> Bool {
    // the one true inequality is fake < true
    !lhs && rhs
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *