仿新浪微博 UINavigationController 向左滑动时显示上一个控制器的View.

实现原理,UINavigationController 的 self.view显示时把当前显示的view截图下来,保存到一个数组中。当push一个view时把上一个view的截图放到self.view后面,当self.view向右拖动时显示上一个view。

NavigationController.m

#import "NavigationController.h"

@interface NavigationController ()
/** 存放每个控制器的全屏截图 */
@property (nonatomic, strong) NSMutableArray *images;
@property (nonatomic, strong) UIImageView *lastVcView;
@property (nonatomic, strong) UIView *cover;
@end @implementation NavigationController - (UIImageView *)lastVcView
{
if (!_lastVcView) {
UIWindow *window = [UIApplication sharedApplication].keyWindow;
UIImageView *lastVcView = [[UIImageView alloc] init];
lastVcView.frame = window.bounds;
self.lastVcView = lastVcView;
}
return _lastVcView;
} - (UIView *)cover
{
if (!_cover) {
UIWindow *window = [UIApplication sharedApplication].keyWindow;
UIView *cover = [[UIView alloc] init];
cover.backgroundColor = [UIColor blackColor];
cover.frame = window.bounds;
cover.alpha = 0.5;
self.cover = cover;
}
return _cover;
} - (NSMutableArray *)images
{
if (!_images) {
self.images = [[NSMutableArray alloc] init];
}
return _images;
} - (void)viewDidLoad {
[super viewDidLoad]; // 拖拽手势
UIPanGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(dragging:)];
[self.view addGestureRecognizer:recognizer];
} - (void)dragging:(UIPanGestureRecognizer *)recognizer
{
// 假设仅仅有1个子控制器,停止拖拽
if (self.viewControllers.count <= 1) return; // 在x方向上移动的距离
CGFloat tx = [recognizer translationInView:self.view].x;
if (tx < 0) return; if (recognizer.state == UIGestureRecognizerStateEnded || recognizer.state == UIGestureRecognizerStateCancelled) {
// 决定pop还是还原
CGFloat x = self.view.frame.origin.x;
if (x >= self.view.frame.size.width * 0.5) {
[UIView animateWithDuration:0.25 animations:^{
self.view.transform = CGAffineTransformMakeTranslation(self.view.frame.size.width, 0);
} completion:^(BOOL finished) {
[self popViewControllerAnimated:NO];
[self.lastVcView removeFromSuperview];
[self.cover removeFromSuperview];
self.view.transform = CGAffineTransformIdentity;
[self.images removeLastObject];
}];
} else {
[UIView animateWithDuration:0.25 animations:^{
self.view.transform = CGAffineTransformIdentity;
}];
}
} else {
// 移动view
self.view.transform = CGAffineTransformMakeTranslation(tx, 0); UIWindow *window = [UIApplication sharedApplication].keyWindow;
// 加入截图到最后面
self.lastVcView.image = self.images[self.images.count - 2];
[window insertSubview:self.lastVcView atIndex:0];
[window insertSubview:self.cover aboveSubview:self.lastVcView];
}
} /**
* 产生截图
*/
- (void)createScreenShot
{
UIGraphicsBeginImageContextWithOptions(self.view.frame.size, YES, 0.0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
[self.images addObject:image];
} - (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated]; if (self.images.count > 0) return; [self createScreenShot];
} - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[super pushViewController:viewController animated:animated]; [self createScreenShot];
} @end

最新文章

  1. DSP的Gel作用
  2. ASP.NET MVC 5 - 添加一个模型
  3. 让你的Mac支持NTFS
  4. office2003安装公式编辑器mathtype5.2
  5. matlab 画图数据导入
  6. 关于浮动-float
  7. 让UITableViewCell的分隔线宽度等于屏幕的宽度
  8. bzoj3995[SDOI2015]道路修建
  9. HDU_2553——n皇后问题,作弊
  10. css系列教程--文本
  11. 在eclipse上安装 Marketplace Client
  12. Klockwork告警常见错误
  13. pwnable.kr-fd-Writeup
  14. MyEclipse使用(一)
  15. 关于[LeetCode]Factorial Trailing Zeroes O(logn)解法的理解
  16. The packages can be overrided by Java Endorsed Standards
  17. jdk安装及配置
  18. AssetManager
  19. spring载入外部配置文件的方法
  20. CDH hive-1.1.0-cdh5.10.0 安装

热门文章

  1. WinDbg分析DMP文件方法完全攻略
  2. C++ 11 右值引用以及std::move
  3. 计算机视觉与模式识别代码合集第二版one
  4. 基于Predictive Parsing的ABNF语法分析器(十三)——rulelist、rule、rulename、define-as和elements
  5. UDP 通信
  6. selenium webdriver缺陷
  7. Wix打包系列(七) 添加系统必备组件的安装程序
  8. hdu1267(递推)
  9. hdu1561(树形dp)
  10. Linux 远程查看tomcat控制台