Поставил себе цель, написать боковое меню без фреймов. Почти сделал его, но проблема с перемещением в правую сторону, как полностью запретить открытие в правую сторону ?
CenterViewController.swiftimport UIKit
class CenterViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(CenterViewController.handlePanGesture(_:)))
self.view.addGestureRecognizer(panGestureRecognizer)
}
enum SideMenuControllerPresentationStyle {
case None
case Started
case Ended
}
var presentationStyle: SideMenuControllerPresentationStyle = .None
func handlePanGesture(recognizer: UIPanGestureRecognizer) {
let gestureIsDraggingFromLeftToRight = (recognizer.velocityInView(view).x > 0)
switch(recognizer.state) {
case .Began:
if gestureIsDraggingFromLeftToRight {
self.view.layer.shadowColor = UIColor.lightGrayColor().CGColor
self.view.layer.shadowOpacity = 1
self.view.layer.shadowOffset = CGSizeZero
self.view.layer.shadowRadius = 10
presentationStyle = .Started
}
if presentationStyle == .Ended {
presentationStyle = .Started
}
case .Changed:
if presentationStyle == .Started {
recognizer.view!.center.x = recognizer.view!.center.x + recognizer.translationInView(view).x
recognizer.setTranslation(CGPointZero, inView: view)
}
case .Ended:
if self.view.frame.origin.x > 125 {
UIView.animateWithDuration(0.2, delay: 0, options: [], animations: {
self.view.frame.origin.x = 250
}, completion: { (Bool) -> Void in
})
self.presentationStyle = .Ended
} else {
UIView.animateWithDuration(0.2, delay: 0, options: [], animations: {
self.view.frame.origin.x = 0
}, completion: { (Bool) -> Void in
})
self.presentationStyle = .None
}
default:
break
}
}
}
ViewController.swiftimport UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let centerController = self.storyboard?.instantiateViewControllerWithIdentifier("CenterViewController") as! CenterViewController
centerController.view.frame = self.view.frame
addChildViewController(centerController)
self.view.addSubview(centerController.view)
}
}