如何将ImageView拖入Swift中的CollectionView和CollectionView Item中,将其拖入ImageView,如tinder App profile edit

时间:2018-06-09 09:25:38

标签: ios drag-and-drop uicollectionview uiimageview uicollectionviewcell

我是iOS中的新手,我不知道该怎么做,我想实现像配置文件编辑动画这样的好处。

例如,

我可以将图片拖到其他UIImageViewUICollectionView,将收藏品拖到主UIImageView

我实现了LongGestureListener UICollectionView但我只能在UICollectionView项目之间拖放项目。

先谢谢。

1 个答案:

答案 0 :(得分:1)

快速4.2

  • 图像来自上一个控制器

导入UIKit

private let restartIdentifier =“单元格”

MyRoomCollectionViewController类:UICollectionViewController,UICollectionViewDelegateFlowLayout {

var images:[UIImage]=[]
var screenSize: CGRect!
var screenWidth: CGFloat!
fileprivate var longPressGesture: UILongPressGestureRecognizer!

override func viewDidLoad() {
    super.viewDidLoad()

    collectionView.allowsMultipleSelection=true

    screenSize = UIScreen.main.bounds
    screenWidth = screenSize.width-6

    let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    layout.sectionInset = UIEdgeInsets(top: 10, left: 1, bottom: 10, right: 1)
    layout.itemSize = CGSize(width: screenWidth/3, height: screenWidth/3)
    layout.minimumInteritemSpacing = 1.5
    layout.minimumLineSpacing = 1.5
    collectionView!.collectionViewLayout = layout

    longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(self.handleLongGesture(gesture:)))
    collectionView.addGestureRecognizer(longPressGesture)
}
override func numberOfSections(in collectionView: UICollectionView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}


override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of items
    return images.count
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! RoomCollectionViewCell

    cell.img.image=images[indexPath.row]

    return cell
}

override func collectionView(_ collectionView: UICollectionView, canMoveItemAt indexPath: IndexPath) -> Bool
{
    return true
}
override func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath)
{
    //print("Starting Index: \(sourceIndexPath.item)")
    //print("Ending Index: \(destinationIndexPath.item)")
    let temp=images[sourceIndexPath.item]
    images.remove(at: sourceIndexPath.item)
    images.insert(temp, at: destinationIndexPath.item)
    self.collectionView.reloadData()
}
@objc func handleLongGesture(gesture: UILongPressGestureRecognizer) {
    switch(gesture.state) {

    case .began:
        guard let selectedIndexPath = collectionView.indexPathForItem(at: gesture.location(in: collectionView)) else {
            break
        }
        collectionView.beginInteractiveMovementForItem(at: selectedIndexPath)
    case .changed:
        collectionView.updateInteractiveMovementTargetPosition(gesture.location(in: gesture.view!))
    case .ended:
        collectionView.endInteractiveMovement()
    default:
        collectionView.cancelInteractiveMovement()
    }
}
@IBAction func arrangeIt(_ sender: Any) {
    performSegue(withIdentifier: "videosegue", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let dest=segue.destination as! VideoMakerViewController
    dest.images=images
}

}