swiftUI 转换:从某个框架缩放 - 就像 iOS 主屏幕在打开应用程序时所做的那样 [英] swiftUI transitions: Scale from some frame - like iOS Homescreen is doing when opening an App
问题描述
如何在翻译中获得 iOS 主屏幕在打开应用程序时的效果:从主屏幕上的应用程序图标开始将应用程序缩放到全屏?
How do I get the effect in a translation, that iOS home screen does when opening an App: It scales the App to fullscreen, starting from the App-icon on the home screen?
在我的代码中,我有带有位置、宽度和高度的图标框架 (CGRect
),我有最后的框架.有没有办法(可能结合一些)转换来获得从图标框架到最终框架的缩放?
In my code, I have the icon-frame (CGRect
) with position, width and height and I have the final frame. Is there a way to (probably combine some) transitions to get a scaling from the icon-frame to the final frame?
我有点类似:
view.transition(AnyTransition.scale(scale: 0, anchor: UnitPoint.trailing))
从尾随中心位置开始,从零缩放到原始大小.它只是接近:
Which scales from zero to the original size, starting from trailing center position. It's only close as:
- 它从零开始缩放,而不是原始图标的大小
- 它从一个固定点(尾部、中心)开始,但我想让它从图标所在的位置开始.
可以肯定的是:它必须是一个过渡,因为视图是新创建和删除的.我尝试保持视图并更改其不透明度以显示/隐藏它.还有许多其他问题,例如当视图消失时无法获得反向动画.
Just to be sure: It must be a transition as the view is newly created and dropped. I tried with keeping the view and just change its opacity to show/hide it. With many other problems like not getting the reverse animation, when the view disappears.
推荐答案
这里有一个关于如何通过组合过渡实现这种效果的演示...(为了演示的简单性,位置和大小被硬编码 - 它们可以通过几何阅读器,对齐指南或锚点偏好,实际上不影响过渡使用思路,也可以配置动画)
Here is a demo of idea how such effect could be done with combined transitions... (positions and sizes are hardcoded for demo simplicity - they can be read with geometry reader, alignment guides or anchor preferences, and actually do not affect the transition usage idea, also animation can be configured)
演示:
struct TestRisingView: View {
let screen = UIScreen.main.bounds
@State var showingView = false
@State var btFrame: CGRect = .zero
var body: some View {
GeometryReader { g in
ZStack {
Rectangle().fill(Color.clear)
self.activatingButton(frame: CGRect(x: 80, y: 30, width: 60, height: 40))
self.activatingButton(frame: CGRect(x: self.screen.maxX - 80, y: 30, width: 60, height: 40))
self.activatingButton(frame: CGRect(x: self.screen.maxX - 80, y: self.screen.maxY - 60, width: 60, height: 40))
self.activatingButton(frame: CGRect(x: 80, y: self.screen.maxY - 60, width: 60, height: 40))
if self.showingView {
self.topView
.zIndex(1)
.transition(
AnyTransition.scale(scale: 0.12).combined(with:
AnyTransition.offset(x: self.btFrame.origin.x - g.size.width/2.0,
y: self.btFrame.origin.y - g.size.height/2.0))
)
}
}
}
}
func activatingButton(frame: CGRect) -> some View {
Button(action: {
withAnimation {
self.btFrame = frame
self.showingView.toggle()
}
}) {
Text("Tap")
.padding()
.background(Color.yellow)
}
.position(frame.origin)
}
var topView: some View {
Rectangle()
.fill(Color.green)
.frame(width: 300, height: 400)
}
}
struct TestRisingView_Previews: PreviewProvider {
static var previews: some View {
TestRisingView()
}
}
这篇关于swiftUI 转换:从某个框架缩放 - 就像 iOS 主屏幕在打开应用程序时所做的那样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!