Skip to content

Commit 28c4e91

Browse files
committed
Updated
1 parent d14c54e commit 28c4e91

File tree

6 files changed

+73
-105
lines changed

6 files changed

+73
-105
lines changed

Sources/UIFloatMenu.swift

Lines changed: 23 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,19 @@ class UIFloatMenu {
3232
static private var animationDuration: TimeInterval = 0.3
3333

3434
// Delegate
35-
static var closeDelegate: UIFloatMenuCloseDelegate?
36-
static var textFieldDelegate: UIFloatMenuTextFieldDelegate?
35+
static public var delegate = Delegates()
3736

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

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

4746
let menuView = UIFloatMenuView.init(items: actions, vc: currentVC, header: headerConfig, config: viewConfig,
48-
closeDelegate: closeDelegate, textFieldDelegate: textFieldDelegate)
47+
delegate: delegate)
4948
menuView.tag = id
5049

5150
vc.view.addSubview(menuView)
@@ -137,38 +136,13 @@ class UIFloatMenu {
137136
static private func panChanged(_ gesture: UIPanGestureRecognizer) {
138137
let view = gesture.view!
139138
let translation = gesture.translation(in: gesture.view)
140-
let velocity = gesture.velocity(in: gesture.view)
141139

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

144142
let rubberBanding = true
145143

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

148-
if gesture.direction(in: view) == .Up && gesture.view!.frame.origin.y < initY.last! {
149-
if let UIFloatMenu = currentVC.view.viewWithTag(queue.last!.uuid!) {
150-
let backTranslationAmount = translation.y >= 0 ? translation.y : -pow(abs(translation.y), 0.6)
151-
UIFloatMenu.transform = CGAffineTransform(translationX: 0, y: backTranslationAmount)
152-
}
153-
}
154-
155-
if gesture.direction(in: view) == .Down {
156-
for order in 0..<queue.count-1 {
157-
if let UIFloatMenu = currentVC.view.viewWithTag(queue[order].uuid) {
158-
if velocity.y > 180 {
159-
UIView.animate(withDuration: 0.2, animations: {
160-
UIFloatMenu.transform = .identity
161-
})
162-
} else {
163-
if UIFloatMenu.frame.origin.y <= initY[order] {
164-
let backTranslationAmount = translation.y >= 0 ? translation.y : -pow(abs(translation.y), 0.6)
165-
UIFloatMenu.transform = CGAffineTransform(translationX: 0, y: backTranslationAmount)
166-
}
167-
}
168-
}
169-
}
170-
}
171-
172146
view.transform = CGAffineTransform(translationX: 0, y: translationAmount)
173147
}
174148

@@ -186,33 +160,9 @@ class UIFloatMenu {
186160
}
187161
}
188162

