Instantly share code, notes, and snippets.
zntfdr/SimpleContextMenu.swift Secret
Created Jun 20, 2019
// | |
// 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