gpt4 book ai didi

ios - 如何将 ImageView 拖入 Swift 中的 CollectionView 并将 CollectionView Item 拖入 ImageView (如 Tinder 应用程序配置文件编辑)

转载 作者:行者123 更新时间:2023-12-01 22:41:27 24 4
gpt4 key购买 nike

我是 iOS 新手,我不知道该怎么做,我想实现类似个人资料编辑动画的 Tinder。

例如,

我可以将图像拖到其他 UIImageViewUICollectionView 中,并将 Collection Item 拖到 Main UIImageView 中。

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

提前致谢。

最佳答案

适用于 swift 4.2

  • 图像来自上一个 Controller

              import UIKit
    private let reuseIdentifier = "cell"
    class 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()
    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(handleLongGesture(gesture:)))
    collectionView.addGestureRecognizer(longPressGesture)
    }

    override func numberOfSections(in _: UICollectionView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
    }

    override func collectionView(_: UICollectionView, numberOfItemsInSection _: 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(_: UICollectionView, canMoveItemAt _: IndexPath) -> Bool {
    return true
    }

    override func 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)
    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(_: Any) {
    performSegue(withIdentifier: "videosegue", sender: self)
    }

    override func prepare(for segue: UIStoryboardSegue, sender _: Any?) {
    let dest = segue.destination as! VideoMakerViewController
    dest.images = images
    }
    }

关于ios - 如何将 ImageView 拖入 Swift 中的 CollectionView 并将 CollectionView Item 拖入 ImageView (如 Tinder 应用程序配置文件编辑),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50772776/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com