对于不能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. jquery 中的 return false 不起作用

    jquery  中的 return false 不起作用 $(".lcId").each(function(e) { if ($(this).attr("checked& ...

  2. WinForm------点击Control弹出MessageBox

    private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { //弹 ...

  3. select()函数以及FD_ZERO、FD_SET、FD_CLR、FD_ISSET

    http://hi.baidu.com/%B1%D5%C4%BF%B3%C9%B7%F0/blog/item/e7284ef16bcec3c70a46e05e.html select函数用于在非阻塞中 ...

  4. HTML学习笔记——锚链接、pre标签、实体

    1>锚链接 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ww ...

  5. Android学习笔记——ProgressBarHandler

    该工程的功能是实现点击按钮进度条按10%递增,使用的方式是Handler 以下的代码是MainActivity.java中的代码 package com.example.progressbarhand ...

  6. OpenGL Tutorial

    https://open.gl https://www.processing.org/tutorials/pshader/

  7. ecshop怎么添加配送方式

    步骤1.打开includes\modules\shipping文件夹,把sto_express.php复制多一份,重名为tt_express.php: 步骤2.打开tt_express.php,ctr ...

  8. msbuild

    https://msdn.microsoft.com/zh-cn/library/ms171466(v=vs.100).aspx

  9. App Extension Today

     App Extensions 是iOS8新开放的扩展机制,之后不断增加功能.App Extension Programming Guide: Today   不喜欢废话,直接上干货!   一:重要概 ...

  10. java中跳出if判断

    今天学到的一点儿新东西一个if判断里面有好多东西,紧接着还有其他代码,不能使用return来结束这个if判断这时候,就需要这样: out:if (!"null".equals(re ...