189-
// MARK: - correctPosition()
190-
static public func correctPosition(_ position: UIFloatMenuPresentStyle) -> UIFloatMenuPresentStyle {
191-
let device = UIDevice.current.userInterfaceIdiom
192-
193-
if device == .pad {
194-
let layout = Layout.determineLayout()
195-
196-
if layout == .iPadOneThirdScreen {
197-
if case .center = position {
198-
return .center
199-
}
200-
return .default
201-
}
202-
return position
203-
} else if device == .phone {
204-
if case .center = position {
205-
return .center
206-
}
207-
return .default
208-
} else {
209-
return position
210-
}
211-
}
212-
213163
// MARK: - closeMenu()
214164
static private func closeMenu(_ menuView: UIView) {
215-
let correct = correctPosition(viewConfig.presentation)
165+
let correct = UIFloatMenuHelper.correctPosition(viewConfig.presentation)
216166
let appRect = UIApplication.shared.windows[0].bounds
217167
let topPadding = UIFloatMenuHelper.getPadding(.top)
218168

@@ -268,9 +218,10 @@ class UIFloatMenu {
268218
}
269219

270220
menuView.alpha = 0
271-
UIView.animate(withDuration: animationDuration, animations: {
221+
let animator = UIViewPropertyAnimator(duration: animationDuration, dampingRatio: 1.0) {
272222
menuView.alpha = 1
273-
})
223+
}
224+
animator.startAnimation()
274225
break
275226
case .default:
276227
if iPad_window_width != 0 {
@@ -279,9 +230,10 @@ class UIFloatMenu {
279230
if animation {
280231
menuView.center = CGPoint(x: appRect.width/2, y: getPrepare)
281232

282-
UIView.animate(withDuration: animationDuration, delay: 0, options: .transitionCurlUp, animations: {
233+
let animator = UIViewPropertyAnimator(duration: animationDuration, dampingRatio: 1.0) {
283234
menuView.center.y = getShow
284-
})
235+
}
236+
animator.startAnimation()
285237
} else {
286238
menuView.center = CGPoint(x: appRect.width/2, y: getShow)
287239
}
@@ -293,9 +245,10 @@ class UIFloatMenu {
293245

294246
if animation {
295247
menuView.alpha = 0
296-
UIView.animate(withDuration: animationDuration, animations: {
248+
let animator = UIViewPropertyAnimator(duration: animationDuration, dampingRatio: 1.0) {
297249
menuView.alpha = 1
298-
})
250+
}
251+
animator.startAnimation()
299252
}
300253
break
301254
case .leftDown(let overToolBar):
@@ -304,9 +257,10 @@ class UIFloatMenu {
304257

305258
if animation {
306259
menuView.alpha = 0
307-
UIView.animate(withDuration: animationDuration, delay: 0, options: .curveEaseOut, animations: {
260+
let animator = UIViewPropertyAnimator(duration: animationDuration, dampingRatio: 1.0) {
308261
menuView.alpha = 1
309-
})
262+
}
263+
animator.startAnimation()
310264
}
311265
break
312266
case .rightUp(let overNavBar):
@@ -318,9 +272,10 @@ class UIFloatMenu {
318272

319273
if animation {
320274
menuView.alpha = 0
321-
UIView.animate(withDuration: animationDuration, animations: {
275+
let animator = UIViewPropertyAnimator(duration: animationDuration, dampingRatio: 1.0) {
322276
menuView.alpha = 1
323-
})
277+
}
278+
animator.startAnimation()
324279
}
325280
}
326281
break
@@ -333,9 +288,10 @@ class UIFloatMenu {
333288

334289
if animation {
335290
menuView.alpha = 0
336-
UIView.animate(withDuration: animationDuration, animations: {
291+
let animator = UIViewPropertyAnimator(duration: animationDuration, dampingRatio: 1.0) {
337292
menuView.alpha = 1
338-
})
293+
}
294+
animator.startAnimation()
339295
}
340296
}
341297
break

Sources/UIFloatMenuController.swift

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ class UIFloatMenuController: UIViewController, UIGestureRecognizerDelegate {
1616
public var config = UIFloatMenuConfig()
1717
public var actions = [UIFloatMenuAction]()
1818

19-
var closeDelegate: UIFloatMenuCloseDelegate?
20-
var textFieldDelegate: UIFloatMenuTextFieldDelegate?
19+
public var delegate = Delegates()
2120

2221
lazy private var backgroundView = UIView()
2322

@@ -83,8 +82,8 @@ class UIFloatMenuController: UIViewController, UIGestureRecognizerDelegate {
8382
menu.currentVC = currentVC
8483
menu.headerConfig = header
8584
menu.viewConfig = config
86-
menu.closeDelegate = closeDelegate
87-
menu.textFieldDelegate = textFieldDelegate
85+
menu.delegate.close = delegate.close
86+
menu.delegate.textField = delegate.textField
8887
menu.show(self, actions: actions)
8988
}
9089

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

113-
if closeDelegate != nil {
114-
closeDelegate?.UIFloatMenuDidCloseMenu()
112+
if delegate.close != nil {
113+
delegate.close?.UIFloatMenuDidCloseMenu()
115114
}
116115
}
117116

118-
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
119-
return true
120-
}
121-
122117
// MARK: - detect theme changes and windows resize
123118
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
124119
super.traitCollectionDidChange(previousTraitCollection)
@@ -170,10 +165,6 @@ class UIFloatMenuController: UIViewController, UIGestureRecognizerDelegate {
170165
}
171166
}
172167
}
173-
} else if traitCollection.horizontalSizeClass == .regular {
174-
print("horizontalSizeClass-regular")
175-
} else {
176-
print("horizontalSizeClass-unspecified")
177168
}
178169
}
179170

@@ -233,7 +224,7 @@ class UIFloatMenuController: UIViewController, UIGestureRecognizerDelegate {
233224
if Orientation.isLandscape {
234225
menuView.center = self.view.center
235226
} else {
236-
menu.showTo(menuView, positions: menu.correctPosition((self.queue.last?.config.presentation)!))
227+
menu.showTo(menuView, positions: UIFloatMenuHelper.correctPosition((self.queue.last?.config.presentation)!))
237228
}
238229
}
239230
}

