Swift下使用UICollectionView 實(shí)現(xiàn)長按拖拽功能
導(dǎo)讀
簡單用Swift寫了一個(gè)collectionview的拖拽點(diǎn)擊排序效果;
拖拽排序是新聞?lì)惖腁pp可以說是必有的交互設(shè)計(jì),如今日頭條,網(wǎng)易新聞等。
GitHub地址:https://github.com/wangliujiayou/Swift-dragLabel 歡迎Star.
效果
主要代碼
手勢長按移動(dòng)
1.給CollectionViewCell添加一個(gè)長按手勢.
private lazy var collectionView: UICollectionView = { let clv = UICollectionView(frame: self.view.frame, collectionViewLayout: ChannelViewLayout()) clv.backgroundColor = UIColor.white clv.delegate = self clv.dataSource = self clv.register(ChannelViewCell.self, forCellWithReuseIdentifier: ChannelViewCellIdentifier) clv.register(ChannelHeaderView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: ChannelViewHeaderIdentifier) let longPress = UILongPressGestureRecognizer(target: self, action: #selector(longPressGesture(_:))) clv.addGestureRecognizer(longPress) return clv }()
2.開始長按時(shí)對cell進(jìn)行截圖或拷貝一個(gè)cell,并隱藏cell.
//MARK: - 長按開始 private func dragBegan(point: CGPoint) { indexPath = collectionView.indexPathForItem(at: point) if indexPath == nil || (indexPath?.section)! > 0 || indexPath?.item == 0 {return} let item = collectionView.cellForItem(at: indexPath!) as? ChannelViewCell item?.isHidden = true dragingItem.isHidden = false dragingItem.frame = (item?.frame)! dragingItem.text = item!.text //放大效果(此處可以根據(jù)需求隨意修改) dragingItem.transform = CGAffineTransform(scaleX: 1.1, y: 1.1) }
3.在手勢移動(dòng)的時(shí)候,找到目標(biāo)是的indexPatch,再調(diào)用系統(tǒng)的api交換這個(gè)cell和隱藏cell的位置,并且更新數(shù)據(jù).
//MARK: - 移動(dòng)過程 private func drageChanged(point: CGPoint) { if indexPath == nil || (indexPath?.section)! > 0 || indexPath?.item == 0 {return} dragingItem.center = point targetIndexPath = collectionView.indexPathForItem(at: point) if targetIndexPath == nil || (targetIndexPath?.section)! > 0 || indexPath == targetIndexPath || targetIndexPath?.item == 0 {return} // 更新數(shù)據(jù) let obj = selectedArr[indexPath!.item] selectedArr.remove(at: indexPath!.row) selectedArr.insert(obj, at: targetIndexPath!.item) //交換位置 collectionView.moveItem(at: indexPath!, to: targetIndexPath!) //進(jìn)行記錄 indexPath = targetIndexPath }
4.手勢停止或取消時(shí),移除view,顯示隱藏cell. (這里手勢取消也要掉用此方法)
//MARK: - 長按結(jié)束或取消 private func drageEnded(point: CGPoint) { if indexPath == nil || (indexPath?.section)! > 0 || indexPath?.item == 0 {return} let endCell = collectionView.cellForItem(at: indexPath!) UIView.animate(withDuration: 0.25, animations: { self.dragingItem.transform = CGAffineTransform.identity self.dragingItem.center = (endCell?.center)! }, completion: { (finish) -> () in endCell?.isHidden = false self.dragingItem.isHidden = true self.indexPath = nil }) }
點(diǎn)擊移動(dòng)
collectionView的點(diǎn)擊方法,我這里分為兩段,第一段為點(diǎn)擊處理事件,第二段為點(diǎn)擊添加添加標(biāo)簽(編輯狀態(tài)下第一段可以點(diǎn)擊排序)
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { if indexPath.section > 0 { // 更新數(shù)據(jù) let obj = recommendArr[indexPath.item] recommendArr.remove(at: indexPath.item) selectedArr.append(obj) //移動(dòng)方法 collectionView.moveItem(at: indexPath, to: NSIndexPath(item: selectedArr.count - 1, section: 0) as IndexPath) } else { if isEdite { if indexPath.item == 0 {return} // 更新數(shù)據(jù) let obj = selectedArr[indexPath.item] selectedArr.remove(at: indexPath.item) recommendArr.insert(obj, at: 0) //移動(dòng)方法 collectionView.moveItem(at: indexPath, to: NSIndexPath(item: 0, section: 1) as IndexPath) } else { if switchoverCallback != nil { //處理點(diǎn)擊的閉包 switchoverCallback!(selectedArr, recommendArr, indexPath.item) _ = navigationController?.popViewController(animated: true) } } } }
其他
此代碼只是一個(gè)效果,沒有怎么封裝,如果仔細(xì)看過的朋友可以知道其實(shí)沒有多么復(fù)雜
點(diǎn)擊移動(dòng)
collectionView.moveItem(at: indexPath, to: NSIndexPath(item: 0, section: 1) as IndexPath)
拖拽移動(dòng)
collectionView.moveItem(at: indexPath!, to: targetIndexPath!)
主要就是這兩個(gè)方法,其他都是處理邏輯以及視圖效果.
提示
如果你們是從iOS9開始適配的話,那么可以用系統(tǒng)的Api,非常簡單好用,大家這里可以自己去試試.
// Support for reordering @available(iOS 9.0, *) open func beginInteractiveMovementForItem(at indexPath: IndexPath) -> Bool // returns NO if reordering was prevented from beginning - otherwise YES @available(iOS 9.0, *) open func updateInteractiveMovementTargetPosition(_ targetPosition: CGPoint) @available(iOS 9.0, *) open func endInteractiveMovement() @available(iOS 9.0, *) open func cancelInteractiveMovement()
源碼可以從這里下載
以上所述是小編給大家介紹的Swift下使用UICollectionView 實(shí)現(xiàn)長按拖拽功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Swift 3.0基礎(chǔ)學(xué)習(xí)之類與結(jié)構(gòu)體
最近在學(xué)swift 3.0,主要看的是蘋果的官方文檔,這里只是根據(jù)自己看官方文檔的理解所做的一些記錄,不是完整的翻譯,希望也對你有所幫助。下面這篇文章主要介紹了Swift 3.0基礎(chǔ)學(xué)習(xí)之類與結(jié)構(gòu)體的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-03-03Swift開發(fā)之UITableView狀態(tài)切換效果
這篇文章主要介紹了Swift開發(fā)之UITableView狀態(tài)切換效果的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08swift 3.0中實(shí)現(xiàn)字符串截取、比較的方法示例
時(shí),為了使用現(xiàn)有的字符串生成一個(gè)新的字符串,我們可以使用截取字符串的方法實(shí)現(xiàn)。下面這篇文章主要給大家介紹了關(guān)于swift 3.0中實(shí)現(xiàn)字符串截取的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒。2017-10-10Spring中BeanFactory與FactoryBean的區(qū)別解讀
這篇文章主要介紹了Spring中BeanFactory與FactoryBean的區(qū)別解讀,Java的BeanFactory是Spring框架中的一個(gè)接口,它是用來管理和創(chuàng)建對象的工廠接口,在Spring中,我們可以定義多個(gè)BeanFactory來管理不同的組件,需要的朋友可以參考下2023-12-12在Swift程序中實(shí)現(xiàn)手勢識(shí)別的方法
這篇文章主要介紹了在Swift程序中實(shí)現(xiàn)手勢識(shí)別的方法,蘋果的Swift語言即將進(jìn)入2.0開源階段,人氣爆棚中:D 需要的朋友可以參考下2015-07-07