Skip to content

Instantly share code, notes, and snippets.

@zntfdr zntfdr/starsContextMenu.swift Secret
Created Jun 21, 2019

Embed
What would you like to do?
//
// Stars 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 = .white
let contextMenuInteraction = UIContextMenuInteraction(delegate: self)
view.addInteraction(contextMenuInteraction)
self.view = view
}
// MARK: UIContextMenuInteractionDelegate
func contextMenuInteraction(
_ interaction: UIContextMenuInteraction,
configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
addLabel(text: "⭐️", at: location)
return nil
}
private func addLabel(text: String,
at location: CGPoint) {
let label = UILabel()
label.text = text
label.textAlignment = .center
label.font = .preferredFont(forTextStyle: .largeTitle)
label.frame.size = CGSize(width: 40, height: 25)
view.addSubview(label)
label.center = location
}
}
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
You can’t perform that action at this time.