Skip to content

pikachu987/Reorder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Reorder

CI Status Version License Platform

Introduce

UITableView UICollectionView MultipleSection

Requirements

Reorder written in Swift 5.0. Compatible with iOS 9.0+

Installation

Cocoapods

Reorder is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'Reorder'

Usage

UITableView

class TableViewController: UIViewController {
    let tableView = UITableView(frame: .zero, style: .plain)
    var list = [[Int]]()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.reorder.delegate = self
    }
}

// MARK: TableViewReorderDelegate
extension TableViewController: TableViewReorderDelegate {
    var reorderSuperview: UIView {
        return self.navigationController?.view ?? UIView()
    }

    func tableViewReorder(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
        if sourceIndexPath.section == destinationIndexPath.section { // Row
            self.list[sourceIndexPath.section].swapAt(sourceIndexPath.row, destinationIndexPath.row)
        } else { // MultipleSection
            let sourceItem = self.list[sourceIndexPath.section][sourceIndexPath.row]
            self.list[sourceIndexPath.section].remove(at: sourceIndexPath.row)
            self.list[destinationIndexPath.section].insert(sourceItem, at: destinationIndexPath.row)
        }
    }

    func tableViewReorder(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        return self.list[indexPath.section][indexPath.row].canMove
    }
}

UICollectionView

class CollectionViewController: UIViewController {
    let collectionView = UICollectionView(frame: .zero, collectionViewLayout: Layout())
    var list = [[Int]]()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.collectionView.delegate = self
        self.collectionView.dataSource = self
        self.collectionView.reorder.delegate = self
    }
}

// MARK: CollectionViewReorderDelegate
extension MultipleSectionCollectionViewExampleViewController: CollectionViewReorderDelegate {
    var reorderSuperview: UIView {
        return self.navigationController?.view ?? UIView()
    }

    func collectionViewReorder(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
        if sourceIndexPath.section == destinationIndexPath.section { // Item
            let item = self.list[sourceIndexPath.section][sourceIndexPath.item]
            self.list[sourceIndexPath.section].remove(at: sourceIndexPath.item)
            self.list[sourceIndexPath.section].insert(item, at: destinationIndexPath.item)
        } else { // MultipleSection
            let sourceItem = self.list[sourceIndexPath.section][sourceIndexPath.row]
            self.list[sourceIndexPath.section].remove(at: sourceIndexPath.row)
            self.list[destinationIndexPath.section].insert(sourceItem, at: destinationIndexPath.row)
        }
    }
}

Move Auto Scroll

self.tableView.reorder.scrollFrame = CGRect(x: 0, y: 88, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height - 88)
self.collectionView.reorder.scrollFrame = CGRect(x: 0, y: 88, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height - 88)

Can't Move

// MARK: TableViewReorderDelegate
extension ExampleViewController: TableViewReorderDelegate {
    func tableViewReorder(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        return self.list[indexPath.row].canMove
    }
}

Property

Scale

Snapshot view size on cell touch

enum Scale {
    case custom(CGFloat)
    case none
    case small
    case medium
    case large
}
OverlapBehaviour

The amount of overlap with other cells when moving a cell

public enum OverlapBehaviour {
    case oneThird
    case half
    case twoThirds
}

Delegate

protocol ReorderDelegate: class {
    var reorderSuperview: UIView { get }
    func reorderBegan()
    func reorderChanged()
    func reorderEnded()
}

protocol CollectionViewReorderDelegate: ReorderDelegate {
    func collectionViewReorder(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath)
    func collectionViewReorder(_ collectionView: UICollectionView, canMoveItemAt indexPath: IndexPath) -> Bool
}

protocol TableViewReorderDelegate: ReorderDelegate {
    func tableViewReorder(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath)
    func tableViewReorder(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool
}

Protocol

Select the view for which you want to create a snapshop. Is optional. If you don't adopt ReorderCell, make the contentView a snapshot.

class TableViewCell: UITableViewCell, ReorderCell {
    private let reorderView = UIView()

    var reorderSnapshotView: UIView {
        return self.reorderView
    }
}

Author

pikachu987, pikachu77769@gmail.com

License

Reorder is available under the MIT license. See the LICENSE file for more info.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published