Skip to content

Commit

Permalink
Add demo
Browse files Browse the repository at this point in the history
Update readme and podspec
  • Loading branch information
cemolcay committed Sep 9, 2017
1 parent f2971ab commit 7d4ef76
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 48 deletions.
Binary file added Demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
54 changes: 52 additions & 2 deletions DragMenuPicker.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,64 @@ Pod::Spec.new do |s|

s.name = "DragMenuPicker"
s.version = "0.0.1"
s.summary = "A custom button with ability to select an option from its items menu with drag gesture."
s.summary = "A custom picker lets you pick an option from its auto scrolling menu without lifting your finger up."

# This description is used to generate tags and improve search results.
# * Think: What does it do? Why did you write it? What is the focus?
# * Try to keep it short, snappy and to the point.
# * Write the description between the DESC delimiters below.
# * Finally, don't worry about the indent, CocoaPods strips it!
s.description = <<-DESC
DragMenuPicker
===
A custom picker lets you pick an option from its auto scrolling menu without lifting your finger up.
You can either use the `@IBDesignable` picker button `DragMenuPicker` or create your own with `DragMenuView` which implements all picker logic.
Demo
----
![alt tag](https://github.com/cemolcay/DragMenuPicker/raw/master/Demo.gif)
Requirements
----
- iOS 9.0+
- Swift 3.0+
Install
----
```
pod 'DragMenuPicker'
```
Usage
----
Create a `DragMenuPicker` from either storyboard or programmatically.
Set its `title` and `items` property to shown in menu.
Set its `didSelectItem` property or implement `dragMenuView(_ dragMenuView: DragMenuView, didSelect item: String, at index: Int)` delegate method to set your action after picking.
You can also set its `direction`, either horizontal or vertical with `margins` to screen edges.
``` swift
horizontalDragPicker?.title = "Horizontal Picker"
horizontalDragPicker?.items = ["First", "Second", "Third", "Fourth", "Other", "Another", "Item 2", "Item 3"]
horizontalDragPicker?.direction = .horizontal
horizontalDragPicker?.margins = 20
horizontalDragPicker?.menuDelegate = self
horizontalDragPicker?.didSelectItem = { item, index in
print("\(item) selected at index \(index)")
}
```
`DragMenuPicker` shows `DragMenuView` with `DragMenuItemView`s inside when you touch down the picker. It disappears after you pick something from menu or cancel picking by lifting your finger up outside of the menu.
They are heavily customisable. You can set `applyStyle` property which callbacks you prototype menu and item that you can style and it applies it to menu.
Also there are `@IBInspectable` properties on `DragMenuPicker` that you can style basic properties inside storyboard.
DESC

s.homepage = "https://github.com/cemolcay/DragMenuPicker"
Expand Down Expand Up @@ -90,7 +140,7 @@ s.summary = "A custom button with ability to select an option from its item
# Not including the public_header_files will make all headers public.
#

s.source_files = "DragSwitchControl/DragSwitchControl.swift"
s.source_files = "DragMenuPicker/DragMenuPicker.swift"
# s.exclude_files = "Classes/Exclude"

# s.public_header_files = "Classes/**/*.h"
Expand Down
3 changes: 3 additions & 0 deletions DragMenuPicker.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = 77Y3N48SNF;
INFOPLIST_FILE = "$(SRCROOT)/DragMenuPicker/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.cemolcay.DragMenuPicker;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand All @@ -284,6 +285,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = 77Y3N48SNF;
INFOPLIST_FILE = "$(SRCROOT)/DragMenuPicker/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.cemolcay.DragMenuPicker;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand All @@ -310,6 +312,7 @@
B25675041F601A2B0080D8ED /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
Expand Down
81 changes: 39 additions & 42 deletions DragMenuPicker/DragMenuPicker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,6 @@

import UIKit

extension UIView {
func debugLayer(color: UIColor = .red) {
layer.borderColor = color.cgColor
layer.borderWidth = 1
}
}

/// Action handler on drag menu item selection.
public typealias DragMenuSelectItemAction = (_ item: String, _ index: Int) -> Void

Expand Down Expand Up @@ -97,8 +90,8 @@ public class DragMenuView: UIView {
public weak var delegate: DragMenuViewDelegate?

/// Helper enum to scroll menu in a direction.
private enum ScrollDirection {
case left, right, up, down
private enum ScrollDirection: Int {
case left = 0, right, up, down
}

/// Initilizes the drag menu with items, direction and item options.
Expand All @@ -118,7 +111,6 @@ public class DragMenuView: UIView {
/// - applyStyle: Style drag menu and its every item with this optional function.
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) {
self.direction = direction
print(controlBounds)
super.init(frame: CGRect(
x: direction == .horizontal ? -controlBounds.minX + margins : 0,
y: direction == .horizontal ? 0 : -controlBounds.minY + margins,
Expand All @@ -128,9 +120,6 @@ public class DragMenuView: UIView {
clipsToBounds = true
addSubview(menuView)

debugLayer()
menuView.debugLayer(color: .blue)

addGestureRecognizer(UITapGestureRecognizer(target: nil, action: nil)) // add dummy gesture recognizer
menuView.backgroundColor = backgroundColor
menuView.frame = CGRect(
Expand All @@ -154,7 +143,6 @@ public class DragMenuView: UIView {
itemView.font = font
self.items.append(itemView)
menuView.addSubview(itemView)
itemView.debugLayer(color: .green)
applyStyle?(self, itemView)
}
}
Expand Down Expand Up @@ -209,35 +197,44 @@ public class DragMenuView: UIView {
}

isScrolling = true
scrollTimer = Timer.scheduledTimer(withTimeInterval: 0.016, repeats: true, block: { [weak self] _ in
guard let this = self else { return }
switch to {
case .left:
var newPosition = this.menuView.frame.origin.x + this.maximumScrollingSpeed
if newPosition > 0 {
newPosition = 0
}
this.menuView.layer.frame.origin.x = newPosition
case .right:
var newPosition = this.menuView.frame.origin.x - this.maximumScrollingSpeed
if newPosition <= this.frame.size.width - this.menuView.frame.size.width {
newPosition = this.frame.size.width - this.menuView.frame.size.width
}
this.menuView.layer.frame.origin.x = newPosition
case .up:
var newPosition = this.menuView.frame.origin.y + this.maximumScrollingSpeed
if newPosition > 0 {
newPosition = 0
}
this.menuView.frame.origin.y = newPosition
case .down:
var newPosition = this.menuView.frame.origin.y - this.maximumScrollingSpeed
if newPosition <= this.frame.size.height - this.menuView.frame.size.height {
newPosition = this.frame.size.height - this.menuView.frame.size.height
}
this.menuView.frame.origin.y = newPosition
scrollTimer = Timer.scheduledTimer(
timeInterval: 0.016,
target: self,
selector: #selector(timerTick(timer:)),
userInfo: to.rawValue,
repeats: true)
}

@objc private func timerTick(timer: Timer) {
guard let directionValue = (timer.userInfo as? Int),
let scrollDirection = ScrollDirection(rawValue: directionValue)
else { return }
switch scrollDirection {
case .left:
var newPosition = menuView.frame.origin.x + maximumScrollingSpeed
if newPosition > 0 {
newPosition = 0
}
menuView.layer.frame.origin.x = newPosition
case .right:
var newPosition = menuView.frame.origin.x - maximumScrollingSpeed
if newPosition <= frame.size.width - menuView.frame.size.width {
newPosition = frame.size.width - menuView.frame.size.width
}
})
menuView.layer.frame.origin.x = newPosition
case .up:
var newPosition = menuView.frame.origin.y + maximumScrollingSpeed
if newPosition > 0 {
newPosition = 0
}
menuView.frame.origin.y = newPosition
case .down:
var newPosition = menuView.frame.origin.y - maximumScrollingSpeed
if newPosition <= frame.size.height - menuView.frame.size.height {
newPosition = frame.size.height - menuView.frame.size.height
}
menuView.frame.origin.y = newPosition
}
}

private func stopScrolling() {
Expand Down
2 changes: 0 additions & 2 deletions DragMenuPicker/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class ViewController: UIViewController, DragMenuViewDelegate {
horizontalDragPicker?.didSelectItem = { item, index in
print("\(item) selected at index \(index)")
}
horizontalDragPicker?.debugLayer()

// VerticalDragPicker
verticalDragPicker?.title = "Vertical Picker"
Expand All @@ -39,7 +38,6 @@ class ViewController: UIViewController, DragMenuViewDelegate {
verticalDragPicker?.didSelectItem = { item, index in
print("\(item) selected at index \(index)")
}
verticalDragPicker?.debugLayer()
}

// MARK: DragMenuViewDelegate
Expand Down
31 changes: 29 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
DragMenuPicker
===

A custom button with ability to select an option from its items menu with drag gesture.
A custom picker lets you pick an option from its auto scrolling menu without lifting your finger up.

You can either use the `@IBDesignable` picker button `DragMenuPicker` or create your own with `DragMenuView` which implements all picker logic.

Demo
----
![alt tag](https://github.com/cemolcay/DragMenuPicker/raw/master/Demo.gif)

Requirements
----
Expand All @@ -16,8 +19,32 @@ Install
----

```
pod 'DragSwitchControl'
pod 'DragMenuPicker'
```

Usage
----

Create a `DragMenuPicker` from either storyboard or programmatically.
Set its `title` and `items` property to shown in menu.
Set its `didSelectItem` property or implement `dragMenuView(_ dragMenuView: DragMenuView, didSelect item: String, at index: Int)` delegate method to set your action after picking.
You can also set its `direction`, either horizontal or vertical with `margins` to screen edges.


``` swift
horizontalDragPicker?.title = "Horizontal Picker"
horizontalDragPicker?.items = ["First", "Second", "Third", "Fourth", "Other", "Another", "Item 2", "Item 3"]
horizontalDragPicker?.direction = .horizontal
horizontalDragPicker?.margins = 20
horizontalDragPicker?.menuDelegate = self
horizontalDragPicker?.didSelectItem = { item, index in
print("\(item) selected at index \(index)")
}
```


`DragMenuPicker` shows `DragMenuView` with `DragMenuItemView`s inside when you touch down the picker. It disappears after you pick something from menu or cancel picking by lifting your finger up outside of the menu.

They are heavily customisable. You can set `applyStyle` property which callbacks you prototype menu and item that you can style and it applies it to menu.

Also there are `@IBInspectable` properties on `DragMenuPicker` that you can style basic properties inside storyboard.

0 comments on commit 7d4ef76

Please sign in to comment.