UITouch触摸事件

  • 主要为三个方法
1.-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
2.
3. UITouch *touch = [touches anyObject];
4. CGPoint point = [touch locationInView:self];
5. start = point;
6. end = point;
7.
8.}
9.
10.-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
11.
12. UITouch *touch = [touches anyObject];
13. CGPoint point = [touch locationInView:self];
14. end = point;
15.
16. [self setNeedsDisplay];
17.}
18.
19.-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
20.
21. UITouch *touch = [touches anyObject];
22. CGPoint point = [touch locationInView:self];
23. end = point;
24.
25. [self setNeedsDisplay];
26.
27.
28.}
29.

  • 示例
1.#import "myView.h"
2.
3.@interface myView ()
4.{
5. CGPoint start;
6. CGPoint end;
7.}
8.@end
9.
10.
11.@implementation myView
12.
13.-(instancetype)initWithFrame:(CGRect)frame{
14. if (self = [super initWithFrame:frame]) {
15. self.backgroundColor = [UIColor whiteColor];
16. }
17. return self;
18.}
19.
20.
21.
22.-(void)drawRect:(CGRect)rect{
23.
24. if (start.x != end.x && start.y != end.y) {
25.
26. CGFloat w = fabs(end.x - start.x);
27. CGFloat h = fabs(end.y - start.y);
28.
29. CGFloat x = end.x < start.x ? end.x : start.x;
30. CGFloat y = end.y < start.y ? end.y : start.y;
31.
32. //画圆
33. UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:
34. CGRectMake(x, y, w, h)];
35.
36. //画方
37.// UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(x, y, w, h)];
38.
39. [[UIColor redColor] setStroke];
40. [path stroke];
41. }
42.
43.}
44.
45.-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
46.
47. UITouch *touch = [touches anyObject];
48. CGPoint point = [touch locationInView:self];
49. start = point;
50. end = point;
51.
52.}
53.
54.-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
55.
56. UITouch *touch = [touches anyObject];
57. CGPoint point = [touch locationInView:self];
58. end = point;
59.
60. [self setNeedsDisplay];
61.}
62.
63.-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
64.
65. UITouch *touch = [touches anyObject];
66. CGPoint point = [touch locationInView:self];
67. end = point;
68.
69. [self setNeedsDisplay];
70.
71.
72.}
73.
74.
75.
76.
77.@end
78.

 

UITouch触摸事件的更多相关文章

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

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

  2. 触摸事件UITouch的用法

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

  3. 触摸事件,手势识别(UITouch,UIGestureRecognizer)

    触摸发生时,UIWindow会有一个队列来存放所有的触摸事件,然后再把这些事件发送给对应的hit-test view,hit-test view会通过touch的四个函数来接收这些事件. 四个函数分别 ...

  4. 触摸事件UITouch的应用

    因为UIView或者UIViewController都是继承与UIResponder ,所以都有UITouch这个事件.当用户点击屏幕的时候,会产生触摸事件. 通过UITouch事件,可以监听到开始触 ...

  5. iOS:触摸控件UITouch、事件类UIEvent

    UITouch:触摸控件类   UIEvent:事件类 ❤️❤️❤️UITouch的介绍❤️❤️❤️ 一.触摸状态类型枚举 typedef NS_ENUM(NSInteger, UITouchPhas ...

  6. iOS 触摸事件与UIResponder(内容根据iOS编程编写)

    触摸事件 因为 UIView 是 UIResponder 的子类,所以覆盖以下四个方法就可以处理四种不同的触摸事件: 1.  一根手指或多根手指触摸屏幕 - (void)touchesBegan:(N ...

  7. iOS开发系列--触摸事件、手势识别、摇晃事件、耳机线控

    -- iOS事件全面解析 概览 iPhone的成功很大一部分得益于它多点触摸的强大功能,乔布斯让人们认识到手机其实是可以不用按键和手写笔直接操作的,这不愧为一项伟大的设计.今天我们就针对iOS的触摸事 ...

  8. iOS中响应者链条-触摸事件

    总体来说,分2个步骤: 一,从上到下寻找合适的控件来处理这个触摸事件.如下图,如果点击了黄色4,则UIApplication -> UIWindow -> 1白色 -> 2橙色 -& ...

  9. iOS触摸事件

    触摸常见的事件有以下几种,触摸事件一般写在view文件中,因为viewController文件有可能控制不止一个view,不适合写触摸事件 // 开始触摸 - (void)touchesBegan:( ...

随机推荐

  1. C#程序设计基础——类、对象、方法

    类与对象 类 类是一种构造,通过使用该构造,用户可以将其他类型的变量.方法和事件组合在一起,从而创建自定义类型.类就像一个蓝图,它定义类型的数据和行为. 对象 定义类之后,便可通过将类加载到内存中来使 ...

  2. 获得进程可执行文件的路径: GetModuleFileNameEx, GetProcessImageFileName, QueryFullProcessImageName

    http://blog.csdn.net/bichenggui/article/details/4774457 -------------------------------------------- ...

  3. Android 判断wifi和4G网络是否开启

    public boolean isWifiAvailable() { ConnectivityManager connectivityManager = (ConnectivityManager) g ...

  4. Struts2的注解功能

    我们知道通常情况下,Struts2是通过struts.xml配置的.但是随着系统规模的加大我们需要配置的文件会比较大,虽然我们可以根据不同的系统功能将不同模块的配置文件单独书写,然后通过<inc ...

  5. .net 开发框架

    .NET开发人员值得关注的七个开源项目 2010年07月02日09:33 it168网站原创 作者:黄永兵 编译 编辑:胡铭娅 我要评论(0) [IT168技术分析]微软近几年在.NET社区开源项目方 ...

  6. Farm Irrigation(并查集)

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

  7. Ubuntu 中使用 谷歌日历

    简介 对于经常使用待办类软件的人来说,谷歌日历是个不错的选择.但每次,都要登录网页去查看,对于我这样的懒人来说似乎麻烦了些. 所以在网上找了个叫做 Calendar Indicator 的软件. 效果 ...

  8. Qt入门(7)——窗口几何结构

    QWidget提供了几个处理窗口几何结构的函数.这些函数中的几个操作纯客户区域(例如不包含窗口框架的窗口),其它一些包括窗口框架.它们之间的区别在某种意义上被完成覆盖明显地最普通的方法.包括窗口的框架 ...

  9. ZOJ 3469 Food Delivery

    题目大意: 有n个人,住在一条直线上.第i个人的坐标是Xi,街上有个外卖餐馆的位置是X,现在餐厅工作人员要给街上的每个人送饭,送完之后再回到餐厅,送饭人的速度是V,每个人有个不满意值,当这个人送餐时间 ...

  10. HDOJ 2052 Picture

    Problem Description Give you the width and height of the rectangle,darw it. Input Input contains a n ...