From 67570256d002398dd70a9bcf20ae3540c29e4cf9 Mon Sep 17 00:00:00 2001 From: Bob Sun Date: Sun, 5 Feb 2017 00:35:23 +0800 Subject: [PATCH] tap to show copy menu --- pass.xcodeproj/project.pbxproj | 8 +++---- pass/LabelTableViewCell.swift | 13 ++++++++++++ pass/PasswordDetailTableViewController.swift | 22 +++++++++++++++++++- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/pass.xcodeproj/project.pbxproj b/pass.xcodeproj/project.pbxproj index bfc2ea5..2c4cf07 100644 --- a/pass.xcodeproj/project.pbxproj +++ b/pass.xcodeproj/project.pbxproj @@ -10,7 +10,6 @@ 94BA784B85E071D25EE89B59 /* libPods-pass.a in Frameworks */ = {isa = PBXBuildFile; fileRef = ADCE7A5C3CCC67D7D21BB3C4 /* libPods-pass.a */; }; DC1208581E35EBE60042942E /* ObjectiveGit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DC1208571E35EBE60042942E /* ObjectiveGit.framework */; }; DC4914961E434301007FF592 /* LabelTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC4914941E434301007FF592 /* LabelTableViewCell.swift */; }; - DC4914971E434301007FF592 /* LabelTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = DC4914951E434301007FF592 /* LabelTableViewCell.xib */; }; DC4914991E434600007FF592 /* PasswordDetailTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC4914981E434600007FF592 /* PasswordDetailTableViewController.swift */; }; DC5734AE1E439AD400D09270 /* PasswordsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC5734AD1E439AD400D09270 /* PasswordsViewController.swift */; }; DC7E6EEA1E432E48006C2443 /* Password.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC7E6EE91E432E48006C2443 /* Password.swift */; }; @@ -30,6 +29,7 @@ DCC277D21E30D6EA00402246 /* pass.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = DCC408C81E30BA1300F29B0E /* pass.xcdatamodeld */; }; DCC408A41E2FCC9E00F29B0E /* PasswordStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCC408A31E2FCC9E00F29B0E /* PasswordStore.swift */; }; DCC408C71E307DBB00F29B0E /* SVProgressHUD.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DCC408C61E307DBB00F29B0E /* SVProgressHUD.framework */; }; + DCDDEAB01E4639F300F68193 /* LabelTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = DCDDEAAF1E4639F300F68193 /* LabelTableViewCell.xib */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -38,7 +38,6 @@ AEAD6B31EAF5D061447A68CC /* Pods-pass.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-pass.release.xcconfig"; path = "Pods/Target Support Files/Pods-pass/Pods-pass.release.xcconfig"; sourceTree = ""; }; DC1208571E35EBE60042942E /* ObjectiveGit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = ObjectiveGit.framework; path = Carthage/Build/iOS/ObjectiveGit.framework; sourceTree = ""; }; DC4914941E434301007FF592 /* LabelTableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LabelTableViewCell.swift; sourceTree = ""; }; - DC4914951E434301007FF592 /* LabelTableViewCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = LabelTableViewCell.xib; sourceTree = ""; }; DC4914981E434600007FF592 /* PasswordDetailTableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PasswordDetailTableViewController.swift; sourceTree = ""; }; DC4A746D1E30FBDE00E8EB18 /* Objective-CBridgingHeader.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Objective-CBridgingHeader.h"; sourceTree = ""; }; DC5734AD1E439AD400D09270 /* PasswordsViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PasswordsViewController.swift; sourceTree = ""; }; @@ -61,6 +60,7 @@ DCC408A31E2FCC9E00F29B0E /* PasswordStore.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PasswordStore.swift; sourceTree = ""; }; DCC408C61E307DBB00F29B0E /* SVProgressHUD.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SVProgressHUD.framework; path = Carthage/Build/iOS/SVProgressHUD.framework; sourceTree = ""; }; DCC408C91E30BA1300F29B0E /* pass.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = pass.xcdatamodel; sourceTree = ""; }; + DCDDEAAF1E4639F300F68193 /* LabelTableViewCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = LabelTableViewCell.xib; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -125,7 +125,7 @@ DCAAF7441E2FA66800AB94BC /* SettingsTableViewController.swift */, DC8963BF1E38EEB900828B09 /* SSHKeySettingTableViewController.swift */, DC917BDD1E2E8231000FDF54 /* Assets.xcassets */, - DC4914951E434301007FF592 /* LabelTableViewCell.xib */, + DCDDEAAF1E4639F300F68193 /* LabelTableViewCell.xib */, DC917BDF1E2E8231000FDF54 /* LaunchScreen.storyboard */, DC917BDA1E2E8231000FDF54 /* Main.storyboard */, DCC408C81E30BA1300F29B0E /* pass.xcdatamodeld */, @@ -211,7 +211,7 @@ files = ( DC917BE11E2E8231000FDF54 /* LaunchScreen.storyboard in Resources */, DC917BDE1E2E8231000FDF54 /* Assets.xcassets in Resources */, - DC4914971E434301007FF592 /* LabelTableViewCell.xib in Resources */, + DCDDEAB01E4639F300F68193 /* LabelTableViewCell.xib in Resources */, DC917BDC1E2E8231000FDF54 /* Main.storyboard in Resources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/pass/LabelTableViewCell.swift b/pass/LabelTableViewCell.swift index d6d087b..4dee5e9 100644 --- a/pass/LabelTableViewCell.swift +++ b/pass/LabelTableViewCell.swift @@ -13,6 +13,12 @@ class LabelTableViewCell: UITableViewCell { @IBOutlet weak var contentLabel: UILabel! @IBOutlet weak var titleLabel: UILabel! + override var canBecomeFirstResponder: Bool { + get { + return true + } + } + override func awakeFromNib() { super.awakeFromNib() } @@ -20,5 +26,12 @@ class LabelTableViewCell: UITableViewCell { override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) } + + override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool { + return action == #selector(copy(_:)) + } + override func copy(_ sender: Any?) { + UIPasteboard.general.string = contentLabel.text + } } diff --git a/pass/PasswordDetailTableViewController.swift b/pass/PasswordDetailTableViewController.swift index 997cd55..7c55903 100644 --- a/pass/PasswordDetailTableViewController.swift +++ b/pass/PasswordDetailTableViewController.swift @@ -8,7 +8,7 @@ import UIKit -class PasswordDetailTableViewController: UITableViewController { +class PasswordDetailTableViewController: UITableViewController, UIGestureRecognizerDelegate { var passwordEntity: PasswordEntity? struct TableCell { @@ -43,6 +43,26 @@ class PasswordDetailTableViewController: UITableViewController { tableData[tableDataIndex].item.append(TableCell(title: key, content: value)) } } + + let tapGesture = UITapGestureRecognizer(target: self, action: #selector(PasswordDetailTableViewController.tapMenu(recognizer:))) + tableView.addGestureRecognizer(tapGesture) + tapGesture.delegate = self + } + + func tapMenu(recognizer: UITapGestureRecognizer) { + print("tap") + if recognizer.state == UIGestureRecognizerState.ended { + let tapLocation = recognizer.location(in: self.tableView) + if let tapIndexPath = self.tableView.indexPathForRow(at: tapLocation) { + print(tapIndexPath) + if let tappedCell = self.tableView.cellForRow(at: tapIndexPath) as? LabelTableViewCell { + tappedCell.becomeFirstResponder() + let menuController = UIMenuController.shared + menuController.setTargetRect(tappedCell.contentLabel.frame, in: tappedCell.contentLabel.superview!) + menuController.setMenuVisible(true, animated: true) + } + } + } } override func numberOfSections(in tableView: UITableView) -> Int {