最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Disable animations of SwiftUI ProgressView with CircularProgressViewStyle - Stack Overflow

programmeradmin5浏览0评论

For snapshot testing purposes I need to disable the animation of a SwiftUI ProgressView with CircularProgressViewStyle...

            ProgressView()
                .progressViewStyle(CircularProgressViewStyle())

Does anyone know how to do this please? I have already tried the following which do not work...

Doesn't Work
            ProgressView()
                .progressViewStyle(CircularProgressViewStyle())
                .disabled(true)
Doesn't Work
            ProgressView()
                .progressViewStyle(CircularProgressViewStyle())
                .animation(nill)
Doesn't Work
            ProgressView()
                .progressViewStyle(CircularProgressViewStyle())
                .transaction { transaction in
                    transaction.animation = nil
                }
Doesn't Work
            ProgressView(value: 0, total: 100)
                .progressViewStyle(CircularProgressViewStyle())
                .transaction { transaction in
                    transaction.animation = nil
                }

For snapshot testing purposes I need to disable the animation of a SwiftUI ProgressView with CircularProgressViewStyle...

            ProgressView()
                .progressViewStyle(CircularProgressViewStyle())

Does anyone know how to do this please? I have already tried the following which do not work...

Doesn't Work
            ProgressView()
                .progressViewStyle(CircularProgressViewStyle())
                .disabled(true)
Doesn't Work
            ProgressView()
                .progressViewStyle(CircularProgressViewStyle())
                .animation(nill)
Doesn't Work
            ProgressView()
                .progressViewStyle(CircularProgressViewStyle())
                .transaction { transaction in
                    transaction.animation = nil
                }
Doesn't Work
            ProgressView(value: 0, total: 100)
                .progressViewStyle(CircularProgressViewStyle())
                .transaction { transaction in
                    transaction.animation = nil
                }
Share asked Nov 28, 2024 at 10:41 Oliver PearmainOliver Pearmain 20.6k13 gold badges93 silver badges94 bronze badges 1
  • If none of those work then you probably cannot disable it with SwiftUI. What does "snapshot testing purposes" mean? Do you have a flag or something that indicates whether you are testing? I would just use a static Image to replace/cover the ProgressView. – Sweeper Commented Nov 28, 2024 at 10:46
Add a comment  | 

1 Answer 1

Reset to default 0

Given that this is for testing, it would be convenient and desirable to disable all the progress views globally by swizzling UIActivityIndicatorView.startAnimating. You would just do the swizzling in the setUp method of your tests.

extension UIActivityIndicatorView {
    @objc func swizzledStartAnimating() {
        hidesWhenStopped = false
    }
    
    static nonisolated func disableAnimations() {
        let originalSelector = #selector(UIActivityIndicatorView.startAnimating)
        let swizzledSelector = #selector(UIActivityIndicatorView.swizzledStartAnimating)
        let originalMethod = class_getInstanceMethod(self, originalSelector)!
        let swizzledMethod = class_getInstanceMethod(self, swizzledSelector)!
        method_exchangeImplementations(originalMethod, swizzledMethod)
    }
}

let disableProgressView: () = UIActivityIndicatorView.disableAnimations()

I am calling UIActivityIndicatorView.disableAnimations() in the initialiser of a global property. This makes sure that it is called exactly once, even if the property's getter is called multiple times. In the setUp of your tests, you just need to write

_ = disableProgressView

to call the property getter.

Alternatively, use SwiftUI-Introspect to access the underlying UIActivityIndicatorView:

ProgressView().progressViewStyle(.circular)
    .introspect(.progressView(style: .circular), on: .iOS(.v14, .v15, .v16, .v17, .v18)) {
        $0.hidesWhenStopped = false
        $0.stopAnimating()
    }

You will need to add all the versions of iOS that you want to introspect on.


Of course, all of this is based on the assumption that SwiftUI's circular progress view is implemented by UIActivityIndicatorView. This is the case all the way from iOS 14 when ProgressView was first added to SwiftUI. If this becomes false in the future, that means ProgressView is now implemented natively in SwiftUI, in which case one or more of the attempts shown in the question would work.

发布评论

评论列表(0)

  1. 暂无评论