• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

swiftUI 转换:从某个框架缩放 - 就像 iOS 主屏幕在打开应用程序时所做的那样

用户头像
it1352
帮助1

问题说明

如何在翻译中获得 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.

正确答案

#1

这里有一个关于如何通过组合过渡实现这种效果的演示...(为了演示的简单性,位置和大小被硬编码 - 它们可以通过几何阅读器,对齐指南或锚点偏好,实际上不影响过渡使用思路,也可以配置动画)

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()
    }
}

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcfchka
系列文章
更多 icon
同类精品
更多 icon
继续加载