使用UIScreenEdgePanGestureRecognizer写iOS7侧边栏

A UIScreenEdgePanGestureRecognizer looks for panning (dragging) gestures that start near an edge of the screen. The system uses screen edge gestures in some cases to initiate view controller transitions. You can use this class to replicate the same gesture behavior for your own actions.

UIScreenEdgePanGestureRecognizer看起来像pan手势,它是检测屏幕边缘的pan手势的。系统在某些controller转场的时候会使用这个手势。你也可以使用这个手势做其他的事情。

源码:

  1. #import "RootViewController.h"
  2.  
  3. @interface RootViewController ()<UIGestureRecognizerDelegate>
  4.  
  5. {
  6.  
  7. CGFloat _centerX;
  8. CGFloat _centerY;
  9. UIView *_backgroundView;
  10.  
  11. }
  12.  
  13. @end
  14.  
  15. @implementation RootViewController
  16.  
  17. - (void)viewDidLoad
  18. {
  19. [super viewDidLoad];
  20.  
  21. // 存储坐标
  22. _centerX = self.view.bounds.size.width / ;
  23. _centerY = self.view.bounds.size.height / ;
  24. self.view.backgroundColor = [UIColor blackColor];
  25.  
  26. // 屏幕边缘pan手势(优先级高于其他手势)
  27. UIScreenEdgePanGestureRecognizer *leftEdgeGesture = \
  28. [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self
  29. action:@selector(handleLeftEdgeGesture:)];
  30. leftEdgeGesture.edges = UIRectEdgeLeft; // 屏幕左侧边缘响应
  31. [self.view addGestureRecognizer:leftEdgeGesture]; // 给self.view添加上
  32.  
  33. // 设置一个UIView用来替换self.view,self.view用来当做背景使用
  34. _backgroundView = [[UIView alloc] initWithFrame:self.view.bounds];
  35. _backgroundView.backgroundColor = [UIColor yellowColor];
  36. [self.view addSubview:_backgroundView];
  37.  
  38. // 展示的view
  39. UIView *showView_01 = [[UIView alloc] initWithFrame:CGRectMake(, , , )];
  40. showView_01.tag = 0x1;
  41. showView_01.backgroundColor = [UIColor redColor];
  42. [_backgroundView addSubview:showView_01];
  43. }
  44.  
  45. - (void)handleLeftEdgeGesture:(UIScreenEdgePanGestureRecognizer *)gesture
  46. {
  47. // 获取到当前被触摸的view
  48. UIView *view = [self.view hitTest:[gesture locationInView:gesture.view]
  49. withEvent:nil];
  50.  
  51. NSLog(@"tag = %ld", (long)view.tag);
  52.  
  53. if(UIGestureRecognizerStateBegan == gesture.state ||
  54. UIGestureRecognizerStateChanged == gesture.state)
  55. {
  56. // 根据被触摸手势的view计算得出坐标值
  57. CGPoint translation = [gesture translationInView:gesture.view];
  58. NSLog(@"%@", NSStringFromCGPoint(translation));
  59.  
  60. NSLog(@"进行中");
  61.  
  62. // 进行设置
  63. _backgroundView.center = CGPointMake(_centerX + translation.x, _centerY);
  64. }
  65. else
  66. {
  67. // 恢复设置
  68. [UIView animateWithDuration:. animations:^{
  69. _backgroundView.center = CGPointMake(_centerX, _centerY);
  70.  
  71. }];
  72. }
  73. }
  74.  
  75. @end

RootViewController.m

处理手势:

效果如下图:

如果想与其他手势并发操作,实现如下代理即可:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}

