UITouch一般无法直接获取,是通过UIView的touchesBegan等函数获得。

//这四个方法是UIResponder中得方法

// Generally, all responders which do custom touch handling should override all four of these methods.

// Your responder will receive either touchesEnded:withEvent: or touchesCancelled:withEvent: for each

// touch it is handling (those touches it received in touchesBegan:withEvent:).

// *** You must handle cancelled touches to ensure correct behavior in your application.  Failure to

// do so is very likely to lead to incorrect behavior or crashes.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;

前面写了几次提到UITouch,也提到了UITouch的三个代理方法

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;

这里还有几个代理方法

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;//触摸取消事件

- (void)remoteControlReceivedWithEvent:(UIEvent *)event;//注册接受触摸事件的Control

下面写了一个简单的DEMO,触摸时,移动图片

- (void)loadView {

[super loadView];

// Release any retained subviews of the main view.

// e.g. self.myOutlet = nil;

UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(20.0, 50.0, 45.0, 45.0)];

image.image = [UIImage imageNamed:@"1.png"];

image.tag = 100;

[self.view addSubview:image];

}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

{

UITouch *touch = [touches anyObject];

UIImageView *view1 = (UIImageView*)[self.view  viewWithTag:100];

CGPoint point = [touch  locationInView:self.view];

CGRect  frame = view1.frame;

frame.origin = point;

view1.frame = frame;

}

效果

UITouch的用法的更多相关文章

  1. 触摸事件UITouch的用法

    触摸屏幕是iOS设备接受用户输入的主要方式,包括单击.双击.拨动以及多点触摸等,这些操作都会产生触摸事件. 在Cocoa中,代表触摸对象的类是UITouch.当用户触摸屏幕后,就会产生相应的事件,所有 ...

  2. iOS多线程之8.NSOPeration的其他用法

      本文主要对NSOPeration的一些重点属性和方法做出介绍,以便大家可以更好的使用NSOPeration. 1.添加依赖 - (void)addDependency:(NSOperation * ...

  3. QuartZ2D __ 简单用法 1

    一. 简单做一个画板 1. 建立一个UIView类 2. 在.m里建立一个延展 3. 分别定义一个起点, 一个终点的结构体属性 . 在建立一个存储路径的数组 @interface DrawView ( ...

  4. iOS开发——UI进阶篇(十二)事件处理,触摸事件,UITouch,UIEvent,响应者链条,手势识别

    触摸事件 在用户使用app过程中,会产生各种各样的事件 一.iOS中的事件可以分为3大类型 触摸事件加速计事件远程控制事件 响应者对象在iOS中不是任何对象都能处理事件,只有继承了UIResponde ...

  5. iOS中Block的基础用法

    本文简介 本章不会对Block做过多的实现研究.只是讲解基本的用法.纯粹基础知识.结合实际项目怎么去做举例.Block使用场景,可以在两个界面的传值,也可以对代码封装作为参数的传递等.用过GCD就知道 ...

  6. Block 的基本用法

    iOS中Block的基础用法 转载自简书 本文简介 本章不会对Block做过多的实现研究.只是讲解基本的用法.纯粹基础知识.结合实际项目怎么去做举例.Block使用场景,可以在两个界面的传值,也可以对 ...

  7. ios开发 <AppName>-Prefix.pch文件的用法详解

    我们知道,每新建立一个工程,比如说HelloWord,在分类SupportingFiles里都会有一个以工程名开头-Prefix.pch结尾的文件,如HelloWord-Prefix.pch.对于这个 ...

  8. [HMLY]10.iOS中block的基础用法

    本文简介 本章不会对Block做过多的实现研究.只是讲解基本的用法.纯粹基础知识.结合实际项目怎么去做举例.Block使用场景,可以在两个界面的传值,也可以对代码封装作为参数的传递等.用过GCD就知道 ...

  9. [HMLY]9.深入浅出-iOS Reactive Cocoa的常见用法

    简介 今天的主角是Reactive Cocoa,聊聊Reactive Cocoa的常见使用:KVO.Target.Delegate.Notification. Reactive Cocoa 是一个重量 ...

随机推荐

  1. Js作用域链及变量作用域

    要理解变量的作用域范围就得先理解作用域链 用var关键字声明一个变量时,就是为该变量所在的对象添加了一个属性. 作用域链:由于js的变量都是对象的属性,而该对象可能又是其它对象的属性,而所有的对象都是 ...

  2. validate插件:验证密码没有空格 用户名是5-10位 至少包含数字和大小写字母中的两种字符

    //校验密码是否含有空格 jQuery.validator.addMethod("notblank", function(value, element) { var pwdblan ...

  3. 《javascript高级程序设计》 第24章 最佳实践 Best Practices

    24.1 可维护性 Maintainability24.1.1 什么是可维护的代码 What Is Maintainable Code?24.1.2 代码约定 Code Conventions 24. ...

  4. hdu--(1025)Constructing Roads In JGShining's Kingdom(dp/LIS+二分)

    Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65 ...

  5. 221. Maximal Square -- 矩阵中1组成的最大正方形

    Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and re ...

  6. target不起作用了

    原因是 <a href="",target></a>中间多了个逗号.

  7. java 分析方法调用过程

    StackTraceElement[] s = new Exception().getStackTrace(); for(int i=0;i<s.length;i++) System.out.p ...

  8. 各种浏览器hack

    Hack是针对不同的浏览器去写不同的CSS样式,从而让各浏览器能达到一致的渲染效果,那么针对不同的浏览器写不同的CSS CODE的过程,就叫CSS HACK,同时也叫写CSS Hack.然后将Hack ...

  9. EF5.X Code First表关联与延迟加载

    1-指定导航属性,会自动生成外键,命名规则为:“表名_主键名”2-默认情况下与导航属性的主键名称相同的字段会自动被标记为外键3-通过[ForeignKey]标记指定实体类的属性为外键,4-方式2的升级 ...

  10. 从客户端中检测到有潜在危险的Request.Form值的解决方法

    描述:从客户端中检测到有潜在危险的Request.Form值的解决方法asp.net 2.0 通常解决办法将.aspx文件中的page项添加ValidateRequest="false&qu ...