Skip to content

Instantly share code, notes, and snippets.

@justdoit0823
Last active March 26, 2017 12:48
Show Gist options
  • Save justdoit0823/3db2f50357424bc4115fd29d14801f0c to your computer and use it in GitHub Desktop.
Save justdoit0823/3db2f50357424bc4115fd29d14801f0c to your computer and use it in GitHub Desktop.
create and close slide subview in iOS with swift
func showSlideView() {
let parentFrame = self.view.frame
let leftframe = CGRect(x: 0, y: 0, width: 0, height: parentFrame.height)
let rightframe = CGRect(x: parentFrame.width * 0.8, y: 0, width: parentFrame.width * 0.2, height: parentFrame.height)
let slideView = UIView(frame: leftframe)
let rightView = UIView(frame: rightframe)
let singleFingerTap = UITapGestureRecognizer(target: self, action: #selector(self.closeSlide))
rightView.addGestureRecognizer(singleFingerTap)
slideView.backgroundColor = UIColor(red: 2, green: 2, blue: 2, alpha: 0.8)
slideView.layer.shadowOpacity = 0.8
rightView.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 0.95)
self.view.addSubview(slideView)
UIView.transition(with: slideView, duration: 1.0, options: UIViewAnimationOptions.curveEaseInOut, animations: ({slideView.frame = CGRect(x: 0, y: 0, width: parentFrame.width * 0.8, height: parentFrame.height)}))
self.view.insertSubview(rightView, belowSubview: slideView)
UIView.transition(with: slideView, duration: 0.5, options: UIViewAnimationOptions.curveEaseInOut, animations: ({rightView.alpha = 1}))
self.leftSubView = slideView
self.rightSubView = rightView
}
func closeSlide(_ sender: UITapGestureRecognizer) {
let parentFrame = self.view.frame
UIView.transition(with: self.leftSubView, duration: 1.0, options: UIViewAnimationOptions.curveEaseInOut, animations: ({self.leftSubView.frame = CGRect(x: 0, y: 0, width: 0, height: parentFrame.height)}))
UIView.transition(with: self.rightSubView, duration: 0.5, options: UIViewAnimationOptions.curveEaseInOut, animations: ({self.rightSubView.alpha = 0}))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment