ios - 如何在 Swift 中将 ImageView 拖入 CollectionView 并将 CollectionView 项拖入 ImageView,如 tinder App 配置文件编辑
问题描述
我是 iOS 的新手,我不知道该怎么做,我想实现 tinder 之类的配置文件编辑动画。
例如,
我可以将图像拖到 otherUIImageView
或UICollectionView
Collection Item 到 Main UIImageView
。
我有实现LongGestureListener
但UICollectionView
我只能在 UICollectionView 项目之间拖放项目。
提前致谢。
解决方案
对于 Swift 4.2
图片来自上一个控制器
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 } }
推荐阅读
- asp.net - Sendgrid - 提供的授权授权无效、过期或撤销
- api - Viber 聊天 API,开发 Viber 聊天应用程序以支持深色主题
- azure - 使用 Azure AD 帐户将 Azure Logic App 连接到 SQL
- google-apps-script - 当单元格为空时,Google 表格应用脚本 onEdit 设置公式
- javascript - 如何使用本地存储存储最近提交的选项卡或刷新选项卡
- c# - 如何修改现有的 VB.NET 类以传递附加变量
- sql - 如何根据联接结果更新表?
- java - 为什么快速 java 文档的悬停不起作用?
- sql - 集成 Where、Group By 和 Order By 子句?
- c - 如何退出中断程序并返回主程序?