現(xiàn)在這些框架很多第三方資源翼闹,但是想自己寫一個(gè)。側(cè)滑實(shí)現(xiàn)比較簡單允青,根據(jù)自己需求去擴(kuò)展界面橄碾。
創(chuàng)建一個(gè)類MenuViewController作為Navigation的rootVC
新建一個(gè)UIView(MenuView)作為左側(cè)漏出來的一部分,新建一個(gè)ContentViewController作為右側(cè)可拖動(dòng)的部分颠锉。將兩個(gè)分別添加到MenuViewController上給ContentViewController的view加上拖拽的手勢法牲。
實(shí)現(xiàn)手勢的事件方法,側(cè)滑效果就完成了琼掠。
- (void)panGesture:(UIPanGestureRecognizer *)recognizer {
CGPoint point = [recognizer translationInView:self.contenViewController.view];
recognizer.view.center = CGPointMake(recognizer.view.center.x + point.x, recognizer.view.center.y);
CGFloat viewX = recognizer.view.frame.origin.x;
CGFloat viewY = recognizer.view.frame.origin.y;
if (viewX < 0) {
recognizer.view.frame = CGRectMake(0, viewY, recognizer.view.frame.size.width, recognizer.view.frame.size.height);
} else if (viewX >= self.view.frame.size.width * 0.8) {
recognizer.view.frame = CGRectMake(self.view.frame.size.width * 0.8, viewY, recognizer.view.frame.size.width, recognizer.view.frame.size.height);
} else {
recognizer.view.frame = CGRectMake(viewX + point.x, viewY, recognizer.view.frame.size.width, recognizer.view.frame.size.height);
}
if (recognizer.state == UIGestureRecognizerStateEnded) {
if (viewX < 120) {
[UIView animateWithDuration:0.5 animations:^{
recognizer.view.frame = CGRectMake(0, viewY, recognizer.view.frame.size.width, recognizer.view.frame.size.height);
}];
}
if (viewX > 120) {
[UIView animateWithDuration:0.5 animations:^{
recognizer.view.frame = CGRectMake(self.view.frame.size.width * 0.8, viewY, recognizer.view.frame.size.width, recognizer.view.frame.size.height);
}];
}
}
[recognizer setTranslation:CGPointZero inView:self.contenViewController.view];
}