Skip to content

Instantly share code, notes, and snippets.

@aybekckaya
Created May 13, 2020 23:53
Show Gist options
  • Save aybekckaya/949b759c98a5049a4055c711a7e7a90f to your computer and use it in GitHub Desktop.
Save aybekckaya/949b759c98a5049a4055c711a7e7a90f to your computer and use it in GitHub Desktop.
// MARK: Set Up
extension InteractiveMenu {
fileprivate func setUpUI() {
self.translatesAutoresizingMaskIntoConstraints = true //1
self.frame = CGRect(x: 0, y: 0, width: superview!.frame.size.width, height: superview!.frame.size.height)
self.addSubview(self.viewBlurBackground)
self.viewBlurBackground.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height)
self.addSubview(self.containerView)
self.containerView.frame = CGRect(x: 0, y: self.frame.size.height - self.configuration.containerViewHeight, width: self.frame.size.width , height: self.totalHeightContainerView)
self.viewInContainer = self.dataSource.interactiveMenuViewInContainerView(interactiveMenu: self)
viewInContainer.frame = CGRect(x: 0, y: 0, width: self.containerView.frame.size.width , height: self.configuration.containerViewHeight)
self.containerView.addSubview(viewInContainer)
viewInContainer.layoutIfNeeded()
//2
if let scrollableContent = self.dataSource.interactiveMenuViewScrollableContentView(interactiveMenu: self) {
scrollableContent.addObserver(self, forKeyPath: "contentOffset", options: [.new , .old], context: nil)
self.panGestureScrollableContent = scrollableContent.gestureRecognizers?.first(where: { rec -> Bool in
if let _:UIPanGestureRecognizer = rec as? UIPanGestureRecognizer {
return true
}
return false
}) as? UIPanGestureRecognizer
self.scrollableContentView = scrollableContent
}
self.frameOpenPosition = CGRect(x: 0, y: self.frame.size.height - self.configuration.containerViewHeight, width: self.frame.size.width , height: self.totalHeightContainerView)
self.frameClosedPosition = CGRect(x: 0, y: self.frame.size.height, width: self.frame.size.width , height: self.totalHeightContainerView)
self.containerView.frame = self.frameClosedPosition
self.frame = CGRect(x: 0, y: self.frame.size.height, width: self.frame.size.width, height: self.frame.size.height)
let tapGestureBlurView = UITapGestureRecognizer(target: self, action: #selector(blurViewDidTapped))
self.viewBlurBackground.addGestureRecognizer(tapGestureBlurView)
self.panGestureContainerView = InstantPanGesture(target: self, action: #selector(containerViewPanned(recognizer:))) //3
self.panGestureContainerView.name = "PanContent"
self.containerView.addGestureRecognizer(self.panGestureContainerView)
panGestureContainerView.delegate = self
self.containerView.backgroundColor = self.configuration.containerViewBackgroundColor
}
//4
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if let key = keyPath , key == "contentOffset" , let scroll = object as? UIScrollView {
if scroll.contentOffset.y < 0 {
scroll.setContentOffset(CGPoint(x: scroll.contentOffset.x, y: 0), animated: false )
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment