swiftUI转换:从某个框架缩放-就像打开应用程序时iOS Homescreen所做的一样 [英] 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 Homescreen所做的一样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!