侧滑栏的实现

2017-06-02  本文已影响0人  真的很菜
效果图

现在这些框架很多第三方资源,但是想自己写一个。侧滑实现比较简单,根据自己需求去扩展界面。

创建一个类MenuViewController作为Navigation的rootVC

新建一个UIView(MenuView)作为左侧漏出来的一部分,新建一个ContentViewController作为右侧可拖动的部分。将两个分别添加到MenuViewController上给ContentViewController的view加上拖拽的手势。

实现手势的事件方法,侧滑效果就完成了。

- (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];

}


demo下载

上一篇下一篇

猜你喜欢

热点阅读