Skip to content

Instantly share code, notes, and snippets.

@zntfdr
Created June 20, 2019 15:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zntfdr/9bdbe0f7918cb1e63e816d3ff0b50191 to your computer and use it in GitHub Desktop.
Save zntfdr/9bdbe0f7918cb1e63e816d3ff0b50191 to your computer and use it in GitHub Desktop.
//
// Simple Context Menu Playground
// fivestars.blog
//
// Created by Federico Zanetello on 20/6/19.
//
import UIKit
import PlaygroundSupport
final class MyViewController: UIViewController,
UIContextMenuInteractionDelegate {
override func loadView() {
let view = UIView()
view.backgroundColor = .black
let interactionView = UIView()
interactionView.backgroundColor = .systemYellow
interactionView.translatesAutoresizingMaskIntoConstraints = false
let interaction = UIContextMenuInteraction(delegate: self)
interactionView.addInteraction(interaction)
print("interaction added")
view.addSubview(interactionView)
NSLayoutConstraint.activate([
interactionView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
interactionView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
interactionView.heightAnchor.constraint(equalTo: interactionView.widthAnchor),
interactionView.heightAnchor.constraint(equalToConstant: 150)
])
self.view = view
}
// MARK: UIContextMenuInteractionDelegate
func contextMenuInteraction(
_ interaction: UIContextMenuInteraction,
configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
print("configurationForMenuAtLocation")
return UIContextMenuConfiguration(identifier: nil,
previewProvider: nil,
actionProvider: { [weak self] _ in
return self?.makeContextMenu()
})
}
private func makeContextMenu() -> UIMenu {
let blog = UIAction(__title: "fivestars.blog",
image: UIImage(systemName: "star.fill")) { _ in
print("https://fivestars.blog")
}
let twitter = UIAction(__title: "Federico Zanetello",
image: UIImage(systemName: "at")) { _ in
print("https://twitter.com/zntfdr")
}
return UIMenu(__title: "",
image: nil,
identifier: nil,
children: [blog, twitter])
}
func contextMenuInteraction(
_ interaction: UIContextMenuInteraction,
previewForHighlightingMenuWith configuration: UIContextMenuConfiguration)
-> UITargetedPreview? {
print("previewForHighlightingMenuWith")
return nil
}
func contextMenuInteraction(
_ interaction: UIContextMenuInteraction,
previewForDismissingMenuWith configuration: UIContextMenuConfiguration)
-> UITargetedPreview? {
print("previewForDismissingMenuWith")
return nil
}
func contextMenuInteraction(
_ interaction: UIContextMenuInteraction,
willCommitWithAnimator animator: UIContextMenuInteractionCommitAnimating) {
print("willCommitWithAnimator")
}
func contextMenuInteractionWillPresent(
_ interaction: UIContextMenuInteraction) {
print("willPresent")
}
func contextMenuInteractionDidEnd(
_ interaction: UIContextMenuInteraction) {
print("didEnd")
}
}
PlaygroundPage.current.liveView = MyViewController()
PlaygroundPage.current.needsIndefiniteExecution = true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment