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
}()
//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
//放大效果(此处可以根据需求随意修改)
dragingItem.transform = CGAffineTransform(scaleX: 1.1, y: 1.1)
}
//MARK: - 移动过程
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}
// 更新数据
let obj = selectedArr[indexPath!.item]
selectedArr.remove(at: indexPath!.row)
selectedArr.insert(obj, at: targetIndexPath!.item)
//交换位置
collectionView.moveItem(at: indexPath!, to: targetIndexPath!)
//进行记录
indexPath = targetIndexPath
}
//MARK: - 长按结束或取消
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
})
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if indexPath.section > 0 {
// 更新数据
let obj = recommendArr[indexPath.item]
recommendArr.remove(at: indexPath.item)
selectedArr.append(obj)
//移动方法
collectionView.moveItem(at: indexPath, to: NSIndexPath(item: selectedArr.count - 1, section: 0) as IndexPath)
} else {
if isEdite {
if indexPath.item == 0 {return}
// 更新数据
let obj = selectedArr[indexPath.item]
selectedArr.remove(at: indexPath.item)
recommendArr.insert(obj, at: 0)
//移动方法
collectionView.moveItem(at: indexPath, to: NSIndexPath(item: 0, section: 1) as IndexPath)
} else {
if switchoverCallback != nil {
//处理点击的闭包
switchoverCallback!(selectedArr, recommendArr, indexPath.item)
_ = navigationController?.popViewController(animated: true)
}
}
}
}
collectionView.moveItem(at: indexPath, to: NSIndexPath(item: 0, section: 1) as IndexPath)
collectionView.moveItem(at: indexPath!, to: targetIndexPath!)
// 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()
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有