Skip to content

Commit f2971ab

Browse files
committed
Fix scrollable bounds
1 parent ec219cb commit f2971ab

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

DragMenuPicker/Base.lproj/Main.storyboard

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<!--View Controller-->
1313
<scene sceneID="tne-QT-ifu">
1414
<objects>
15-
<viewController id="BYZ-38-t0r" customClass="ViewController" customModule="DragSwitchControl" customModuleProvider="target" sceneMemberID="viewController">
15+
<viewController id="BYZ-38-t0r" customClass="ViewController" customModule="DragMenuPicker" customModuleProvider="target" sceneMemberID="viewController">
1616
<layoutGuides>
1717
<viewControllerLayoutGuide type="top" id="y3c-jy-aDJ"/>
1818
<viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
@@ -24,12 +24,12 @@
2424
<scrollView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="AYI-DX-rrd">
2525
<rect key="frame" x="0.0" y="20" width="375" height="647"/>
2626
<subviews>
27-
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="tIf-g9-hPg" customClass="DragMenuPicker" customModule="DragSwitchControl" customModuleProvider="target">
27+
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="tIf-g9-hPg" customClass="DragMenuPicker" customModule="DragMenuPicker" customModuleProvider="target">
2828
<rect key="frame" x="24" y="171" width="134" height="45"/>
2929
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
3030
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
3131
</view>
32-
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="Jvk-lC-uRI" customClass="DragMenuPicker" customModule="DragSwitchControl" customModuleProvider="target">
32+
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="Jvk-lC-uRI" customClass="DragMenuPicker" customModule="DragMenuPicker" customModuleProvider="target">
3333
<rect key="frame" x="24" y="301" width="134" height="45"/>
3434
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
3535
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>

DragMenuPicker/DragMenuPicker.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ public class DragMenuView: UIView {
118118
/// - applyStyle: Style drag menu and its every item with this optional function.
119119
public init(items: [String], initalSelection: Int, estimatedItemSize: CGFloat, controlBounds: CGRect, direction: DragMenuDirection, margins: CGFloat, backgroundColor: UIColor, highlightedColor: UIColor, textColor: UIColor, highlightedTextColor: UIColor, font: UIFont, applyStyle: DragMenuApplyStyleAction? = nil) {
120120
self.direction = direction
121+
print(controlBounds)
121122
super.init(frame: CGRect(
122123
x: direction == .horizontal ? -controlBounds.minX + margins : 0,
123124
y: direction == .horizontal ? 0 : -controlBounds.minY + margins,
@@ -134,7 +135,7 @@ public class DragMenuView: UIView {
134135
menuView.backgroundColor = backgroundColor
135136
menuView.frame = CGRect(
136137
x: direction == .horizontal ? -(CGFloat(initalSelection) * estimatedItemSize) + controlBounds.minX : 0,
137-
y: direction == .horizontal ? 0 : -(CGFloat(initalSelection) * estimatedItemSize) + controlBounds.minY - (controlBounds.size.height / 2),
138+
y: direction == .horizontal ? 0 : -(CGFloat(initalSelection) * estimatedItemSize) + controlBounds.minY - margins,
138139
width: direction == .horizontal ? CGFloat(items.count) * estimatedItemSize : controlBounds.width,
139140
height: direction == .horizontal ? controlBounds.height : CGFloat(items.count) * estimatedItemSize)
140141

@@ -153,7 +154,7 @@ public class DragMenuView: UIView {
153154
itemView.font = font
154155
self.items.append(itemView)
155156
menuView.addSubview(itemView)
156-
157+
itemView.debugLayer(color: .green)
157158
applyStyle?(self, itemView)
158159
}
159160
}
@@ -174,7 +175,9 @@ public class DragMenuView: UIView {
174175
/// - Parameter location: Current position of touch on drag menu.
175176
public func updateMenu(for position: CGPoint) {
176177
// Check if menu is scrollable
177-
if menuView.frame.width > frame.width || menuView.frame.height > frame.height {
178+
let scrollable = (menuView.frame.minX < 0 || menuView.frame.maxX > frame.size.width) || // check horizontal scrollable bounds
179+
(menuView.frame.minY < 0 || menuView.frame.maxY > frame.size.height) // check vertical scrollable bounds
180+
if scrollable {
178181
// Update menu position
179182
switch direction {
180183
case .horizontal:
@@ -380,7 +383,8 @@ public class DragMenuView: UIView {
380383
public override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
381384
guard isOpen,
382385
let dragMenu = dragMenu,
383-
let touchLocation = touches.first?.location(in: dragMenu) else { return }
386+
let touchLocation = touches.first?.location(in: dragMenu)
387+
else { return }
384388
dragMenu.updateMenu(for: touchLocation)
385389
}
386390

DragMenuPicker/ViewController.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class ViewController: UIViewController, DragMenuViewDelegate {
2828
horizontalDragPicker?.didSelectItem = { item, index in
2929
print("\(item) selected at index \(index)")
3030
}
31+
horizontalDragPicker?.debugLayer()
3132

3233
// VerticalDragPicker
3334
verticalDragPicker?.title = "Vertical Picker"
@@ -38,6 +39,7 @@ class ViewController: UIViewController, DragMenuViewDelegate {
3839
verticalDragPicker?.didSelectItem = { item, index in
3940
print("\(item) selected at index \(index)")
4041
}
42+
verticalDragPicker?.debugLayer()
4143
}
4244

4345
// MARK: DragMenuViewDelegate

0 commit comments

Comments
 (0)