基本事件包括begin,canceled,move,ended四项,如果对象的hidden属性为yes,则无效果,hidden属性必须为no;才能使用:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{//触碰开始

//    NSLog(@"%ld",[touches count]);

if ([[event allTouches]count]==2) {

NSArray * one =[[event allTouches]allObjects];

_tiLabel.hidden=NO;

_yLabel.hidden=NO;

_tiLabel.center=[[one objectAtIndex:0] locationInView:self.view];

_yLabel.center=[[one objectAtIndex:1] locationInView:self.view];

}

}

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{//触控发生意外终止是

_tiLabel.hidden=YES;_yLabel.hidden=YES;

}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{//触控结束时

_tiLabel.hidden=YES;_yLabel.hidden=YES;

}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event//触控移动时

{

//    NSLog(@"%ld",[[event allTouches]count]);

if ([[event allTouches]count]==2) {

NSArray * one =[[event allTouches] allObjects];

_tiLabel.hidden=NO;

_yLabel.hidden=NO;

_tiLabel.center=[[one objectAtIndex:0] locationInView:self.view];

_yLabel.center=[[one objectAtIndex:1] locationInView:self.view];

}

}

下面是六大常用事件,包括:点击,拖动,捏合,旋转,长按以及轻扫

  点击事件:顾名思义(UITapGestureRecognizer)

  拖动事件:拖动view内的对象(UIPanGestureRecognizer)

  捏合事件:主要用于操作对象的方法以及缩小(UIPinchGestureRecognizer)

  旋转事件:主要用于控制对象的旋转角度(UIRotationGestureRecognizer)

  长按事件:顾名思义(UILongPressGestureRecognizer)

  清扫事件:主要add在view内,轻扫又可以按照属性分为上下左右四向的清扫(UISwipeGestureRecognizer)

想对某个对象添加六大事件,对象的userinteractionEnable属性必须为yes;否则六大事件会无响应:

- (void)viewDidLoad {

[super viewDidLoad];

UIImage * pic =[UIImage imageNamed:@"rmb.jpg"];

UIImageView * imgView = [[UIImageView alloc]initWithImage:pic];

imgView . backgroundColor = [UIColor blackColor];

imgView.frame =CGRectMake(0, 0, 300, 200);

imgView . userInteractionEnabled=YES;/**********该项设置必须为yes************/

[self.view addSubview:imgView];//imageview对象的代码创建

/*------------------------下面是事件对象的创建以及add操作---------------------*/

//点击

UITapGestureRecognizer * tapGestureRecognizer=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)];

tapGestureRecognizer.numberOfTapsRequired=1;

tapGestureRecognizer.numberOfTouchesRequired=1;

  //设置点击事件的单击次数以及手指个数

[imgView addGestureRecognizer:tapGestureRecognizer];

//拖动

UIPanGestureRecognizer * panGestureRecognizer=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan:)];

[imgView addGestureRecognizer:panGestureRecognizer];

//旋转

UIRotationGestureRecognizer * rotationGestureRecognizer =  [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(handleRotation:)];

[imgView addGestureRecognizer:rotationGestureRecognizer];

//捏合

UIPinchGestureRecognizer * pinchGestureRecognizer= [[UIPinchGestureRecognizer alloc]initWithTarget:self  action:@selector(handlePinch:)];

[imgView addGestureRecognizer:pinchGestureRecognizer];

//长按

UILongPressGestureRecognizer * longGestureRecognizer = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLong:)];

[imgView addGestureRecognizer:longGestureRecognizer];

//清扫

UISwipeGestureRecognizer * leftGestureRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipe:)];

leftGestureRecognizer.direction=UISwipeGestureRecognizerDirectionLeft;//设置清扫的方向

UISwipeGestureRecognizer * rightGestureRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipe:)];

rightGestureRecognizer.direction=UISwipeGestureRecognizerDirectionRight;//设置清扫的方向

[self.view addGestureRecognizer:leftGestureRecognizer];

[self.view addGestureRecognizer:rightGestureRecognizer];

}

-(void)handleTap:(UITapGestureRecognizer * )recognizer {

//     NSLog(@"向左清扫");

[[[UIAlertView alloc]initWithTitle:@"提示" message:@"点击事件发生" delegate:self cancelButtonTitle:nil otherButtonTitles:@"ok",nil] show];

}//点击处理

-(void)handlePan:(UIPanGestureRecognizer *) recognizer{

UIImageView * current=(UIImageView *)recognizer.view;//获取imageview对象

CGPoint translaation=[recognizer translationInView:recognizer.view];//获得移动的坐标

current.center=CGPointMake(current.center.x+translaation.x, current.center.y+translaation.y);

//使用原先坐标加上移动后的坐标,赋值给imageview对象

[recognizer setTranslation:CGPointZero inView:self.view];

//清零,防止再次移动

}//拖动处理

-(void)handleRotation:(UIRotationGestureRecognizer *) recognizer{

recognizer . view . transform=CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);

}//旋转处理

-(void) handlePinch:(UIPinchGestureRecognizer *) recognizer{

recognizer . view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);

recognizer.scale=1;

}//捏合处理

-(void) handleSwipe:(UISwipeGestureRecognizer *) recognizer{

if (recognizer.direction==UISwipeGestureRecognizerDirectionRight) {

NSLog(@"向右清扫");

}else if(recognizer.direction==UISwipeGestureRecognizerDirectionLeft){

NSLog(@"向左清扫");

}

}//清扫处理

-(void) handleLong:(UILongPressGestureRecognizer *) recognizer{

NSLog(@"长按事件");

}//长按处理

ios手势操作,四个基本事件与六个常用事件的更多相关文章

  1. iOS 手势操作:拖动、捏合、旋转、点按、长按、轻扫、自定义

    1.UIGestureRecognizer 介绍 手势识别在 iOS 中非常重要,他极大地提高了移动设备的使用便捷性. iOS 系统在 3.2 以后,他提供了一些常用的手势(UIGestureReco ...

  2. iOS手势操作,拖动,轻击,捏合,旋转,长按,自定义(http://www.cnblogs.com/huangjianwu/p/4675648.html)

    1.UIGestureRecognizer 介绍 手势识别在 iOS 中非常重要,他极大地提高了移动设备的使用便捷性. iOS 系统在 3.2 以后,他提供了一些常用的手势(UIGestureReco ...

  3. ios手势

    iOS 手势操作:拖动.捏合.旋转.点按.长按.轻扫.自定义 大 中 小   1.UIGestureRecognizer 介绍 手势识别在 iOS 中非常重要,他极大地提高了移动设备的使用便捷性. i ...

  4. ios的手势操作之UIGestureRecognizer浅析

    转载地址:http://blog.csdn.net/likendsl/article/details/7554150 每一个手势的实现例子,可参考下面网址:http://www.cnblogs.com ...

  5. IOS各种手势操作实例

    先看下效果 手势相关的介绍 IOS中手势操作一般是 UIGestureRecognizer 类的几个手势子类去实现,一般我们用到的手势就这么5种: 1.点击  UITapGestureRecogniz ...

  6. 【转】 ios的手势操作之UIGestureRecognizer浅析

    一.概述 iPhone中处理触摸屏的操作,在3.2之前是主要使用的是由UIResponder而来的如下4种方式: - (void)touchesBegan:(NSSet *)touches withE ...

  7. iOS项目之“返回”手势操作相关

    在程序中,总会设置“返回”按钮,但不可能在每一个控制器中都去设置一次“返回”按钮,那如何设置全局的“返回”按钮呢? 首先自定义一个导航控制器,在tabBarController中添加子控制器时,使用这 ...

  8. APP 自动化之手势操作appium提供API详解(四)

    一.手势操作1.上下左右滑屏 swipe---滑动 java-client 4.x 是有swipe方法的,可以通过传递坐标信息就可以完成滑动androidDriver.swipe(startx, st ...

  9. iOS手势UIGestureRecognizer的使用及手势冲突的解决办法【转】

    转自:iOS开发中的手势体系——UIGestureRecognizer分析及其子类的使用 关于手势的一篇很好的帖子,转载过来免得丢失.你可能最感兴趣的是手势间的互斥处理,那么就搜索 4.手势间的互斥处 ...

随机推荐

  1. 微软消息分析器(Microsoft Message Analyzer )更新至1.2版-2015-1-20

    就在刚才,收到了微软Connect的邮件推送,大名鼎鼎的微软消息分析器更新至1.2版,并且有公众下载链接,大家可以在这里进行下载. 这里简单摘录一下博客里面提到的新版所增加的功能与功能的改进方面. G ...

  2. 关于《selenium2自动测试实战--基于Python语言》

    关于本书的类型: 首先在我看来技术书分为两类,一类是“思想”,一类是“操作手册”. 对于思想类的书,一般作者有很多年经验积累,这类书需要细读与品位.高手读了会深有体会,豁然开朗.新手读了不止所云,甚至 ...

  3. git开发流程、常用命令及工具、TortoiseGit使用及常见问题

    根据我最近使用git的一些经历,git是基于分支的版本控制工具,分支有远程分支和本地分支. 一.开发流程 - 从远程服务器的master,clone一份项目文件到本地,然后本地master的基础上br ...

  4. 到处都是坑的微信支付V3之 微信支付回调页面

    据上次 到处都是坑的微信支付V3 后很多园友在被虐了千百遍后终于跳转到了亲切的微信支付界面,但输入密码支付后却不知道怎么处理了,接下来补上支付后的处理流程. 1. html中根据前台支付后反馈信息成功 ...

  5. 3.Java基础之Date对象

    毕向东老师Java基础学习笔记——Date对象 今天学习Java中的Date对象后,感觉这个对象对我们主要有以下几点用处. 1.获取时间和日期并按照自己定义的格式显示. 2.网站设计时显示时间.  知 ...

  6. java基础面试题

    参考:http://blog.csdn.net/jackfrued/article/details/44921941 说未经允许不转载,我只好参考了. 1.面向对象的特征有哪些方面? 抽象:抽象是将一 ...

  7. 【Python实战】机型自动化标注(搜狗爬虫实现)

    1. 引言 从安卓手机收集上来的机型大都为这样: mi|5 mi|4c mi 4c 2014022 kiw-al10 nem-tl00h 收集的机型大都杂乱无章,不便于做统计分析.因此,标注显得尤为重 ...

  8. springmvc处理上传图片代码(校验图片尺寸、图片大小)

    package com.maizuo.web.controller; import com.maizuo.domain.Result; import com.maizuo.util.Constants ...

  9. AngularJS中的JSONP实践

    欢迎大家指导与讨论: ) 概念 首先呢,Json和JSONP是不一样的哦.Json呢,是众多数据存储的其中一种格式,是数据书写方式的其中一种.好比是大中华众多诗体的一种(比如说是七言诗吧).这种诗体规 ...

  10. CClayer ignoreAnchorPointForPosition 参数的作用

    ignoreAnchorPointForPosition:忽略锚点对于位置的设定.即非位置的设定(比如说缩放),则不受此参数的控制. 并且默认情况下CCLayer的默认锚点是 中点,而不是左下角的点. ...