Commit 78402c67 authored by CZ1004's avatar CZ1004

【优化】优化左右滑动功能

parent ab524572
{
"images" : [
{
"filename" : "Frame_1171276231.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "Frame_1171276231@2x.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "Frame_1171276231@3x.png",
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
{
"images" : [
{
"filename" : "Frame_1171276231.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "Frame_1171276231@2x.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "Frame_1171276231@3x.png",
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
{
"images" : [
{
"filename" : "Group_1171275102.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "Group_1171275102@2x.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "Group_1171275102@3x.png",
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
{
"images" : [
{
"filename" : "Group_1171275102.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "Group_1171275102@2x.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "Group_1171275102@3x.png",
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
{
"images" : [
{
"filename" : "Group_1171275102.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "Group_1171275102@2x.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "Group_1171275102@3x.png",
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
{
"images" : [
{
"filename" : "Group_1171275242.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "Group_1171275242@2x.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "Group_1171275242@3x.png",
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
{
"images" : [
{
"filename" : "Group_1171275243.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "Group_1171275243@2x.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "Group_1171275243@3x.png",
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
...@@ -9,6 +9,11 @@ import UIKit ...@@ -9,6 +9,11 @@ import UIKit
import SnapKit import SnapKit
import Photos import Photos
enum SlideDirection {
case left
case right
}
class PhotoRemoveViewController: BaseViewController { class PhotoRemoveViewController: BaseViewController {
var mediaType : TrashTypeEnum? { var mediaType : TrashTypeEnum? {
...@@ -43,6 +48,23 @@ class PhotoRemoveViewController: BaseViewController { ...@@ -43,6 +48,23 @@ class PhotoRemoveViewController: BaseViewController {
private let scaleMax: CGFloat = 0.7 private let scaleMax: CGFloat = 0.7
private let actionMargin: CGFloat = 120 private let actionMargin: CGFloat = 120
// 创建两个按钮
private lazy var trashButton = {
let button = UIButton(frame: CGRect(x: self.view.centerX - 22 - 47, y: self.view.centerY + 509 * RScreenH() / 2 - 47 / 2, width: 47, height: 47))
button.setImage(UIImage(named: "Group_1171275243"), for: .normal)
button.setImage(UIImage(named: "Group_1171275242"), for: .selected)
button.addTarget(self, action: #selector(trashButtonAction), for: .touchUpInside)
return button
}()
private lazy var keepListButton = {
let button = UIButton(frame: CGRect(x: self.view.centerX + 22, y: self.view.centerY + 509 * RScreenH() / 2 - 47 / 2, width: 47, height: 47))
button.setImage(UIImage(named: "Group_1171275102 3"), for: .normal)
button.setImage(UIImage(named: "Group_1171275102 2"), for: .selected)
button.addTarget(self, action: #selector(keepListButtonAction), for: .touchUpInside)
return button
}()
lazy var trashSubView : TrashSubView = { lazy var trashSubView : TrashSubView = {
let view = TrashSubView() let view = TrashSubView()
...@@ -68,11 +90,64 @@ class PhotoRemoveViewController: BaseViewController { ...@@ -68,11 +90,64 @@ class PhotoRemoveViewController: BaseViewController {
self.currentIndex = tempArray.count self.currentIndex = tempArray.count
self.dataModel = result self.dataModel = result
super.init(nibName: nil, bundle: nil) super.init(nibName: nil, bundle: nil)
} }
required init?(coder: NSCoder) { required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented") fatalError("init(coder:) has not been implemented")
} }
// MARK: - 按钮事件
@objc func trashButtonAction(){
if self.photoViews.count > 0 {
if let view = self.photoViews.first {
self.animateViewExit(view: view,direction: .left)
}
}
}
@objc func keepListButtonAction(){
if self.photoViews.count > 0 {
if let view = self.photoViews.first {
self.animateViewExit(view: view,direction: .right)
}
}
}
func animateViewExit(view : PhotosRemoveBaseView, direction : SlideDirection) {
// 1. 设置锚点
let customAnchor = CGPoint(x: 0.5, y: 2.0)
view.layer.anchorPoint = customAnchor
// 2. 修正位置
let originalCenter = view.center
let offsetY = view.bounds.height * (customAnchor.y - 0.5)
view.layer.position = CGPoint(
x: originalCenter.x,
y: originalCenter.y + offsetY
)
// 3. 执行动画
UIView.animate(withDuration: 0.2, delay: 0, options: .curveEaseIn, animations: {
view.transform = CGAffineTransform(rotationAngle: direction == SlideDirection.left ? -.pi/6 : .pi/6)
view.alpha = 0
}) { _ in
// 数据变化
if direction == SlideDirection.left{
// 删除操作,先存到单利
self.vibrate()
self.saveDataToDBAndSigtonTrash()
}else {
// 清除单利数据
self.clearSigtonTrashData()
// 保留操作
self.vibrate()
self.saveDataToSigtonKeepList()
}
// 视图变化
self.recycleView()
}
}
// MARK: - 生命周期 // MARK: - 生命周期
override func viewDidLoad() { override func viewDidLoad() {
...@@ -148,6 +223,10 @@ class PhotoRemoveViewController: BaseViewController { ...@@ -148,6 +223,10 @@ class PhotoRemoveViewController: BaseViewController {
showCurrentPageUIWhenTashDataChanged() showCurrentPageUIWhenTashDataChanged()
self.addListener() self.addListener()
self.view.addSubview(self.trashButton)
self.view.addSubview(self.keepListButton)
} }
// 拿到当前类型垃圾桶数据的最后一个 // 拿到当前类型垃圾桶数据的最后一个
...@@ -277,15 +356,13 @@ class PhotoRemoveViewController: BaseViewController { ...@@ -277,15 +356,13 @@ class PhotoRemoveViewController: BaseViewController {
self.navView.resetButton.isHidden = true self.navView.resetButton.isHidden = true
} }
} }
}
if assetModel.count > 0 {
if assetModel.count > 0 { // 显示垃圾桶
// 显示垃圾桶 self.trashSubView.resourceCountlabel.text = String(assetModel.count)
self.trashSubView.resourceCountlabel.text = String(assetModel.count) self.showTrashView()
self.showTrashView() }else{
}else{ self.hideTrashView()
self.hideTrashView()
}
} }
} }
} }
...@@ -297,7 +374,7 @@ class PhotoRemoveViewController: BaseViewController { ...@@ -297,7 +374,7 @@ class PhotoRemoveViewController: BaseViewController {
let photoView = PhotosRemoveBaseView() let photoView = PhotosRemoveBaseView()
photoView.mediaType = self.mediaType photoView.mediaType = self.mediaType
photoView.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(handlePan))) photoView.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(handlePan)))
view.addSubview(photoView) self.view.addSubview(photoView)
photoView.frame = CGRectMake(0, 0, self.view.width - 30, 509 * RScreenH()) photoView.frame = CGRectMake(0, 0, self.view.width - 30, 509 * RScreenH())
photoView.center = self.view.center photoView.center = self.view.center
photoViews.append(photoView) photoViews.append(photoView)
...@@ -435,7 +512,9 @@ class PhotoRemoveViewController: BaseViewController { ...@@ -435,7 +512,9 @@ class PhotoRemoveViewController: BaseViewController {
} }
} }
}else { }else {
view.configure(with: PhotoAndVideoMananger.mananger.getImageFromAssetID(id: dataModel[imageIndex].localIdentifier) ?? UIImage()) PhotoAndVideoMananger.mananger.asynGetImageFromAssetID(id: dataModel[imageIndex].localIdentifier) { image in
view.configure(with: image)
}
} }
} }
...@@ -446,10 +525,16 @@ class PhotoRemoveViewController: BaseViewController { ...@@ -446,10 +525,16 @@ class PhotoRemoveViewController: BaseViewController {
private func updateActionButton(for view: PhotosRemoveBaseView, translation: CGPoint) { private func updateActionButton(for view: PhotosRemoveBaseView, translation: CGPoint) {
if translation.x > 0 { if translation.x > 0 {
self.trashButton.isSelected = false
self.keepListButton.isSelected = true
view.showRightButton() view.showRightButton()
} else if translation.x < 0 { } else if translation.x < 0 {
self.trashButton.isSelected = true
self.keepListButton.isSelected = false
view.showLeftButton() view.showLeftButton()
} else { } else {
self.keepListButton.isSelected = false
self.trashButton.isSelected = false
view.hideButtons() view.hideButtons()
} }
} }
...@@ -478,6 +563,7 @@ class PhotoRemoveViewController: BaseViewController { ...@@ -478,6 +563,7 @@ class PhotoRemoveViewController: BaseViewController {
view.center = self.view.center view.center = self.view.center
view.hideButtons() view.hideButtons()
} }
initTrashAndKeepListButton()
} }
private func recycleView() { private func recycleView() {
...@@ -505,16 +591,23 @@ class PhotoRemoveViewController: BaseViewController { ...@@ -505,16 +591,23 @@ class PhotoRemoveViewController: BaseViewController {
// 视图层级处理 // 视图层级处理
bringCurrentViewToFront() bringCurrentViewToFront()
newView.alpha = 1
UIView.animate(withDuration: 0.3) { initTrashAndKeepListButton()
newView.alpha = 1 }
}
func initTrashAndKeepListButton(){
self.trashButton.isSelected = false
self.keepListButton.isSelected = false
self.view.bringSubviewToFront(self.trashButton)
self.view.bringSubviewToFront(self.keepListButton)
} }
private func bringCurrentViewToFront() { private func bringCurrentViewToFront() {
guard photoViews.count >= 2 else { return } guard photoViews.count >= 2 else { return }
view.bringSubviewToFront(photoViews[1]) view.bringSubviewToFront(photoViews[1])
view.bringSubviewToFront(photoViews[0]) view.bringSubviewToFront(photoViews[0])
view.bringSubviewToFront(self.trashButton)
view.bringSubviewToFront(self.keepListButton)
} }
......
...@@ -71,7 +71,8 @@ class PhotosRemoveBaseView: UIView { ...@@ -71,7 +71,8 @@ class PhotosRemoveBaseView: UIView {
imageView.contentMode = .scaleAspectFit imageView.contentMode = .scaleAspectFit
addSubview(imageView) addSubview(imageView)
leftButton.frame = CGRect(x: self.width / 2 - 45.5, y: self.height / 2 - 21, width: 95, height: 42) leftButton.frame = CGRect(x: 0, y: 0, width: 95, height: 42)
leftButton.center = imageView.center
leftButton.setTitle("Delete", for: .normal) leftButton.setTitle("Delete", for: .normal)
leftButton.backgroundColor = UIColor(red: 0.95, green: 0.21, blue: 0.21, alpha: 1) leftButton.backgroundColor = UIColor(red: 0.95, green: 0.21, blue: 0.21, alpha: 1)
leftButton.tintColor = .white leftButton.tintColor = .white
...@@ -81,7 +82,8 @@ class PhotosRemoveBaseView: UIView { ...@@ -81,7 +82,8 @@ class PhotosRemoveBaseView: UIView {
addSubview(leftButton) addSubview(leftButton)
rightButton.frame = CGRect(x: self.width / 2 - 45.5, y: self.height / 2 - 21, width: 95, height: 42) rightButton.frame = CGRect(x: 0, y: 0, width: 95, height: 42)
rightButton.center = imageView.center
rightButton.setTitle("Retain", for: .normal) rightButton.setTitle("Retain", for: .normal)
rightButton.backgroundColor = UIColor(red: 0.18, green: 0.76, blue: 0.35, alpha: 1) rightButton.backgroundColor = UIColor(red: 0.18, green: 0.76, blue: 0.35, alpha: 1)
rightButton.tintColor = .white rightButton.tintColor = .white
...@@ -99,7 +101,7 @@ class PhotosRemoveBaseView: UIView { ...@@ -99,7 +101,7 @@ class PhotosRemoveBaseView: UIView {
} }
func showLeftButton() { func showLeftButton() {
self.leftButton.center = self.center self.leftButton.center = imageView.center
UIView.animate(withDuration: 0.2) { UIView.animate(withDuration: 0.2) {
self.leftButton.alpha = 1 self.leftButton.alpha = 1
self.maskTempleteView.backgroundColor = UIColor(red: 0.95, green: 0.21, blue: 0.21, alpha: 0.4000) self.maskTempleteView.backgroundColor = UIColor(red: 0.95, green: 0.21, blue: 0.21, alpha: 0.4000)
...@@ -109,7 +111,7 @@ class PhotosRemoveBaseView: UIView { ...@@ -109,7 +111,7 @@ class PhotosRemoveBaseView: UIView {
} }
func showRightButton() { func showRightButton() {
self.rightButton.center = self.center self.rightButton.center = imageView.center
UIView.animate(withDuration: 0.2) { UIView.animate(withDuration: 0.2) {
self.rightButton.alpha = 1 self.rightButton.alpha = 1
self.maskTempleteView.backgroundColor = UIColor(red: 0.26, green: 0.78, blue: 0.41, alpha: 0.4000) self.maskTempleteView.backgroundColor = UIColor(red: 0.26, green: 0.78, blue: 0.41, alpha: 0.4000)
...@@ -119,12 +121,12 @@ class PhotosRemoveBaseView: UIView { ...@@ -119,12 +121,12 @@ class PhotosRemoveBaseView: UIView {
} }
func hideButtons() { func hideButtons() {
UIView.animate(withDuration: 0.2) { UIView.animate(withDuration: 0.2) { [self] in
self.leftButton.alpha = 0 self.leftButton.alpha = 0
self.rightButton.alpha = 0 self.rightButton.alpha = 0
self.maskTempleteView.removeFromSuperview() self.maskTempleteView.removeFromSuperview()
self.leftButton.center = self.center self.leftButton.center = self.imageView.center
self.rightButton.center = self.center self.rightButton.center = self.imageView.center
} }
} }
......
...@@ -296,6 +296,22 @@ class PhotoAndVideoMananger { ...@@ -296,6 +296,22 @@ class PhotoAndVideoMananger {
} }
} }
func asynGetImageFromAssetID(id: String,finised:@escaping (UIImage)->Void) {
if let asset = getPHAsssetwithID(ids: [id]) {
let imageManager = PHImageManager.default()
let targetSize = CGSize(width: 400, height: 400)
let options = PHImageRequestOptions()
options.isSynchronous = false
options.deliveryMode = .opportunistic
options.isNetworkAccessAllowed = true
imageManager.requestImage(for: asset, targetSize: targetSize, contentMode: .aspectFill, options: options) { result, _ in
finised(result ?? UIImage())
}
}else {
finised(UIImage())
}
}
func getImageFromAsset(asset: PHAsset) -> UIImage? { func getImageFromAsset(asset: PHAsset) -> UIImage? {
var image: UIImage? var image: UIImage?
let imageManager = PHImageManager.default() let imageManager = PHImageManager.default()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment