我想用不同的延迟让动物眨眼 Int.random(in: 1..<9)
该 问题 是。
有解决办法 UIView
动画和异步线程延迟......。但是有什么方法可以让 UIImage.animatedImage
使闪烁之间有延迟?
如果完全没有方法--如何解决我的问题?
代码。
所有动画的文件
import SwiftUI
struct AnimalAnimation: UIViewRepresentable {
@Binding var animalImages: [UIImage?]
@Binding var size: CGRect
func makeUIView(context: Self.Context) -> UIView {
let someView = UIView(frame: CGRect(x: size.minX, y: size.minY , width: size.width, height: size.height))
let someImage = UIImageView(frame: CGRect(x: 0, y: 0, width: size.width, height: size.height))
let animated = UIImage.animatedImage(with: animalImages as! [UIImage], duration: 0.9)!
someImage.clipsToBounds = true
someImage.autoresizesSubviews = true
someImage.contentMode = UIView.ContentMode.scaleAspectFit
someImage.image = animated
someView.addSubview(someImage)
return someView
}
func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<AnimalAnimation>) {
}
}
我用的是这个 ContentView
's body
VStack {
AnimalAnimation(animalImages: $animal1Array, size: $animal1Size)
}
的图像。
animalImages = [UIImage(named: "eyes-open"),UIImage(named: "eyes-closed")]
这里是SwiftUI的唯一变体。用Xcode 11.4 iOS 13.4测试。(Sizesframes,如果需要可以配置,演示它并不重要)
struct EyesBlinkView: View {
enum EyesState: String {
case open = "eyes-open"
case closed = "eyes-closed"
}
@State private var eyesState = EyesState.open
var body: some View {
Image(eyesState.rawValue).resizable().scaledToFit()
.onAppear { self.blink() }
}
private func blink() {
self.eyesState = .closed
DispatchQueue.main.asyncAfter(deadline: .now() + 0.9) {
self.eyesState = .open
DispatchQueue.main.asyncAfter(deadline:
DispatchTime.now().advanced(by: .seconds(Int.random(in: 1..<9)))) {
self.blink()
}
}
}
}
你为什么不在数组中再次添加相同的图像,使其 "看起来 "像一个延迟?