Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 126 additions & 0 deletions Example/ContextMenuViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

import UIKit
import SwiftReorder

class ContextMenuViewController: UITableViewController {

var items = (1...10).map { "Item \($0)" }

private var impactFeedbackgenerator : AnyObject? {
if #available(iOS 10, *) {
return UIImpactFeedbackGenerator(style: .light)
}
return nil
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

init() {
super.init(style: .plain)
}

override func viewDidLoad() {
super.viewDidLoad()

title = "Context Menu"

tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
tableView.allowsSelection = false
tableView.reorder.delegate = self

if #available(iOS 13.0, *) {
tableView.reorder.longPressDuration = 0.09
tableView.reorder.animationDuration = 0.1
}
}
}

extension ContextMenuViewController: UIContextMenuInteractionDelegate {
@available(iOS 13.0, *)
func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {

return UIContextMenuConfiguration(identifier: nil, previewProvider: nil, actionProvider: { suggestedActions in
return self.makeContextMenu()
})
}

@available(iOS 13.0, *)
private func makeContextMenu() -> UIMenu {

let delete = UIAction(title: "Delete", image: UIImage(systemName: "trash"), attributes: .destructive) { action in
}

let share = UIAction(title: "Share", image: UIImage(systemName: "square.and.arrow.up")) { action in
}

return UIMenu(title: "Main Menu", children: [delete, share])
}
}

extension ContextMenuViewController {

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let spacer = tableView.reorder.spacerCell(for: indexPath) {
return spacer
}

let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = items[indexPath.row]


if #available(iOS 13.0, *) {
let interaction = UIContextMenuInteraction(delegate: self)
cell.addInteraction(interaction)
} else {
// Fallback on earlier versions -- Do Nothing
}

return cell
}

}

extension ContextMenuViewController: TableViewReorderDelegate {

func tableView(_ tableView: UITableView, reorderRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
let item = items[sourceIndexPath.row]
items.remove(at: sourceIndexPath.row)
items.insert(item, at: destinationIndexPath.row)
if #available(iOS 10, *) {
impactFeedbackgenerator!.impactOccurred()
}

}

func tableView(_ tableView: UITableView, canReorderRowAt indexPath: IndexPath) -> Bool {
if #available(iOS 10, *) {
impactFeedbackgenerator!.impactOccurred()
}
return true
}
}
5 changes: 5 additions & 0 deletions Example/RootViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class RootViewController: UITableViewController {
case nonMovable
case effects
case customCells
case contextMenu

case count
}
Expand Down Expand Up @@ -75,6 +76,8 @@ extension RootViewController {
cell.textLabel?.text = "Effects"
case .customCells:
cell.textLabel?.text = "Custom Cells"
case .contextMenu:
cell.textLabel?.text = "Context Menu"
case .count:
break
}
Expand All @@ -97,6 +100,8 @@ extension RootViewController {
navigationController?.pushViewController(EffectsViewController(), animated: true)
case .customCells:
navigationController?.pushViewController(CustomCellsViewController(), animated: true)
case .contextMenu:
navigationController?.pushViewController(ContextMenuViewController(), animated: true)
case .count:
break
}
Expand Down
5 changes: 5 additions & 0 deletions SwiftReorder.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
66FC50F71D5EE49D00CFCCCE /* LongListViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 66FC50EE1D5EE49D00CFCCCE /* LongListViewController.swift */; };
66FC50F81D5EE49D00CFCCCE /* NonMovableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 66FC50EF1D5EE49D00CFCCCE /* NonMovableViewController.swift */; };
66FC50F91D5EE49D00CFCCCE /* RootViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 66FC50F01D5EE49D00CFCCCE /* RootViewController.swift */; };
954884B2232EE07600668FCC /* ContextMenuViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 954884B1232EE07600668FCC /* ContextMenuViewController.swift */; };
AB6E455C21A82AAD00D47CFC /* CustomCellsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB6E455B21A82AAD00D47CFC /* CustomCellsViewController.swift */; };
D9DAD5E12134663E00484E71 /* SwiftReorder.h in Headers */ = {isa = PBXBuildFile; fileRef = D9DAD5DF2134663E00484E71 /* SwiftReorder.h */; settings = {ATTRIBUTES = (Public, ); }; };
D9DAD5E42134663E00484E71 /* SwiftReorder.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D9DAD5DD2134663E00484E71 /* SwiftReorder.framework */; };
Expand Down Expand Up @@ -66,6 +67,7 @@
66FC50F01D5EE49D00CFCCCE /* RootViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = RootViewController.swift; path = Example/RootViewController.swift; sourceTree = SOURCE_ROOT; };
66FC50FA1D5EE4CA00CFCCCE /* UITableView+Reorder.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "UITableView+Reorder.swift"; path = "Source/UITableView+Reorder.swift"; sourceTree = "<group>"; };
66FC50FC1D5EE4D600CFCCCE /* ReorderController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ReorderController.swift; path = Source/ReorderController.swift; sourceTree = "<group>"; };
954884B1232EE07600668FCC /* ContextMenuViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContextMenuViewController.swift; sourceTree = "<group>"; };
AB6E455B21A82AAD00D47CFC /* CustomCellsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomCellsViewController.swift; sourceTree = "<group>"; };
D9DAD5DD2134663E00484E71 /* SwiftReorder.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SwiftReorder.framework; sourceTree = BUILT_PRODUCTS_DIR; };
D9DAD5DF2134663E00484E71 /* SwiftReorder.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SwiftReorder.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -121,6 +123,7 @@
66FC50EF1D5EE49D00CFCCCE /* NonMovableViewController.swift */,
66FC50EB1D5EE49D00CFCCCE /* EffectsViewController.swift */,
AB6E455B21A82AAD00D47CFC /* CustomCellsViewController.swift */,
954884B1232EE07600668FCC /* ContextMenuViewController.swift */,
66FC50ED1D5EE49D00CFCCCE /* Info.plist */,
);
path = Example;
Expand Down Expand Up @@ -265,6 +268,7 @@
66FC50F91D5EE49D00CFCCCE /* RootViewController.swift in Sources */,
AB6E455C21A82AAD00D47CFC /* CustomCellsViewController.swift in Sources */,
66FC50F81D5EE49D00CFCCCE /* NonMovableViewController.swift in Sources */,
954884B2232EE07600668FCC /* ContextMenuViewController.swift in Sources */,
66FC50F11D5EE49D00CFCCCE /* AppDelegate.swift in Sources */,
66FC50F71D5EE49D00CFCCCE /* LongListViewController.swift in Sources */,
66FC50F51D5EE49D00CFCCCE /* GroupedViewController.swift in Sources */,
Expand Down Expand Up @@ -410,6 +414,7 @@
buildSettings = {
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
CODE_SIGN_IDENTITY = "iPhone Developer";
DEVELOPMENT_TEAM = "";
INFOPLIST_FILE = Example/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = adamshin.SwiftReorder;
Expand Down