对于不能addTarget的UI对象,添加手势为他们带来了“福音”,以为UIView添加手势为例,揭开手势的面目。

1,创建一个view先,

UIView * jrView=[[UIViewalloc] initWithFrame:CGRectMake(, , , )];

    jrView.center=self.view.center;

    jrView.backgroundColor=[UIColorgreenColor];

    [self.viewaddSubview:jrView];

2,添加手势

常用的手势:点击、长按、扫一下(表面滑过)、拖动、旋转、捏合。。

①:点击

首先,定义点击手势,然后设置手势的相关属性(点击次数、接触点的个数。。),最后将手势添加到view上即可。

点击了view就执行手势的@selector(tapGestureAction:)方法(所有的手势都是这样);

/** --点击-- */
UITapGestureRecognizer * tap=[[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(tapGestureAction:)];
//-=--点击次数
tap.numberOfTapsRequired=; //=-=-=触碰点的个数
tap.numberOfTouchesRequired=; //tap.numberOfTouches; readOnly [jrView addGestureRecognizer:tap];

②:长按

定义手势

属性:算是长按的区域,算是长按的时间

添加手势到view

/** --长按-- */
UILongPressGestureRecognizer * lPress=[[UILongPressGestureRecognizeralloc] initWithTarget:selfaction:@selector(longPressAction:)]; //===--=长按的识别区域(在这里面即使移动了,算长按)
lPress.allowableMovement=;
//=-=-==-按的时间-(按多久算长按)
lPress.minimumPressDuration=0.5; [jrView addGestureRecognizer:lPress];

③:拖动

就是这么简洁;

/** 拖动 */

UIPanGestureRecognizer * pan=[[UIPanGestureRecognizeralloc] initWithTarget:selfaction:@selector(panAction:)];

    [jrView addGestureRecognizer:pan];

④:轻扫(表面滑过)

可以设置滑过的方向,不同的方向触发不同的事件;

/**  轻扫  */
UISwipeGestureRecognizer * swipe=[[UISwipeGestureRecognizeralloc] initWithTarget:selfaction:@selector(swipeAction:)]; swipe.direction=UISwipeGestureRecognizerDirectionLeft; [jrView addGestureRecognizer:swipe];

⑤: 旋转

竟然有代理,请查看代理协议;

/**  旋转  */
UIRotationGestureRecognizer * rotation=[[UIRotationGestureRecognizeralloc] initWithTarget:selfaction:@selector(rotationAction:)]; rotation.delegate=self; [jrView addGestureRecognizer:rotation];

⑥: 捏合

要捏合的话,两个手指操作(合情合理);

/**  捏合  */
UIPinchGestureRecognizer * pinch=[[UIPinchGestureRecognizeralloc] initWithTarget:selfaction:@selector(pinchAction:)]; pinch.delegate=self; [jrView addGestureRecognizer:pinch];

同时,还可以设置同一个view响应多个手势,所以有了手势的delegate;

#pragma mark - 代理方法,使操作对象响应多个手势
- (BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ returnYES; }

手势

给UIView添加手势的更多相关文章

  1. UIView添加手势

    _shareImage.userInteractionEnabled=YES; UITapGestureRecognizer *imagegesture=[[UITapGestureRecognize ...

  2. UIView添加支持代码块的手势

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(a ...

  3. 给View添加手势,防止点击View上其他视图触发点击效果

    在开发过程中,我们可能会遇到这个问题. 当我们给一个view添加了手势,但是我们又不想点击view上面的视图也触发手势.如下图: 我们在红色view上添加了手势,但是又不想点击黄色view也触发.其实 ...

  4. UITableViewCell中的UILabel添加手势没有响应的解决方法

    有时候自定义UITableViewCell,且cell中添加了一个UILabel,我们的目的是给该label添加一个手势.但是如果按照常规的添加方法,发现所添加的手势并不能响应.以下为解决方法:将手势 ...

  5. iOS给UIView添加点击事件

    我要给一个UIView对象topView添加点击事件,方法如下: 步骤1:创建手势响应函数 (void)event:(UITapGestureRecognizer *)gesture { //处理事件 ...

  6. 【Swift 2.1】为 UIView 添加点击事件和点击效果

    前言 UIView 不像 UIButton 加了点击事件就会有点击效果,体验要差不少,这里分别通过自定义和扩展来实现类似 UIButton 的效果. 声明 欢迎转载,但请保留文章原始出处:) 博客园: ...

  7. iOS 中 为UIView添加背景图片

    创建UIImage的方法有两种: UIImage *image = [UIImageimageNamed:@"image.jpg"];//这种不释放内存,要缓存 NSString ...

  8. [BS-29] 给UIView添加背景图片

    给UIView添加背景图片 //默认情况下只能设置UIView的背景颜色,不能给UIView设置背景图片,但通过绘图知识可以做到 - (void)drawRect:(CGRect)rect { [su ...

  9. 李洪强iOS开发之添加手势

    李洪强iOS开发之添加手势 02 - 添加手势

随机推荐

  1. SQL 操作语句

    SQL Server T-SQL高级查询 高级查询在数据库中用得是最频繁的,也是应用最广泛的. Ø 基本常用查询 --select select * from student; --all 查询所有 ...

  2. CentOS7搭建hadoop2.6.4+HBase1.1.6

    环境: CentOS7 hadoop2.6.4两个节点:master.slave1 HBase1.1.6 过程: hadoop安装目录:/usr/hadoop-2.6.4 master节点,hadoo ...

  3. 当SVN服务器端IP地址发生变化时,客户端重新定位

    第一种方法: 重新设置URL: 第二种方法: 找到客户端数据库文件 ,在隐藏的文件夹.svn中,找到文件夹中的文件 *.db文件 ,用SQLite打开,修改表Repository中的数据

  4. Java Native Interface Specification

    http://docs.oracle.com/javase/6/docs/technotes/guides/jni/spec/jniTOC.html http://www.ibm.com/develo ...

  5. AssetBundle in Unity 5.0

    http://forum.unity3d.com/threads/new-assetbundle-build-system-in-unity-5-0.293975/

  6. css 父层 透明 子层不透明Alpha

    html代码 <div class="user2-register-bg"> <div class="user2-register-con"& ...

  7. ILMerge

    ILMerge http://www.microsoft.com/en-hk/download/details.aspx?id=17630 ILMerge 下载地址:http://www.micros ...

  8. Haproxy安装配置及日志输出问题

    简介: 软件负载均衡一般通过两种方式来实现:基于操作系统的软负载实现和基于第三方应用的软负载实现.LVS就是基于Linux操作系统实现的一种软负载,HAProxy就是开源的并且基于第三应用实现的软负载 ...

  9. jquery.validate 的ajax验证(转)

    在做网站的时候有一块需要用到jquery.validate插件 ajax方式的方式来验证原始密码是否正确,研究了研究加上博客园朋友的帮助,终于实现了.贴出代码 <script type=&quo ...

  10. SignalR 资料

    链接:http://www.cnblogs.com/royding/p/3919134.html