Search
 
SCRIPT & CODE EXAMPLE
 

SWIFT

detect binding valu change swiftui

import Combine
import SwiftUI

/// See `View.onChange(of: value, perform: action)` for more information
struct ChangeObserver<Base: View, Value: Equatable>: View {
    let base: Base
    let value: Value
    let action: (Value)->Void

    let model = Model()

    var body: some View {
        if model.update(value: value) {
            DispatchQueue.main.async { self.action(self.value) }
        }
        return base
    }

    class Model {
        private var savedValue: Value?
        func update(value: Value) -> Bool {
            guard value != savedValue else { return false }
            savedValue = value
            return true
        }
    }
}

extension View {
    /// Adds a modifier for this view that fires an action when a specific value changes.
    ///
    /// You can use `onChange` to trigger a side effect as the result of a value changing, such as an Environment key or a Binding.
    ///
    /// `onChange` is called on the main thread. Avoid performing long-running tasks on the main thread. If you need to perform a long-running task in response to value changing, you should dispatch to a background queue.
    ///
    /// The new value is passed into the closure. The previous value may be captured by the closure to compare it to the new value. For example, in the following code example, PlayerView passes both the old and new values to the model.
    ///
    /// ```
    /// struct PlayerView : View {
    ///   var episode: Episode
    ///   @State private var playState: PlayState
    ///
    ///   var body: some View {
    ///     VStack {
    ///       Text(episode.title)
    ///       Text(episode.showTitle)
    ///       PlayButton(playState: $playState)
    ///     }
    ///   }
    ///   .onChange(of: playState) { [playState] newState in
    ///     model.playStateDidChange(from: playState, to: newState)
    ///   }
    /// }
    /// ```
    ///
    /// - Parameters:
    ///   - value: The value to check against when determining whether to run the closure.
    ///   - action: A closure to run when the value changes.
    ///   - newValue: The new value that failed the comparison check.
    /// - Returns: A modified version of this view
    func onChange<Value: Equatable>(of value: Value, perform action: @escaping (_ newValue: Value)->Void) -> ChangeObserver<Self, Value> {
        ChangeObserver(base: self, value: value, action: action)
    }
}
Comment

detect binding valu change swiftui

struct TypewriterTextView: View {
    @Binding var textString:String
    @State private var typingInterval = 0.3
    @State private var typedString = ""

    var body: some View {
        Text(typedString).onChange(of: textString) {
            typedString = ""
            Timer.scheduledTimer(withTimeInterval: self.typingInterval, repeats: true, block: { timer in

                if self.typedString.length < self.textString.length {
                    self.typedString = self.typedString + self.textString[self.typedString.length]
                }
                else { timer.invalidate() }
            })
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Swift :: swift http request 
Swift :: how to select but not focus textfield swift 
Swift :: check when webview finish loading swift 
Swift :: swipe left to go back iphone swift 
Swift :: use of map instad of for loop 
Swift :: fizzbuzz in swift 
Swift :: swift get day from available string 
Swift :: swift get max of two numbers 
Swift :: get index filter swift 
Swift :: swiftui text field 
Swift :: swift create array from range 
Swift :: swift set uicolor 
Swift :: IOS create UIAlertViewController programmatically 
Swift :: an in swift 
Swift :: uitableview set space between cells 
Swift :: NumberFormatter swift 
Swift :: create class swift 
Swift :: swift change label text 
Swift :: swift dictionary get key from valye 
Swift :: add months to date swift 
Swift :: convert image to base64 swift Ui 
Swift :: swift uilabel dynamic height based on text length 
Swift :: Create a Swift Array 
Swift :: swift enum storyboard 
Swift :: Swiftui run action before navigationlink inside on button 
Swift :: Swift Closure as function parameter 
Swift :: compactMap 
Swift :: Swift Nested Function with Parameters 
Swift :: swift ranges 
Swift :: swift view controller background image 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =