我正在尝试使用 SwiftUI 重新创建 iOS 11/12 App Store。 假设“故事”是点击卡片时显示的 View 。
我已经完成了卡片,但我现在遇到的问题是如何制作动画以显示“故事”。
由于我不擅长解释,这里有一个 gif:
我曾想过将整个卡片制作成 PresentationLink,但“故事”显示为模式,因此它不会覆盖整个屏幕,也不会执行我想要的动画。
最相似的是 NavigationLink,但是这迫使我添加一个 NavigationView,并且卡片显示为另一个页面。
我实际上不关心它是 PresentationLink 还是 NavigationLink 或其他任何东西,只要它执行动画并显示“故事”即可。
提前致谢。
我的代码:
Card.swift
struct Card: View {
var icon: UIImage = UIImage(named: "flappy")!
var cardTitle: String = "Welcome to \nCards!"
var cardSubtitle: String = ""
var itemTitle: String = "Flappy Bird"
var itemSubtitle: String = "Flap That!"
var cardCategory: String = ""
var textColor: UIColor = UIColor.white
var background: String = ""
var titleColor: Color = .black
var backgroundColor: Color = .white
var body: some View {
VStack {
if background != "" {
Image(background)
.resizable()
.frame(width: 380, height: 400)
.cornerRadius(20)
} else {
RoundedRectangle(cornerRadius: 20)
.frame(width: 400, height: 400)
.foregroundColor(backgroundColor)
}
VStack {
HStack {
VStack(alignment: .leading) {
if cardCategory != "" {
Text(verbatim: cardCategory.uppercased())
.font(.headline)
.fontWeight(.heavy)
.opacity(0.3)
.foregroundColor(titleColor)
//.opacity(1)
}
HStack {
Text(verbatim: cardTitle)
.font(.largeTitle)
.fontWeight(.heavy)
.lineLimit(3)
.foregroundColor(titleColor)
}
}
Spacer()
}.offset(y: -390)
.padding(.bottom, -390)
HStack {
if cardSubtitle != "" {
Text(verbatim: cardSubtitle)
.font(.system(size: 17))
.foregroundColor(titleColor)
}
Spacer()
}
.offset(y: -50)
.padding(.bottom, -50)
}
.padding(.leading)
}.padding(.leading).padding(.trailing)
}
}
所以
Card(cardSubtitle: "Welcome to this library I made :p", cardCategory: "CONNECT", background: "flBackground", titleColor: .white)
最佳答案
SwiftUI 目前不执行自定义模式转换,因此我们必须使用变通方法。
我能想到的一种方法是使用 ZStack
自己进行演示。可以使用 GeometryReader
获取源帧。然后,可以使用框架和位置修饰符控制目标形状。
开始时,目标将被设置为与源的位置和大小完全匹配。紧接着,目的地将在动画 block 中设置为全屏大小。
struct ContentView: View {
@State var isPresenting = false
@State var isFullscreen = false
@State var sourceRect: CGRect? = nil
var body: some View {
ZStack {
GeometryReader { proxy in
Button(action: {
self.isFullscreen = false
self.isPresenting = true
self.sourceRect = proxy.frame(in: .global)
}) { ... }
}
if isPresenting {
GeometryReader { proxy in
ModalView()
.frame(
width: self.isFullscreen ? nil : self.sourceRect?.width ?? nil,
height: self.isFullscreen ? nil : self.sourceRect?.height ?? nil)
.position(
self.isFullscreen ? proxy.frame(in: .global).center :
self.sourceRect?.center ?? proxy.frame(in: .global).center)
.onAppear {
withAnimation {
self.isFullscreen = true
}
}
}
}
}
.edgesIgnoringSafeArea(.all)
}
}
extension CGRect {
var center : CGPoint {
return CGPoint(x:self.midX, y:self.midY)
}
}
关于ios - SwiftUI 中的自定义模式转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57005230/