iOS的触摸事件的用法以及和手势识别器的区别
1、首先来介绍下触摸事件和手势识别器的利与弊
触摸事件和手势识别器二者之间有直接的关系
手势识别器是在触摸事件的基础上演变过来的
当我们用到触摸事件时 默认的uiview是没有什么效果的 只能自定义view才能实现事件的触摸
通常用到的方法如下:
– touchesBegan:withEvent:
– touchesMoved:withEvent:
– touchesEnded:withEvent:
- touchesCancelled:withEvent: 而手势识别器是在触摸事件的基础上而封装的
为什么要封装呢?因为直接touchs方法来定位手势比较麻烦。在还没有UIGestureRecognizer的时代,用touchs也可以计算出博文中说的几种常用手势:点,滑动,拖等等。这些手势都非常常用,每个开发者想要监视这些手势的时候都要自己判断一遍,每个人都在重复造轮子。那把这些手势封装出来标准化,就有了UIGestureRecognizer和它对应的子类手势了。 那为什么还要有touchs等方法存在呢?因为UIGestureRecognizer几种手势比较有限,有的游戏应用需要搞些特别的手势,那就用touchs这些方法来定义了。
2、使用手势步骤
使用手势很简单,分为两步:
创建手势实例。当创建手势时,指定一个回调方法,当手势开始,改变、或结束时,回调方法被调用。
添加到需要识别的View中。每个手势只对应一个View,当屏幕触摸在View的边界内时,如果手势和预定的一样,那就会回调方法。
ps:一个手势只能对应一个View,但是一个View可以有多个手势。
建议在真机上运行这些手势,模拟器操作不太方便,可能导致你认为手势失效。
3、手势的介绍
UITapGestureRecognizer (敲击)
UIPinchGestureRecognizer (捏合,由于缩放)
UIPanGestureRecognizer (拖拽)
UISwipeGestureRecognizer (轻扫)
UIRotationGestureRecognizer(旋转)
UILongPressGestureRecognizer(长按)
4、敲击手势的用法(其他手势类同)代码如下:
//创建手势识别对象
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
//连续敲击二次手势才能识别成功
tap.numberOfTapsRequired = ;
//需要一根手指触摸
tap.numberOfTouchesRequired = ;
//添加手势识别器对象到对应的uiview
[self.iconView addGestureRecognizer:tap]; //添加监听方法(识别到对应的手势就会监听事件)
[tap addTarget:self action:@selector(btClick:)];
5、缩放 + 旋转
#import "ViewController.h" @interface ViewController ()<UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *iconView; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
self.iconView.userInteractionEnabled = YES;
// Do any additional setup after loading the view, typically from a nib.
[self testPichAndRoate];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void) testPichAndRoate
{
//[self testPich];
[self testRoate];
}
-(void) testPich
{
UIPinchGestureRecognizer *pich = [[UIPinchGestureRecognizer alloc] init];
pich.delegate = self;
[self.iconView addGestureRecognizer:pich];
[pich addTarget:self action:@selector(pinchView:)];
}
-(void) pinchView:(UIPinchGestureRecognizer *)pinchView
{
pinchView.view.transform = CGAffineTransformScale(pinchView.view.transform, pinchView.scale, pinchView.scale);
pinchView.scale = ;//必写
}
-(void) testRoate
{
UIRotationGestureRecognizer *roate = [[UIRotationGestureRecognizer alloc] init];
roate.delegate = self;
[self.iconView addGestureRecognizer:roate];
[roate addTarget:self action:@selector(roateView:)];
}
-(void) roateView:(UIRotationGestureRecognizer *)roate
{
roate.view.transform = CGAffineTransformRotate(roate.view.transform, roate.rotation);
roate.rotation = ;//必写
}
@end
6、拖动
#import "ViewController.h" @interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *panView; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] init];
[self.panView addGestureRecognizer:pan];
[pan addTarget:self action:@selector(panTuoDong:)]; } - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)panTuoDong:(UIPanGestureRecognizer *)pan
{
switch (pan.state) {
case UIGestureRecognizerStateBegan: // 开始触发手势 break; case UIGestureRecognizerStateEnded: // 手势结束 break; default:
break;
}
//在view上挪动的距离
CGPoint transtation = [pan translationInView:pan.view];
CGPoint center = pan.view.center;
center.x += transtation.x;
center.y += transtation.y;
pan.view.center = center; //清除挪动的距离
[pan setTranslation:CGPointZero inView:pan.view];
} @end
iOS的触摸事件的用法以及和手势识别器的区别的更多相关文章
- iOS中—触摸事件详解及使用
iOS中--触摸事件详解及使用 (一)初识 要想学好触摸事件,这第一部分的基础理论是必须要学会的,希望大家可以耐心看完. 1.基本概念: 触摸事件 是iOS事件中的一种事件类型,在iOS中按照事件划分 ...
- iOS的触摸事件
在iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并处理事件,我们称其为@''响应者对象''UIApplication,UIViewController,UIView都 ...
- iOS基础 - 触摸事件&手势识别
================================================================== 一.触摸事件&手势识别 1> 4个触摸事件,针对视图 ...
- iOS基础 - 触摸事件与手势识别
一.iOS的输入事件 UIKit可识别三种类型的输入事件: 触摸事件 运动(加速计)事件 远程控制事件 二.UIEvent iOS中许多事件对象都是UIEvent类的实例,记录事件产生的时刻和类型 U ...
- iOS:触摸事件和手势识别的介绍
触摸事件和手势识别的介绍 1.iOS的输入事件 UIKit可识别三种类型的输入事件: 触摸事件 运动事件 远程控制事件 iOS中许多事件对象都是UIEvent类的实例,UIEvent记录了事件所产生 ...
- 移动端touch触摸事件(滑动效果和手势操作)
一.定义 ①touch是移动端的触摸事件,而且是一组事件,主要有以下事件: touchstart 事件:当手指触摸屏幕的时候触发 touchmove 事件:当手指在屏幕来回滑动的时候触发 touche ...
- 我的IOS学习之路(三):手势识别器
在iOS的学习中,对于手势的处理是极为重要的,如对于图片,我们经常需要进行旋转,缩放以及移动等.这里做一下总结,详见代码. - (void)viewDidLoad { [super viewDidLo ...
- 触摸事件,手势识别(UITouch,UIGestureRecognizer)
触摸发生时,UIWindow会有一个队列来存放所有的触摸事件,然后再把这些事件发送给对应的hit-test view,hit-test view会通过touch的四个函数来接收这些事件. 四个函数分别 ...
- iOS开发系列--触摸事件、手势识别、摇晃事件、耳机线控
-- iOS事件全面解析 概览 iPhone的成功很大一部分得益于它多点触摸的强大功能,乔布斯让人们认识到手机其实是可以不用按键和手写笔直接操作的,这不愧为一项伟大的设计.今天我们就针对iOS的触摸事 ...
随机推荐
- jq中数组应用的错误
js中数组可以这样使用: <ul id="ul"> <li value="1">s</li> <li>f< ...
- python中在同一个位置输出数据
import sys, time def print_data(): for i in range(5): sys.stdout.write(str(i) + '\r') time.sleep(1) ...
- wmic 命令的一个汇总,功能很强大
获得系统版本信息wmic datafile where Name='c:\\windows\\explorer.exe' get Manufacturer,Version,Filename 获得系统进 ...
- 通过页面调用APP【H5与APP互通】
现在H5和App原生的内容原来越互通,所涉及的业务也越来越复杂和融合,所以如何互相之间方便的调用才是王道. 场景1 比如用hybrid获取地理位置和短信信息,这当然需要框架封装好,比如利用框架的bri ...
- 深入剖析tomcat 笔记——第8章 载入器
深入剖析tomcat 笔记 目录:
- sql练习记录
三表关联如果字段为0则表示是散客卡 select a.shop_id as id,b.shop_name,a.balance,a.point,(IF(a.card_type_id<>0,c ...
- ZACC_DOCUMENT
method if_ex_acc_document~change. data: wa_extension type bapiparex, ext_value() type c, wa_accit ty ...
- HTML 表单和输入<textarea><label><fieldset><legend><select><optgroup><option><button>
textarea><label><fieldset><legend><select><optgroup><option>& ...
- winform碎片
1.通过同一个pictureBox控件加载不同的图片,在加载前需要释放控件里之前的Image. pictureBox1.Image.Dispose();//Dispose之后对象就不存在了,只能重新C ...
- EF6 CodeFirst+Repository+Ninject+MVC4+EasyUI实践(三)
前言 在上一篇中,我们依靠着EasyUI强大的前端布局特性把前端登录界面和主界面给搭建完成了.这一篇我们就要尝试着把整个解决方案部署到云端呢,也就是Visual Studio Online(TFVC) ...