Commit 3879aac8 by Dmitry Stepanets

Added custom show/hide gestures

parent 58cd9ddb
......@@ -73,7 +73,15 @@ class TodayViewController: UIViewController {
}
@objc private func handleNotificationButton() {
self.coordinator.openNotificationsScreen()
#if DEBUG
if #available(iOS 14, *) {
let promo = WidgetPromotionController(coordinator: WidgetPromotionCoordinator(parentViewController: self))
present(promo, animated: true)
}
#else
fatalError("Remove in release")
#endif
// self.coordinator.openNotificationsScreen()
}
}
......
......@@ -26,7 +26,7 @@ class PromotionPresentationAnimator: NSObject, UIViewControllerAnimatedTransitio
height: contentHeight)
container.addSubview(toViewController.view)
UIView.animate(withDuration: transitionDuration(using: transitionContext)) {
toViewController.view.frame.origin.y = container.frame.height - contentHeight
toViewController.view.frame.origin.y = container.bounds.height * 0.46
} completion: { finished in
transitionContext.completeTransition(finished)
}
......
......@@ -20,12 +20,8 @@ class WidgetPromotionController: UIViewController {
private let stackView = UIStackView()
private let footerView = UIView()
private let learnButton = UIButton()
private var initialTouchPoint = CGPoint(x: 0,y: 0)
private lazy var panGesture: UIPanGestureRecognizer = {
let gesture = UIPanGestureRecognizer(target: self, action: #selector(handlePanGesture(sender:)))
gesture.delegate = self
return gesture
}()
private let fullView: CGFloat = UIScreen.main.bounds.height - UIScreen.main.bounds.height * 0.9
private let partialView: CGFloat = UIScreen.main.bounds.height * 0.46
//Public
var controllerContentHeight: CGFloat {
......@@ -99,26 +95,32 @@ class WidgetPromotionController: UIViewController {
}
}
@objc private func handlePanGesture(sender: UIPanGestureRecognizer) {
let touchPoint = sender.location(in: self.view?.window)
let originalOffsetY = UIScreen.main.bounds.height - self.view.bounds.height
switch sender.state {
case .began:
initialTouchPoint = touchPoint
@objc private func handlePanGesture(recognizer: UIPanGestureRecognizer) {
let translation = recognizer.translation(in: self.view)
let velocity = recognizer.velocity(in: self.view)
let y = self.view.frame.minY
switch recognizer.state {
case .changed:
if touchPoint.y - initialTouchPoint.y > 0 {
view.frame.origin.y = originalOffsetY + (touchPoint.y - initialTouchPoint.y)
self.view.frame = CGRect(x: 0, y: y + translation.y, width: view.frame.width, height: view.frame.height)
recognizer.setTranslation(CGPoint.zero, in: self.view)
case .ended:
var duration = velocity.y < 0 ? Double((y - fullView) / -velocity.y) : Double((partialView - y) / velocity.y )
duration = duration > 0.3 ? 0.3 : duration
if velocity.y >= 0 {
self.dismiss(animated: true)
}
case .cancelled, .ended:
if touchPoint.y - initialTouchPoint.y > 80 {
close()
} else {
UIView.animate(withDuration: 0.25, animations: {
self.view.frame.origin.y = originalOffsetY
})
else {
UIView.animate(withDuration: duration) {
self.view.frame = CGRect(x: 0, y: self.fullView, width: self.view.frame.width, height: self.view.frame.height)
} completion: {[weak self] _ in
if ( velocity.y < 0 ) {
self?.scrollView.isScrollEnabled = true
}
}
}
break
default:
break
}
......@@ -141,7 +143,9 @@ class WidgetPromotionController: UIViewController {
@available(iOS 14, *)
private extension WidgetPromotionController {
func prepareView() {
// view.addGestureRecognizer(panGesture)
let gesture = UIPanGestureRecognizer(target: self, action: #selector(handlePanGesture(recognizer:)))
gesture.delegate = self
view.addGestureRecognizer(gesture)
view.clipsToBounds = true
view.layer.cornerRadius = 24
......@@ -260,11 +264,6 @@ extension WidgetPromotionController: UIScrollViewDelegate {
if scrollView.contentOffset.y == scrollView.contentSize.height - scrollView.frame.height {
AppAnalytics.shared.log(event: .ANALYTICS_WIDGET_BOTTOM_SCROLLED)
}
// if scrollView.contentOffset.y < 0 {
// let originalOffsetY = UIScreen.main.bounds.height - self.view.bounds.height
// view.frame.origin.y = originalOffsetY - (scrollView.contentOffset.y - initialTouchPoint.y)
// return
// }
}
}
......@@ -272,27 +271,17 @@ extension WidgetPromotionController: UIScrollViewDelegate {
//MARK:- UIGesture Delegate
@available(iOS 14, *)
extension WidgetPromotionController: UIGestureRecognizerDelegate {
// func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
// if scrollView.contentOffset.y > 0 {
// return false
// }
//
// scrollView.contentOffset.y = 0
// return true
// }
// func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,
// shouldBeRequiredToFailBy otherGestureRecognizer: UIGestureRecognizer) -> Bool {
// // Do not begin the pan until the swipe fails.
// if otherGestureRecognizer != self.panGesture {
// print("ScrollView Offset \(scrollView.contentOffset.y)")
// if scrollView.contentOffset.y >= 0 {
// return false
// }
//
// return true
// }
//
// return false
// }
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
let gesture = (gestureRecognizer as! UIPanGestureRecognizer)
let direction = gesture.velocity(in: view).y
let y = view.frame.minY
if (y == fullView && scrollView.contentOffset.y == 0 && direction > 0) || (y == partialView) {
scrollView.isScrollEnabled = false
} else {
scrollView.isScrollEnabled = true
}
return false
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment