let fromImage: MTIImage
let toImage: MTIImage
let effect: MTTransition
let duration: Double
@Binding var showToImage: Bool
func makeUIView(context: Context) -> MTIImageView {
let imageView = MTIImageView()
imageView.image = fromImage
imageView.contentMode = .scaleAspectFill
return imageView
}
func updateUIView(_ uiView: MTIImageView, context: Context) {
guard showToImage else { return }
effect.duration = duration
effect.transition(from: fromImage, to: toImage, updater: { image in
DispatchQueue.main.async {
uiView.image = image
}
}, completion: { _ in
DispatchQueue.main.async {
uiView.image = toImage
}
})
}
}
< /code>
struct MetalWaveTransitionView: View {
@State private var fromImage: MTIImage?
@State private var toImage: MTIImage?
@State private var showToImage = false
private let transitionDuration: TimeInterval = 1.5
private let transition = MTCrossWarpTransition() // also tried other MTTransition types
var body: some View {
ZStack {
if let from = fromImage, let to = toImage {
MetalImageTransitionView(
fromImage: from,
toImage: to,
effect: transition,
duration: transitionDuration,
showToImage: $showToImage
)
}
}
.onAppear {
loadImages()
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
showToImage = true
}
}
}
func loadImages() {
if let from = UIImage(named: "imgCals"),
let to = UIImage(named: "imgBMI") {
fromImage = MTIImage(cgImage: from.cgImage!, options: [.SRGB: false], isOpaque: true)
toImage = MTIImage(cgImage: to.cgImage!, options: [.SRGB: false], isOpaque: true)
}
}
}
< /code>
Problem:
- .none transition works fine.
- Any other MTTransition subclass (like MTCrossWarpTransition) does not animate.
- The updater closure is called, but no visible transition happens.
- Images are Metal-compatible (using cgImage with isOpaque: true and .SRGB: false).
How can I correctly implement MTTransitions in SwiftUI with UIViewRepresentable, such that the image transition animations actually play for effects like MTCrossWarpTransition, MTZoomTransition, etc.?
what i want
click here to view
Подробнее здесь: https://stackoverflow.com/questions/797 ... n-only-non
Мобильная версия