pushdeer/ios/PushDeer-iOS/PushDeer/View/Common/DeletableView.swift

80 lines
1.9 KiB
Swift
Raw Normal View History

2021-12-27 22:33:27 +08:00
//
// DeletableView.swift
// PushDeer
//
// Created by HEXT on 2021/12/26.
//
import SwiftUI
/// View
struct DeletableView<Content : View> : View {
/// View
@ViewBuilder let contentView: Content
///
let deleteAction : () -> ()
/// x,
private let offsetMaxX = 80.0
@State private var offsetX = 0.0
@State private var isShowDelete = false
var body: some View {
ZStack {
HStack {
Spacer()
Button(action: {
print("点击删除")
withAnimation(.easeOut) {
deleteAction()
}
}, label: {
Image(systemName: "trash")
.imageScale(.large)
.foregroundColor(Color.red)
.padding()
})
}
contentView
.offset(x: offsetX, y: 0)
.gesture(
DragGesture()
.onChanged({ value in
let width = value.translation.width
print("onChanged", width)
let endX = isShowDelete ? offsetMaxX : 0.0
if width < endX {
offsetX = width - endX
} else {
offsetX = 0
}
})
.onEnded({ value in
withAnimation(.easeOut) {
let width = value.translation.width
print("onEnded", width)
if width > -(offsetMaxX/2) {
offsetX = 0
isShowDelete = false
} else {
offsetX = -offsetMaxX
isShowDelete = true
}
}
})
)
}
}
}
struct DeletableView_Previews: PreviewProvider {
static var previews: some View {
DeletableView(contentView: {
DeviceItemView(name: "未知设备")
}, deleteAction: {
})
}
}