这个想法是重新创建相同的照片布局行为,就像在苹果照片库时,我可以放大和缩小1,3或5张照片在一排。我在半路上。为此,我使用MagnificationGesture()
,并根据手势值更新LazyVGrid()
中GridItems()
的数量。
请让我知道如何实现它。多谢了🙏
代码如下:
import SwiftUI
struct ContentView: View {
let colors: [Color] = [.red, .purple, .yellow, .green, .blue, .mint, .orange]
@State private var colums = Array(repeating: GridItem(), count: 1)
// @GestureState var magnifyBy: CGFloat = 1.0
@State var magnifyBy: CGFloat = 1.0
@State var lastMagnifyBy: CGFloat = 1.0
let minMagnifyBy = 1.0
let maxMagnifyBy = 5.0
var magnification: some Gesture {
MagnificationGesture()
// .updating($magnifyBy) { (currentState, pastState, trans) in
// pastState = currentState.magnitude
// }
.onChanged { state in
adjustMagnification(from: state)
print("Current State \(state)")
}
.onEnded { state in
adjustMagnification(from: state)
// withAnimation(.spring()) {
// validateMagnificationLimits()
// }
lastMagnifyBy = 1.0
}
}
var body: some View {
NavigationView {
ScrollView {
LazyVGrid(columns: colums) {
ForEach(1..<101) { number in
colors[number % colors.count]
.overlay(Text("\(number)").font(.title2.bold()).foregroundColor(.white))
.frame(height: 100)
}
}
.scaleEffect(magnifyBy)
.gesture(magnification)
.navigationTitle("🧨 Grid")
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Button {
withAnimation(.spring(response: 0.8)) {
colums = Array(repeating: .init(), count: colums.count == 5 ? 1 : colums.count % 5 + 2)
}
} label: {
Image(systemName: "square.grid.3x3")
.font(.title2)
.foregroundColor(.primary)
}
}
}
}
}
}
private func adjustMagnification(from state: MagnificationGesture.Value) {
let stepCount = Int(min(max(1, state), 5))
// let delta = state / lastMagnifyBy
// magnifyBy *= delta
withAnimation(.linear) {
colums = Array(repeating: GridItem(), count: stepCount)
}
lastMagnifyBy = state
}
private func getMinMagnificationAllowed() -> CGFloat {
max(magnifyBy, minMagnifyBy)
}
private func getMaxMagnificationAllowed() -> CGFloat {
min(magnifyBy, maxMagnifyBy)
}
private func validateMagnificationLimits() {
magnifyBy = getMinMagnificationAllowed()
magnifyBy = getMaxMagnificationAllowed()
}
}
2条答案
按热度按时间xbp102n01#
给你这使用了TrackableScrollView(代码中的git链接)。我实现了一个可能的zoomStages数组(每行cols),以使它们之间的切换更容易。
dos旁边的按钮将滚动回放大中心,因此同一项目保持焦点。也许一个不透明的过渡,而不是重新安排网格。Have fun ;)
cx6n0qe32#
如果你想模仿照片应用程序在放大/捏时的行为,你必须考虑:
1.当前规模
1.根据方向移动到下一个布局所需的比例/缩放因子
Example project (SwiftUI without third-party libraries)