TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: Freezing modifiers that are based on states

Forums > SwiftUI

I have this simplified version of a view I am working on:

struct RowView: View {
    @State private var isChecked = false
    @State private var offset = 0.0

    var body: some View {
        Rectangle()
            .fill(.gray)
            .frame(height: 100)
        .offset(x: offset)
        .gesture(
            DragGesture()
                .onChanged {
                    offset = $0.translation.width
                    // Unfreze it here
                }
                .onEnded { _ in
                    if offset >= 100 {
                        isChecked.toggle()
                    }
                    withAnimation(.spring()) {
                        offset = 0
                        // Freeze the modifier here
                    }
                }
        )
        .background {
            Rectangle()
                .fill(isChecked ? .blue : .orange) // The modifier
        }
    }
}

When you swipe the view to the right enough it switches color beween blue and orange.

The problem is that when I end the dragging the background changes color immediately. The preferred solution would be to freeze the background rectangle's fill modifier when I end the drag and unfreeze it when I start dragging again. I can't toggle isChecked when the animation ends as other elements depend on that changing immediately.

2      

If I could get this here working then problem solved, but that solution has some problems

2      

I have made this temporary fix:

struct RowView: View {
    @State private var isChecked = false
    @State private var isChecked_delayed = false {
        didSet {
            isChecked = isChecked_delayed
        }
    }

    @State private var offset = 0.0

    var body: some View {
        Rectangle()
            .fill(.gray)
            .frame(height: 100)
            .offset(x: offset)
            .gesture(
                DragGesture(minimumDistance: 10)
                    .onChanged {
                        offset = $0.translation.width
                        isChecked_delayed = isChecked
                    }
                    .onEnded { _ in
                        if offset >= 100 {
                            isChecked.toggle()
                        }
                        withAnimation(.spring()) {
                            offset = 0
                        }
                    }
            )
            .background {
                isChecked_delayed ? Color.blue : Color.orange
            }
    }
}

I very much don't like this solution, as it doesn't scale at all for multiple variables.

2      

Based on the context provided, it seems you are referring to "freezing modifiers" in the context of software development or programming. When you mention "based on states," it likely means that these modifiers are dependent on the current state of a system or object. Here's a brief explanation of this concept:

In software development, "freezing modifiers" often refer to preserving or locking certain properties or attributes of an object or system when it is in a particular state. This is commonly used in situations where you want to prevent further changes to certain aspects of an object once it reaches a specific state.

For example, consider an online order processing system. https://www.hackingwithswift.com/ When an order is placed and payment is successful, the order transitions to a "completed" state. In this "completed" state, you may want to freeze certain order details, such as the order total, customer information, and items purchased, to prevent accidental modifications that could disrupt the accuracy of the order history.

By freezing modifiers based on states, you ensure that critical data remains consistent and unalterable once it reaches a particular state, maintaining data integrity and reducing the risk of unintended consequences.

This concept is often implemented using techniques such as immutable objects, read-only properties, or state-based access control mechanisms, depending on the programming language and design patterns used in the software development process.

2      

Hacking with Swift is sponsored by String Catalog.

SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!

Localize My App

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.