Commit 5282e5e9 authored by Zandor Smith's avatar Zandor Smith 💻
Browse files

Implement context menu.

parent c58135d9
Pipeline #5375 failed with stages
in 1 minute and 1 second
{
"images" : [
{
"idiom" : "universal",
"filename" : "photo-1506744038136-46273834b3fb.jpeg",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13122.16" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14865.1" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina6_1" orientation="portrait" appearance="light"/>
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13104.12"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14819.2"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
......@@ -9,16 +10,45 @@
<!--View Controller-->
<scene sceneID="tne-QT-ifu">
<objects>
<viewController id="BYZ-38-t0r" customClass="ViewController" customModuleProvider="target" sceneMemberID="viewController">
<viewController id="BYZ-38-t0r" customClass="ViewController" customModule="ContextualMenusTest" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" xcode11CocoaTouchSystemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
<subviews>
<imageView clipsSubviews="YES" contentMode="scaleAspectFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="TestImage" translatesAutoresizingMaskIntoConstraints="NO" id="AGE-am-fVo">
<rect key="frame" x="147" y="388" width="120" height="120"/>
<constraints>
<constraint firstAttribute="width" constant="120" id="Hfg-Rw-0Mv"/>
<constraint firstAttribute="height" constant="120" id="Kxh-Cb-Nyv"/>
</constraints>
</imageView>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="naP-nI-KLE">
<rect key="frame" x="187.5" y="832" width="39" height="30"/>
<state key="normal" title="Reset"/>
<connections>
<action selector="reset:" destination="BYZ-38-t0r" eventType="touchUpInside" id="KNs-pm-pcV"/>
</connections>
</button>
</subviews>
<color key="backgroundColor" systemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
<constraints>
<constraint firstItem="naP-nI-KLE" firstAttribute="centerX" secondItem="6Tk-OE-BBY" secondAttribute="centerX" id="Ek5-jK-FO4"/>
<constraint firstItem="naP-nI-KLE" firstAttribute="bottom" secondItem="6Tk-OE-BBY" secondAttribute="bottom" id="FoK-lE-B1M"/>
<constraint firstItem="AGE-am-fVo" firstAttribute="centerY" secondItem="8bC-Xf-vdC" secondAttribute="centerY" id="aBz-Wn-ZrM"/>
<constraint firstItem="AGE-am-fVo" firstAttribute="centerX" secondItem="6Tk-OE-BBY" secondAttribute="centerX" id="l9A-hx-vam"/>
</constraints>
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
</view>
<connections>
<outlet property="imageView" destination="AGE-am-fVo" id="3GV-63-H42"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="-78.260869565217391" y="137.94642857142856"/>
</scene>
</scenes>
<resources>
<image name="TestImage" width="1000" height="667"/>
</resources>
</document>
......@@ -10,11 +10,51 @@ import UIKit
class ViewController: UIViewController {
@IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let interaction = UIContextMenuInteraction(delegate: self)
self.imageView.addInteraction(interaction)
}
@IBAction func reset(_ sender: Any) {
self.imageView.alpha = 1.0
}
}
extension ViewController: UIContextMenuInteractionDelegate {
func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
return UIContextMenuConfiguration(identifier: nil, previewProvider: nil, actionProvider: { suggestedActions in
let setThumbnail = UIAction(title: "Set thumbnail", image: UIImage(systemName: "camera.on.rectangle")) { _ in
print("Set thumbnail")
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
let alert = UIAlertController(title: "Set as thumbnail", message: "The image is now set as the thumbnail.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
let share = UIAction(title: "Share", image: UIImage(systemName: "square.and.arrow.up")) { _ in
print("Share")
let activityViewController = UIActivityViewController(activityItems: [self.imageView.image!], applicationActivities: nil)
self.present(activityViewController, animated: true, completion: nil)
}
let delete = UIAction(title: "Delete", image: UIImage(systemName: "trash"), attributes: .destructive) { _ in
print("Delete")
let alert = UIAlertController(title: "Are you sure?", message: "Are you sure you want to delete this image?", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
alert.addAction(UIAlertAction(title: "Delete", style: .destructive, handler: { _ in
UIView.animate(withDuration: 1.0) {
self.imageView.alpha = 0.0
}
}))
self.present(alert, animated: true, completion: nil)
}
return UIMenu(title: "", children: [setThumbnail, share, delete])
})
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment