Skip to content

Commit 2850fc8

Browse files
committed
Added support for direction
Now you can change the horizontal direction of menu
1 parent 0f46b69 commit 2850fc8

File tree

3 files changed

+42
-17
lines changed

3 files changed

+42
-17
lines changed

Example/ContextMenuSwift/Base.lproj/Main.storyboard

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="19455" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="c6m-RZ-7hU">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="21701" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="c6m-RZ-7hU">
33
<device id="retina4_7" orientation="portrait" appearance="light"/>
44
<dependencies>
55
<deployment identifier="iOS"/>
6-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="19454"/>
6+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="21678"/>
77
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
88
<capability name="System colors in document resources" minToolsVersion="11.0"/>
99
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
@@ -12,13 +12,13 @@
1212
<!--View Controller-->
1313
<scene sceneID="Ozc-Y7-dCv">
1414
<objects>
15-
<viewController id="c6m-RZ-7hU" customClass="ViewController" customModule="ContextMenuSwiftDemo" customModuleProvider="target" sceneMemberID="viewController">
15+
<viewController id="c6m-RZ-7hU" customClass="ViewController" customModule="ContextMenuSwift_Example" customModuleProvider="target" sceneMemberID="viewController">
1616
<view key="view" contentMode="scaleToFill" id="COB-ZW-TLg">
1717
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
1818
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
1919
<subviews>
2020
<scrollView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Ufn-Vv-gCs">
21-
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
21+
<rect key="frame" x="0.0" y="20" width="375" height="647"/>
2222
<subviews>
2323
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="Mzf-ch-0Pr" userLabel="ContentView">
2424
<rect key="frame" x="0.0" y="0.0" width="375" height="2895.5"/>
@@ -357,7 +357,7 @@
357357
</view>
358358
<connections>
359359
<outlet property="canvas" destination="oRD-Ll-05z" id="6OY-xp-qst"/>
360-
<outlet property="cv1" destination="EgT-YS-WuV" id="9g1-38-Wbg"/>
360+
<outlet property="cv1" destination="ihQ-Ez-hEO" id="fhD-Zq-v8U"/>
361361
<outlet property="cv2" destination="Ue8-Rk-YMu" id="w9b-HT-wHk"/>
362362
<outlet property="cv3" destination="RDJ-Ev-Kwv" id="YCO-KQ-bhr"/>
363363
</connections>

Example/ContextMenuSwift/ViewController.swift

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@ class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSour
2626
let edit = "Edit"
2727
let delete = ContextMenuItemWithImage(title: "Delete", image: #imageLiteral(resourceName: "icons8-trash"))
2828
// CM.nibView = UINib(nibName: "CustomCell", bundle: .main)
29+
CM.MenuConstants.horizontalDirection = .right
2930
CM.items = [share, edit, delete]
3031
CM.showMenu(viewTargeted: self.cv1, delegate: self)
31-
let vc1 = UIView(frame: CGRect(x: 0, y: 0, width: CM.MenuConstants.MenuWidth, height: 50))
32-
vc1.backgroundColor = .purple
33-
let vc2 = UIView(frame: CGRect(x: 0, y: 0, width: CM.MenuConstants.MenuWidth, height: 10))
34-
vc2.backgroundColor = .purple
35-
// CM.headerView = vc1
36-
// CM.footerView = vc2
32+
// let vc1 = UIView(frame: CGRect(x: 0, y: 0, width: CM.MenuConstants.MenuWidth, height: 50))
33+
// vc1.backgroundColor = .purple
34+
// let vc2 = UIView(frame: CGRect(x: 0, y: 0, width: CM.MenuConstants.MenuWidth, height: 10))
35+
// vc2.backgroundColor = .purple
36+
// CM.headerView = vc1
37+
// CM.footerView = vc2
3738
// DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
3839
// CM.items = (0..<Int.random(in: 2..<4)).map { "Item \($0)" }
3940
// CM.changeViewTargeted(newView: self.cv3)
@@ -53,11 +54,12 @@ class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSour
5354

5455
extension ViewController : ContextMenuDelegate {
5556
func contextMenuDidSelect(_ contextMenu: ContextMenu, cell: ContextMenuCell, targetedView: UIView, didSelect item: ContextMenuItem, forRowAt index: Int) -> Bool {
57+
print("contextMenuDidSelect", item.title)
5658
return true
5759
}
5860

5961
func contextMenuDidDeselect(_ contextMenu: ContextMenu, cell: ContextMenuCell, targetedView: UIView, didSelect item: ContextMenuItem, forRowAt index: Int) {
60-
62+
print("contextMenuDidDeselect")
6163
}
6264

6365
func contextMenuDidAppear(_ contextMenu: ContextMenu) {

Sources/ContextMenuSwift/ContextMenu.swift

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,14 @@ extension ContextMenuDelegate {
5353

5454
public var CM : ContextMenu = ContextMenu()
5555

56-
public struct ContextMenuConstants {
56+
public class ContextMenuConstants {
57+
58+
public enum HorizontalDirection {
59+
case left
60+
case center
61+
case right
62+
}
63+
5764
public var MaxZoom : CGFloat = 1.1
5865
public var MinZoom : CGFloat = 0.8
5966
public var MenuDefaultHeight : CGFloat = 120
@@ -74,6 +81,7 @@ public struct ContextMenuConstants {
7481
public var BackgroundViewColor : UIColor = UIColor.black.withAlphaComponent(0.6)
7582

7683
public var DismissOnItemTap : Bool = false
84+
public var horizontalDirection: HorizontalDirection = .left
7785
}
7886

7987
open class ContextMenu: NSObject {
@@ -84,7 +92,8 @@ open class ContextMenu: NSObject {
8492
open var placeHolderView : UIView?
8593
open var headerView : UIView?
8694
open var footerView : UIView?
87-
open var nibView: ContextMenuCell.Type = ContextMenuTextCell.self
95+
open var nibView: UINib?
96+
open var cellClassView: ContextMenuCell.Type = ContextMenuTextCell.self
8897
open var closeAnimation = true
8998

9099
open var onItemTap : ((_ index: Int, _ item: ContextMenuItem) -> Bool)?
@@ -301,7 +310,11 @@ open class ContextMenu: NSObject {
301310
tableView.dataSource = self
302311
tableView.delegate = self
303312
tableView.frame = menuView.bounds
304-
tableView.register(nibView, forCellReuseIdentifier: "ContextMenuCell")
313+
if let nibView = nibView {
314+
tableView.register(nibView, forCellReuseIdentifier: "ContextMenuCell")
315+
} else {
316+
tableView.register(cellClassView, forCellReuseIdentifier: "ContextMenuCell")
317+
}
305318
tableView.tableHeaderView = self.headerView
306319
tableView.tableFooterView = self.footerView
307320
tableView.clipsToBounds = true
@@ -546,8 +559,6 @@ open class ContextMenu: NSObject {
546559
else if (tvY + mH) > (mainViewRect.height - MenuConstants.BottomMarginSpace){
547560
mY = tvY - ((tvY + mH) - (mainViewRect.height - MenuConstants.BottomMarginSpace))
548561
}
549-
550-
551562
}
552563

553564
func updateVerticalTargetedImageViewRect() {
@@ -613,6 +624,7 @@ open class ContextMenu: NSObject {
613624
let backgroundHeight = mainViewRect.height - MenuConstants.TopMarginSpace - MenuConstants.BottomMarginSpace
614625

615626
if backgroundHeight > backgroundWidth {
627+
self.updateHorizontalDirection()
616628
self.updateVerticalTargetedImageViewRect()
617629
}
618630
else {
@@ -676,6 +688,17 @@ open class ContextMenu: NSObject {
676688
height: weakSelf.mainViewRect.height
677689
)
678690
}
691+
692+
func updateHorizontalDirection() {
693+
switch MenuConstants.horizontalDirection {
694+
case .left:
695+
mX = MenuConstants.MenuMarginSpace
696+
case .center:
697+
mX = (mainViewRect.width / 2) - (mW / 2)
698+
case .right:
699+
mX = mainViewRect.width - MenuConstants.MenuMarginSpace - mW
700+
}
701+
}
679702
}
680703

681704
extension ContextMenu : UITableViewDataSource, UITableViewDelegate {

0 commit comments

Comments
 (0)