Sources/UIFloatMenuDelegate.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55

66
import UIKit
77

8+
//MARK: - Delegates
9+
public struct Delegates {
10+
var close: UIFloatMenuCloseDelegate!
11+
var textField: UIFloatMenuTextFieldDelegate!
12+
}
13+
814
//MARK: - UIFloatMenuCloseDelegate
915
public protocol UIFloatMenuCloseDelegate: AnyObject {
1016

Sources/UIFloatMenuHelper.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,30 @@ class UIFloatMenuHelper {
108108
return backView
109109
}
110110

111+
// MARK: - correctPosition()
112+
static func correctPosition(_ position: UIFloatMenuPresentStyle) -> UIFloatMenuPresentStyle {
113+
let device = UIDevice.current.userInterfaceIdiom
114+
115+
if device == .pad {
116+
let layout = Layout.determineLayout()
117+
118+
if layout == .iPadOneThirdScreen {
119+
if case .center = position {
120+
return .center
121+
}
122+
return .default
123+
}
124+
return position
125+
} else if device == .phone {
126+
if case .center = position {
127+
return .center
128+
}
129+
return .default
130+
} else {
131+
return position
132+
}
133+
}
134+
111135
}
112136

113137
// MARK: - Detect gesture direction

Sources/UIFloatMenuView/UIFloatMenuHeaderView.swift

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,13 @@
44
//
55

66
import UIKit
7-
import Foundation
87

98
class UIFloatMenuHeaderView: UIView {
109

1110
private var stackView: UIStackView = UIStackView()
1211

13-
var menuConfig = UIFloatMenuConfig()
1412
var headerConfig = UIFloatMenuHeaderConfig()
1513

16-
private var viewWidth = CGFloat()
17-
1814
private var headerHeight: CGFloat = 60
1915

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

5955
self.headerConfig = headerConfig
60-
self.menuConfig = menuConfig
61-
self.viewWidth = width
6256

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

6963
if headerConfig.showLine {
7064
lineView.frame.size = CGSize(width: width-(headerConfig.lineInset*2), height: 1)
71-
lineView.center.x = viewWidth/2
65+
lineView.center.x = width/2
7266
addSubview(lineView)
7367
}
7468

@@ -97,8 +91,8 @@ class UIFloatMenuHeaderView: UIView {
9791

9892
stackView.isUserInteractionEnabled = true
9993

100-
let pan = UITapGestureRecognizer(target: self, action: #selector(tapClose(_:)))
101-
cancelButton.addGestureRecognizer(pan)
94+
let tap = UITapGestureRecognizer(target: self, action: #selector(tapClose(_:)))
95+
cancelButton.addGestureRecognizer(tap)
10296
}
10397

10498
//MARK: - coder

0 commit comments

Comments
 (0)