使用UIScreenEdgePanGestureRecognizer写iOS7侧边栏的更多相关文章

  1. 使用TFHpple解析html

    使用TFHpple解析html https://github.com/topfunky/hpple 前期准备工作 引入静态库文件 添加库文件的 header search paths(注意,必须选中 ...

  2. iOS html格式解析

    使用TFHpple解析html https://github.com/topfunky/hpple 前期准备工作 引入静态库文件 添加库文件的 header search paths(注意,必须选中 ...

  3. 使用Ant Design写一个仿微软ToDo

    实习期的第一份活,自己看Ant Design的官网学习,然后用Ant Design写一个仿微软ToDo. 不做教学目的,只是记录一下. 1.学习 Ant Design 是个组件库,想要会用,至少要知道 ...

  4. 史上最全的常用iOS的第三方框架

    文章来源:http://blog.csdn.net/sky_2016/article/details/45502921 图像: 1.图片浏览控件MWPhotoBrowser       实现了一个照片 ...

  5. 常用iOS的第三方框架

    图像:1.图片浏览控件MWPhotoBrowser       实现了一个照片浏览器类似 iOS 自带的相册应用,可显示来自手机的图片或者是网络图片,可自动从网络下载图片并进行缓存.可对图片进行缩放等 ...

  6. iOS:抽屉侧滑动画两种形式(1、UIView侧滑 2、ViewController侧滑)

    前言: 在iOS中抽屉动画是很常用的一种技术,使用它有很炫的体验效果,为app增添特色,形式就两种,一个是UIView的侧滑,另一个就是ViewController的侧滑. 实现方式: 抽屉侧滑动画有 ...

  7. 一些Demo链接

    youtube下载神器:https://github.com/rg3/youtube-dl我擦咧vim插件:https://github.com/Valloric/YouCompleteMevim插件 ...

  8. iOS 中有用的开源库

    youtube下载神器:https://github.com/rg3/youtube-dl vim插件:https://github.com/Valloric/YouCompleteMe vim插件配 ...

  9. ios的一些开源资源

    1. http://www.open-open.com/lib/view/open1428646127375.html vim插件:https://github.com/Valloric/YouCom ...

随机推荐

  1. Python练习 | WebServer

    #-*- coding:utf-8 -*- import sys, os from http.server import BaseHTTPRequestHandler, HTTPServer #--- ...

  2. Oracle运算符收录(易忘记,但是又很重要的运算符)

    Create Table Test6( id ), name ), age ), sex ) ) 1. ||   符 字符串连接字符串,注意:文字和日期一定嵌入在单引号里面 select ID,Nam ...

  3. debian sudo

    apt-get install sudo vi /etc/sudoers add CentOS 7 root ALL=(ALL) ALL Debian root ALL=(ALL:ALL) ALL 按 ...

  4. 判断弹出框存在(alert_is_ present)

    系统弹窗这个是很常见的场景,有时候它不弹出来去操作的话,会抛异常.那么又不知道它啥时候会出来,那么久需要去判断弹窗是否弹出了 判断 alert 源码分析 class alert_is_present( ...

  5. iOS设备的屏幕分辨率

    全部列在这里吧.方便自己方便别人.保持更新…… iPhone: iPhone 1G320x480 iPhone 3G320x480 iPhone 3GS320x480 iPhone 4640x960 ...

  6. 编译impala2.0.0

    使用redhat5.8没编译成功,改用redhat6.4最终编译成功. 参考官方的文档https://github.com/cloudera/Impala/tree/v1.2.2 不知道官方的read ...

  7. html的列表

    1.无序列表(最常用) 先看个例子:如下图: 使用的 html标签为  <ul>   <!--ul是UnorderList的缩写,意为无序列表:li是ListItem的缩写,列表项- ...

  8. Program, Process and Thread

    A program is an executable file store. A process is a running program. A thread is a single sequence ...

  9. EntityManager对象管理

    根据EntityManager对象的管理方式,可以有以下两种类型: — 容器托管的(container-managed)EntityManager对象 容器托管的EntityManager对象最简单, ...

  10. poj 2259 Team Queue

    Team Queue Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 2977   Accepted: 1092 Descri ...