import UIKit
var itemWh:CGFloat = 0,itemHt:CGFloat = 75;
class ViewController:UIViewController,UICollectionViewDelegate,UICollectionViewDataSource{
var data:[String] = [String]();
let cellName:String = "MyCell";
var myCollection : UICollectionView!
var screenWh:CGFloat = 0;
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.white
//
screenWh = self.view.frame.width;
//每行3个
itemWh = screenWh/3
data = [
"a","b","c","d","e","f","g"
];
//
let layout = UICollectionViewFlowLayout()
layout.itemSize = CGSize(width:itemWh,height:itemHt)
//列间距,行间距,偏移
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0)
myCollection = UICollectionView.init(frame: self.view.frame, collectionViewLayout: layout)
myCollection.register(MyCell.self, forCellWithReuseIdentifier: cellName)
myCollection.backgroundColor = UIColor.white
myCollection.delegate = self
myCollection.dataSource = self
self.view.addSubview(myCollection)
//添加拖动手势
let gesture = UILongPressGestureRecognizer(target: self, action: #selector(viewCustom(_ :)))
myCollection.addGestureRecognizer(gesture)
}
func viewCustom(_ longPress:UILongPressGestureRecognizer){
let point:CGPoint = longPress.location(in: longPress.view)
let indexPath = self.myCollection?.indexPathForItem(at: point)
if(longPress.state == .began && indexPath == nil){return;}
switch longPress.state {
case .began:
self.myCollection?.beginInteractiveMovementForItem(at: indexPath!)
break
case .changed:
self.myCollection?.updateInteractiveMovementTargetPosition(point)
break
case .ended:
self.myCollection?.endInteractiveMovement()
break
default:
self.myCollection?.cancelInteractiveMovement()
break
}
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1;
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return data.count
}
//item 可以移动
func collectionView(_ collectionView: UICollectionView, canMoveItemAt indexPath: IndexPath) -> Bool {
return true;
}
//item 拖动结束时触发
func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
let sourceIndex = data[sourceIndexPath.row];
data.remove(at: sourceIndexPath.row)
data.insert(sourceIndex, at: destinationIndexPath.row)
//myCollection.reloadData()
print(data)
}
//点击执行删除
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
data.remove(at: indexPath.row)
myCollection.deleteItems(at: [indexPath])
print(data)
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cells = myCollection.dequeueReusableCell(withReuseIdentifier: cellName, for: indexPath) as! MyCell
cells.logo.image = #imageLiteral(resourceName: "a0")
cells.title.text = data[indexPath.row]
return cells
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
class MyCell:UICollectionViewCell{
var logo:UIImageView!
var title:UILabel!
override init(frame: CGRect) {
super.init(frame: frame);
let imgWidth:CGFloat = 50;
logo = UIImageView(frame: CGRect(x: (itemWh-imgWidth)/2, y: 5, width: imgWidth, height: imgWidth));
logo.clipsToBounds = true
logo.layer.cornerRadius = 2;
logo.contentMode = .scaleAspectFill;
title = UILabel(frame: CGRect(x: (itemWh-imgWidth)/2, y: 5+imgWidth, width: imgWidth, height: 18));
title.textAlignment = .center
title.font = UIFont.systemFont(ofSize: 16)
title.textColor = UIColor.init(white: 0.3, alpha: 1)
self.addSubview(logo)
self.addSubview(title)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}