admin管理员组文章数量:1331653
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
|
1 Answer
Reset to default 0Given 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.
本文标签: Disable animations of SwiftUI ProgressView with CircularProgressViewStyleStack Overflow
版权声明:本文标题:Disable animations of SwiftUI ProgressView with CircularProgressViewStyle - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742267000a2443591.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
Image
to replace/cover theProgressView
. – Sweeper Commented Nov 28, 2024 at 10:46