对于不能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. Java单例模式——并非看起来那么简单

    版权声明:本文为[viclee]原创,如需转载请注明出处~ Java中单例(Singleton)模式是一种广泛使用的设计模式.单例模式的主要作用是保证在Java程序中,某个类只有一个实例存在.一些管理 ...

  2. linux查看java jdk安装路径和设置环境变量

    一:查看类型 windows: set java_home:查看JDK安装路径 java -version:查看JDK版本 linux: whereis java which java (java执行 ...

  3. WebDataGrid设置某行某列的值

    <ig:WebDataGrid ID="grid"  OnRowSelectionChanged="grid_RowSelectionChanged" O ...

  4. Android学习笔记——Bundle

    该工程的功能是实现不同线程之间数据的传递 以下代码是MainActivity.java中的代码 package com.example.bundle; import android.app.Activ ...

  5. CSS 中背景图片定位方法

    三种: 关键字:background-position: top left; 像素:background-position: 0px 0px; 百分比:background-position: 0% ...

  6. Java/JavaWeb中读取资源文件

    1.一般工程中使用I/O类指定文件的绝对路径读取 FileInputStream fis = new FileInputStream("src/main/resources/zsm.prop ...

  7. Linux版MonoDevelop无法连接调试器的解决方案(Could not connet to the debugger)

    安装了Linux版本的MonoDevelop之后,在运行程序的时候会提示Could not connnet to the debugger.的错误. 原因是新版本的Gnome Terminal不再接受 ...

  8. Visual Studio 2012中Visual Assist破解办法

    本工具有两种破解方式 1.破解方式一:Visual_Assist_X_DiE.exe 运行Visual_Assist_X_DiE.exe,勾选安装的VC版本,点击“Patch”按钮即可. 2.破解方式 ...

  9. php构造函数extends

    extends的继续关系 page继承Dapta:也就是page拥有data的所有功能. <?php class Data{ function f(){ "; } } class Pa ...

  10. JS获取checkbox的个数

    本文算是转载自网络,当时用了他的函数,现在想总结一下,但忘了原文地址了 ================================================================ ...