给UIView添加手势
对于不能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添加手势的更多相关文章
- UIView添加手势
_shareImage.userInteractionEnabled=YES; UITapGestureRecognizer *imagegesture=[[UITapGestureRecognize ...
- UIView添加支持代码块的手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(a ...
- 给View添加手势,防止点击View上其他视图触发点击效果
在开发过程中,我们可能会遇到这个问题. 当我们给一个view添加了手势,但是我们又不想点击view上面的视图也触发手势.如下图: 我们在红色view上添加了手势,但是又不想点击黄色view也触发.其实 ...
- UITableViewCell中的UILabel添加手势没有响应的解决方法
有时候自定义UITableViewCell,且cell中添加了一个UILabel,我们的目的是给该label添加一个手势.但是如果按照常规的添加方法,发现所添加的手势并不能响应.以下为解决方法:将手势 ...
- iOS给UIView添加点击事件
我要给一个UIView对象topView添加点击事件,方法如下: 步骤1:创建手势响应函数 (void)event:(UITapGestureRecognizer *)gesture { //处理事件 ...
- 【Swift 2.1】为 UIView 添加点击事件和点击效果
前言 UIView 不像 UIButton 加了点击事件就会有点击效果,体验要差不少,这里分别通过自定义和扩展来实现类似 UIButton 的效果. 声明 欢迎转载,但请保留文章原始出处:) 博客园: ...
- iOS 中 为UIView添加背景图片
创建UIImage的方法有两种: UIImage *image = [UIImageimageNamed:@"image.jpg"];//这种不释放内存,要缓存 NSString ...
- [BS-29] 给UIView添加背景图片
给UIView添加背景图片 //默认情况下只能设置UIView的背景颜色,不能给UIView设置背景图片,但通过绘图知识可以做到 - (void)drawRect:(CGRect)rect { [su ...
- 李洪强iOS开发之添加手势
李洪强iOS开发之添加手势 02 - 添加手势
随机推荐
- redhat安装VMware tools的方法
如果我们仔细看的话, 就会发现在VMware软件界面的左下角处显示着 "you don't have VMware Tools installed",即我们还没安装VMware T ...
- 使用Java中的动态代理实现数据库连接池
2002 年 12 月 05 日 作者通过使用JAVA中的动态代理实现数据库连接池,使使用者可以以普通的jdbc连接的使用习惯来使用连接池. 数据库连接池在编写应用服务是经常需要用到的模块,太过频繁的 ...
- 安装beautifulsoup4
python scripts下 pip install beautifulsoup4
- Runner站立会议08
会议时间:2016.4.27 21.10~21.25 地点:基教负一层 今天:看日历的代码,网上下的,没有注释 明天:继续看代码 困难:代码看不懂 会议照片: 燃尽图:
- Java I/O流体系
- Object Graph Serialization
http://coding-time.blogspot.com/2008/03/serialize-object-graph-to-xml-in-net.html http://trycatch.me ...
- json跨域
很有意思的两种连接 ,效果相同. 不同之处: aehyok({"result":"我是远程js带来的数据"}); <script type="t ...
- Python基础之【第二篇】
一.作用域 对作用域来说,只要变量在内存里面存在就可以使用: ==: name = 'saneri' print name 二.三元运算 result = 值1 if 条件 else 值2 如果条件为 ...
- ProFTPD <=1.3.5 mod_copy 未授权文件复制漏洞
poc如下: #!/usr/bin/env python# coding=utf-8 """Site: http://www.beebeeto.com/Framework ...
- OC-block
#import <Foundation/Foundation.h> /* block要掌握的东西 1> 如何定义block变量 int (^sumBlock)(int, int); ...