У меня есть два представления коллекции, и я пытаюсь создать перетаскивание между двумя представлениями коллекции, и первый вариант представления коллекции между перетаскиванием.Я не могу создать, Вот мой код Пожалуйста, дайте мне решение.
У меня есть две коллекции. Один вид - collection_chess, а второй - collection_bottom.Я хочу перейти от второго к первому.
Я нашел один лучший пример KDDragAndDropCollectionView , но проблема в том, что при перетаскивании из первой коллекции в представление второй коллекции создается анимация, и у меня нет требований.анимация, такая как создание места для новой карты, и мне не нужна эта анимация
import UIKit
class ThirdViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
@IBOutlet var collection_chess: UICollectionView!
@IBOutlet var collection_bottom: UICollectionView!
var originalIndexPath:IndexPath? = nil
var draggingIndexPath:IndexPath? = nil
var draggingView: UIView?
var dragOffset: CGPoint = CGPoint(x: 0 , y: 0)
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
//-----------------------------------------------------------------------------------
// MARK: - collection DataSource & Delegate
//-----------------------------------------------------------------------------------
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if(collectionView == self.collection_chess)
{
return 64
}else
{
return 10
}
}
// make a cell for each cell index path
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if(collectionView == self.collection_chess)
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "chessCell", for: indexPath as IndexPath) as! chessCell
cell.lbl_name.text = String(indexPath.row+1)
if indexPath.row % 2 == 0{
cell.backgroundColor = UIColor.white
}else {
cell.backgroundColor = UIColor.red
}
return cell
}else
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "chessimgCell", for: indexPath as IndexPath) as! chessimgCell
cell.lbl_name.text = String(indexPath.row+1)
cell.backgroundColor = UIColor.red
return cell
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.size.width/8, height: collectionView.frame.size.width/8 )
}
public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
}
// public func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
//
// }
// public func collectionView(_ collectionView: UICollectionView, canMoveItemAt indexPath: IndexPath) -> Bool {
// return false
// }
@IBAction func longPressGestureChanged(recognizer: UILongPressGestureRecognizer) {
let globalLocation = recognizer.location(in: view)
if self.collection_chess.frame.contains(globalLocation) {
print("yes")
let location = recognizer.location(in: collection_chess!)
switch recognizer.state {
case .began: startDragAtLocation(location: location)
case .changed: updateDragAtLocation(location: location)
case .ended: endDragAtLocation(location: location)
default:
break
}
} else if collection_bottom.frame.contains(globalLocation) {
print("NO")
} else {
//you do not cover any of collection views
}
}
//...Handling the Start of a Drag
func startDragAtLocation(location: CGPoint) {
guard let cv = self.collection_chess else {
return
}
guard let indexPath = cv.indexPathForItem(at: location) else {
//print("This is return1 ----->")
return }
// guard cv.dataSource?.collectionView?(cv, canMoveItemAt: indexPath) == true else {
// //print("This is return2 ----->")
// return
//
// }
guard let cell = cv.cellForItem(at: indexPath) else {
return }
originalIndexPath = indexPath
draggingIndexPath = indexPath
draggingView = cell.snapshotView(afterScreenUpdates: true)
draggingView!.frame = cell.frame
//cv.addSubview(draggingView!)
self.view.addSubview(draggingView!)
dragOffset = CGPoint(x: draggingView!.center.x - location.x, y:draggingView!.center.y - location.y)
//dragOffset = CGPoint(x: draggingView!.center.x - self.view.frame.origin.x, y:draggingView!.center.y - self.view.frame.origin.y)
draggingView?.layer.shadowPath = UIBezierPath(rect: draggingView!.bounds).cgPath
draggingView?.layer.shadowColor = UIColor.black.cgColor
draggingView?.layer.shadowOpacity = 0.8
draggingView?.layer.shadowRadius = 10
// invalidateLayout()
UIView.animate(withDuration: 0.4, delay: 0, usingSpringWithDamping: 0.4, initialSpringVelocity: 0, options: [], animations: {
self.draggingView?.alpha = 0.95
self.draggingView?.transform = CGAffineTransform(scaleX: 1.2, y: 1.2)
}, completion: nil)
}
func updateDragAtLocation(location: CGPoint) {
guard let view = draggingView else {
print("This is return2 ----->")
return }
guard let cv = collection_chess else {
print("This is return3 ----->")
return }
view.center = CGPoint(x: location.x + dragOffset.x , y: location.y + dragOffset.y )
// if let newIndexPath = cv.indexPathForItem(at: location) {
// cv.moveItem(at: draggingIndexPath!, to: newIndexPath)
// draggingIndexPath = newIndexPath
// }
}
func endDragAtLocation(location: CGPoint) {
print("This is end ----",originalIndexPath?.row)
print(" ----",draggingIndexPath?.row)
guard let dragView = draggingView else { return }
guard let indexPath = draggingIndexPath else { return }
guard let cv = collection_bottom else { return }
guard let datasource = cv.dataSource else { return }
let targetCenter = datasource.collectionView(cv, cellForItemAt: indexPath).center
let shadowFade = CABasicAnimation(keyPath: "shadowOpacity")
shadowFade.fromValue = 0.8
shadowFade.toValue = 0
shadowFade.duration = 0.4
dragView.layer.add(shadowFade, forKey: "shadowFade")
UIView.animate(withDuration: 0.4, delay: 0, usingSpringWithDamping: 0.4, initialSpringVelocity: 0, options: [], animations: {
dragView.center = targetCenter
//dragView.transform = CGAffineTransformIdentity
}) { (completed) in
if indexPath != self.originalIndexPath! {
// datasource.collectionView ?? <#default value#>(cv, moveItemAt: self.originalIndexPath!, to: indexPath)
//datasource.collectionView?(cv, moveItemAtIndexPath: self.originalIndexPath!, toIndexPath: indexPath)
}
dragView.removeFromSuperview()
self.draggingIndexPath = nil
self.draggingView = nil
//self.invalidateLayout()
}
}
}