Skip to content

Commit

Permalink
Updated
Browse files Browse the repository at this point in the history
  • Loading branch information
VladK9 committed Mar 7, 2022
1 parent d14c54e commit 28c4e91
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 105 deletions.
90 changes: 23 additions & 67 deletions Sources/UIFloatMenu.swift
Expand Up @@ -32,20 +32,19 @@ class UIFloatMenu {
static private var animationDuration: TimeInterval = 0.3

// Delegate
static var closeDelegate: UIFloatMenuCloseDelegate?
static var textFieldDelegate: UIFloatMenuTextFieldDelegate?
static public var delegate = Delegates()

// Queue
static private var queue = [UIFloatMenuQueue]()

//MARK: - Show
static func show(_ vc: UIViewController, actions: [UIFloatMenuAction]) {
if queue.count <= maxView {
let correct = correctPosition(viewConfig.presentation)
let correct = UIFloatMenuHelper.correctPosition(viewConfig.presentation)
let id = UIFloatMenuID.genUUID(queue.count)

let menuView = UIFloatMenuView.init(items: actions, vc: currentVC, header: headerConfig, config: viewConfig,
closeDelegate: closeDelegate, textFieldDelegate: textFieldDelegate)
delegate: delegate)
menuView.tag = id

vc.view.addSubview(menuView)
Expand Down Expand Up @@ -137,38 +136,13 @@ class UIFloatMenu {
static private func panChanged(_ gesture: UIPanGestureRecognizer) {
let view = gesture.view!
let translation = gesture.translation(in: gesture.view)
let velocity = gesture.velocity(in: gesture.view)

var translationAmount = translation.y >= 0 ? translation.y : -pow(abs(translation.y), 0.7)
var translationAmount = translation.y >= 0 ? translation.y : -pow(abs(translation.y), 0.5)

let rubberBanding = true

if !rubberBanding && translationAmount < 0 { translationAmount = 0 }

if gesture.direction(in: view) == .Up && gesture.view!.frame.origin.y < initY.last! {
if let UIFloatMenu = currentVC.view.viewWithTag(queue.last!.uuid!) {
let backTranslationAmount = translation.y >= 0 ? translation.y : -pow(abs(translation.y), 0.6)
UIFloatMenu.transform = CGAffineTransform(translationX: 0, y: backTranslationAmount)
}
}

if gesture.direction(in: view) == .Down {
for order in 0..<queue.count-1 {
if let UIFloatMenu = currentVC.view.viewWithTag(queue[order].uuid) {
if velocity.y > 180 {
UIView.animate(withDuration: 0.2, animations: {
UIFloatMenu.transform = .identity
})
} else {
if UIFloatMenu.frame.origin.y <= initY[order] {
let backTranslationAmount = translation.y >= 0 ? translation.y : -pow(abs(translation.y), 0.6)
UIFloatMenu.transform = CGAffineTransform(translationX: 0, y: backTranslationAmount)
}
}
}
}
}

view.transform = CGAffineTransform(translationX: 0, y: translationAmount)
}

Expand All @@ -186,33 +160,9 @@ class UIFloatMenu {
}
}

// MARK: - correctPosition()
static public func correctPosition(_ position: UIFloatMenuPresentStyle) -> UIFloatMenuPresentStyle {
let device = UIDevice.current.userInterfaceIdiom

if device == .pad {
let layout = Layout.determineLayout()

if layout == .iPadOneThirdScreen {
if case .center = position {
return .center
}
return .default
}
return position
} else if device == .phone {
if case .center = position {
return .center
}
return .default
} else {
return position
}
}

// MARK: - closeMenu()
static private func closeMenu(_ menuView: UIView) {
let correct = correctPosition(viewConfig.presentation)
let correct = UIFloatMenuHelper.correctPosition(viewConfig.presentation)
let appRect = UIApplication.shared.windows[0].bounds
let topPadding = UIFloatMenuHelper.getPadding(.top)

Expand Down Expand Up @@ -268,9 +218,10 @@ class UIFloatMenu {
}

menuView.alpha = 0
UIView.animate(withDuration: animationDuration, animations: {
let animator = UIViewPropertyAnimator(duration: animationDuration, dampingRatio: 1.0) {
menuView.alpha = 1
})
}
animator.startAnimation()
break
case .default:
if iPad_window_width != 0 {
Expand All @@ -279,9 +230,10 @@ class UIFloatMenu {
if animation {
menuView.center = CGPoint(x: appRect.width/2, y: getPrepare)

UIView.animate(withDuration: animationDuration, delay: 0, options: .transitionCurlUp, animations: {
let animator = UIViewPropertyAnimator(duration: animationDuration, dampingRatio: 1.0) {
menuView.center.y = getShow
})
}
animator.startAnimation()
} else {
menuView.center = CGPoint(x: appRect.width/2, y: getShow)
}
Expand All @@ -293,9 +245,10 @@ class UIFloatMenu {

if animation {
menuView.alpha = 0
UIView.animate(withDuration: animationDuration, animations: {
let animator = UIViewPropertyAnimator(duration: animationDuration, dampingRatio: 1.0) {
menuView.alpha = 1
})
}
animator.startAnimation()
}
break
case .leftDown(let overToolBar):
Expand All @@ -304,9 +257,10 @@ class UIFloatMenu {

if animation {
menuView.alpha = 0
UIView.animate(withDuration: animationDuration, delay: 0, options: .curveEaseOut, animations: {
let animator = UIViewPropertyAnimator(duration: animationDuration, dampingRatio: 1.0) {
menuView.alpha = 1
})
}
animator.startAnimation()
}
break
case .rightUp(let overNavBar):
Expand All @@ -318,9 +272,10 @@ class UIFloatMenu {

if animation {
menuView.alpha = 0
UIView.animate(withDuration: animationDuration, animations: {
let animator = UIViewPropertyAnimator(duration: animationDuration, dampingRatio: 1.0) {
menuView.alpha = 1
})
}
animator.startAnimation()
}
}
break
Expand All @@ -333,9 +288,10 @@ class UIFloatMenu {

if animation {
menuView.alpha = 0
UIView.animate(withDuration: animationDuration, animations: {
let animator = UIViewPropertyAnimator(duration: animationDuration, dampingRatio: 1.0) {
menuView.alpha = 1
})
}
animator.startAnimation()
}
}
break
Expand Down
21 changes: 6 additions & 15 deletions Sources/UIFloatMenuController.swift
Expand Up @@ -16,8 +16,7 @@ class UIFloatMenuController: UIViewController, UIGestureRecognizerDelegate {
public var config = UIFloatMenuConfig()
public var actions = [UIFloatMenuAction]()

var closeDelegate: UIFloatMenuCloseDelegate?
var textFieldDelegate: UIFloatMenuTextFieldDelegate?
public var delegate = Delegates()

lazy private var backgroundView = UIView()

Expand Down Expand Up @@ -83,8 +82,8 @@ class UIFloatMenuController: UIViewController, UIGestureRecognizerDelegate {
menu.currentVC = currentVC
menu.headerConfig = header
menu.viewConfig = config
menu.closeDelegate = closeDelegate
menu.textFieldDelegate = textFieldDelegate
menu.delegate.close = delegate.close
menu.delegate.textField = delegate.textField
menu.show(self, actions: actions)
}

Expand All @@ -110,15 +109,11 @@ class UIFloatMenuController: UIViewController, UIGestureRecognizerDelegate {
self.dismiss(animated: false, completion: nil)
})

if closeDelegate != nil {
closeDelegate?.UIFloatMenuDidCloseMenu()
if delegate.close != nil {
delegate.close?.UIFloatMenuDidCloseMenu()
}
}

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}

// MARK: - detect theme changes and windows resize
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
Expand Down Expand Up @@ -170,10 +165,6 @@ class UIFloatMenuController: UIViewController, UIGestureRecognizerDelegate {
}
}
}
} else if traitCollection.horizontalSizeClass == .regular {
print("horizontalSizeClass-regular")
} else {
print("horizontalSizeClass-unspecified")
}
}

Expand Down Expand Up @@ -233,7 +224,7 @@ class UIFloatMenuController: UIViewController, UIGestureRecognizerDelegate {
if Orientation.isLandscape {
menuView.center = self.view.center
} else {
menu.showTo(menuView, positions: menu.correctPosition((self.queue.last?.config.presentation)!))
menu.showTo(menuView, positions: UIFloatMenuHelper.correctPosition((self.queue.last?.config.presentation)!))
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions Sources/UIFloatMenuDelegate.swift
Expand Up @@ -5,6 +5,12 @@

import UIKit

//MARK: - Delegates
public struct Delegates {
var close: UIFloatMenuCloseDelegate!
var textField: UIFloatMenuTextFieldDelegate!
}

//MARK: - UIFloatMenuCloseDelegate
public protocol UIFloatMenuCloseDelegate: AnyObject {

Expand Down
24 changes: 24 additions & 0 deletions Sources/UIFloatMenuHelper.swift
Expand Up @@ -108,6 +108,30 @@ class UIFloatMenuHelper {
return backView
}

// MARK: - correctPosition()
static func correctPosition(_ position: UIFloatMenuPresentStyle) -> UIFloatMenuPresentStyle {
let device = UIDevice.current.userInterfaceIdiom

if device == .pad {
let layout = Layout.determineLayout()

if layout == .iPadOneThirdScreen {
if case .center = position {
return .center
}
return .default
}
return position
} else if device == .phone {
if case .center = position {
return .center
}
return .default
} else {
return position
}
}

}

// MARK: - Detect gesture direction
Expand Down
12 changes: 3 additions & 9 deletions Sources/UIFloatMenuView/UIFloatMenuHeaderView.swift
Expand Up @@ -4,17 +4,13 @@
//

import UIKit
import Foundation

class UIFloatMenuHeaderView: UIView {

private var stackView: UIStackView = UIStackView()

var menuConfig = UIFloatMenuConfig()
var headerConfig = UIFloatMenuHeaderConfig()

private var viewWidth = CGFloat()

private var headerHeight: CGFloat = 60

//MARK: - Views
Expand Down Expand Up @@ -57,8 +53,6 @@ class UIFloatMenuHeaderView: UIView {
let width = (device == .pad ? menuConfig.viewWidth_iPad : (Orientation.isPortrait ? appRect.width-30 : appRect.width/2.5))!

self.headerConfig = headerConfig
self.menuConfig = menuConfig
self.viewWidth = width

addSubview(cancelButton)
backgroundColor = menuConfig.blurBackground ? .clear : UIFloatMenuColors.mainColor
Expand All @@ -68,7 +62,7 @@ class UIFloatMenuHeaderView: UIView {

if headerConfig.showLine {
lineView.frame.size = CGSize(width: width-(headerConfig.lineInset*2), height: 1)
lineView.center.x = viewWidth/2
lineView.center.x = width/2
addSubview(lineView)
}

Expand Down Expand Up @@ -97,8 +91,8 @@ class UIFloatMenuHeaderView: UIView {

stackView.isUserInteractionEnabled = true

let pan = UITapGestureRecognizer(target: self, action: #selector(tapClose(_:)))
cancelButton.addGestureRecognizer(pan)
let tap = UITapGestureRecognizer(target: self, action: #selector(tapClose(_:)))
cancelButton.addGestureRecognizer(tap)
}

//MARK: - coder
Expand Down

0 comments on commit 28c4e91

Please sign in to comment.