小编Gwo*_*ozi的帖子

SwiftUI 的 AnimatableModifier 现已弃用,但如何“直接使用 Animatable”呢?

我的目标是在偏移更改动画完成时实现回调。因此,我在网上找到了一种解决方法,它使用 来AnimatableModifier检查何时animatableData等于目标值。

struct OffsetAnimation: AnimatableModifier{
    typealias T = CGFloat
    var animatableData: T{
        get { value }
        set {
            value = newValue
            print("animating \(value)")
            if watchForCompletion && value == targetValue {
                DispatchQueue.main.async { [self] in onCompletion() }
            }
        }
    }
    var watchForCompletion: Bool
    var value: T
    var targetValue: T
    init(value: T, watchForCompletion: Bool, onCompletion: @escaping()->()){
        self.targetValue = value
        self.value = value
        self.watchForCompletion = watchForCompletion
        self.onCompletion = onCompletion
    }
    var onCompletion: () -> ()
    func body(content: Content) -> …
Run Code Online (Sandbox Code Playgroud)

animation deprecated ios swiftui

14
推荐指数
1
解决办法
2302
查看次数

标签 统计

animation ×1

deprecated ×1

ios ×1

swiftui ×1