UITouch触摸事件
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触摸事件的更多相关文章
- iOS开发——UI进阶篇(十二)事件处理,触摸事件,UITouch,UIEvent,响应者链条,手势识别
触摸事件 在用户使用app过程中,会产生各种各样的事件 一.iOS中的事件可以分为3大类型 触摸事件加速计事件远程控制事件 响应者对象在iOS中不是任何对象都能处理事件,只有继承了UIResponde ...
- 触摸事件UITouch的用法
触摸屏幕是iOS设备接受用户输入的主要方式,包括单击.双击.拨动以及多点触摸等,这些操作都会产生触摸事件. 在Cocoa中,代表触摸对象的类是UITouch.当用户触摸屏幕后,就会产生相应的事件,所有 ...
- 触摸事件,手势识别(UITouch,UIGestureRecognizer)
触摸发生时,UIWindow会有一个队列来存放所有的触摸事件,然后再把这些事件发送给对应的hit-test view,hit-test view会通过touch的四个函数来接收这些事件. 四个函数分别 ...
- 触摸事件UITouch的应用
因为UIView或者UIViewController都是继承与UIResponder ,所以都有UITouch这个事件.当用户点击屏幕的时候,会产生触摸事件. 通过UITouch事件,可以监听到开始触 ...
- iOS:触摸控件UITouch、事件类UIEvent
UITouch:触摸控件类 UIEvent:事件类 ❤️❤️❤️UITouch的介绍❤️❤️❤️ 一.触摸状态类型枚举 typedef NS_ENUM(NSInteger, UITouchPhas ...
- iOS 触摸事件与UIResponder(内容根据iOS编程编写)
触摸事件 因为 UIView 是 UIResponder 的子类,所以覆盖以下四个方法就可以处理四种不同的触摸事件: 1. 一根手指或多根手指触摸屏幕 - (void)touchesBegan:(N ...
- iOS开发系列--触摸事件、手势识别、摇晃事件、耳机线控
-- iOS事件全面解析 概览 iPhone的成功很大一部分得益于它多点触摸的强大功能,乔布斯让人们认识到手机其实是可以不用按键和手写笔直接操作的,这不愧为一项伟大的设计.今天我们就针对iOS的触摸事 ...
- iOS中响应者链条-触摸事件
总体来说,分2个步骤: 一,从上到下寻找合适的控件来处理这个触摸事件.如下图,如果点击了黄色4,则UIApplication -> UIWindow -> 1白色 -> 2橙色 -& ...
- iOS触摸事件
触摸常见的事件有以下几种,触摸事件一般写在view文件中,因为viewController文件有可能控制不止一个view,不适合写触摸事件 // 开始触摸 - (void)touchesBegan:( ...
随机推荐
- Maven实战一
转载:http://www.iteye.com/topic/1123221 1. 用Maven 命令创建一个简单的Maven项目 在cmd中运行如下命令: Cmd代码 mvn archetype:ge ...
- VIM default configuration
== Vim的行号.语法显示等设置(.vimrc文件的配置) ==2008年01月18日 星期五 23:01 在终端下使用vim进行编辑时,默认情况下,编辑的界面上是没有显示行号.语法高亮度显示.智能 ...
- 组合数学(Pólya计数原理):UvaOJ 10601 Cubes
Cubes You are given 12 rods of equal length. Each of them is colored in certain color. Your task is ...
- 动态规划(状态压缩):BZOJ 2621 [Usaco2012 Mar]Cows in a Skyscraper
2621: [Usaco2012 Mar]Cows in a Skyscraper Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 303 Sol ...
- bzoj 1602 [Usaco2008 Oct]牧场行走(LCA模板)
1602: [Usaco2008 Oct]牧场行走 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 379 Solved: 216[Submit][Sta ...
- [Sequence Alignment Methods] Cross-Recurrent Plot (CRP)
A recurrence plot (RP) is a straightforward way to visualize characteristics of similar system state ...
- git 七图七模式 -- 工作流
git 是现在最流行的代码合作工作方式,本文通过七张图来描述一下当下最流行的 git 工作模式 集中式工作流 集中式工作流 功能分支工作流 Gitflow工作流 Forking 工作流 Pull Re ...
- 剪花布条 - HDU 2087(简单KMP | 暴力)
分析:基础的练习............... ============================================================================ ...
- Bash 字符串处理命令
字符串长度 str="abc" echo ${#str} 查找子串的位置 str="abc" str1=`expr index $str "a&quo ...
- Java中的字符串流的读取和写入(创建文件并判断重复账户)
各位我又来了!!哎!好心酸!我还没注册到三天!!没法登上博客的首页!!心累!! import java.io.BufferedOutputStream; import java.io.Buffered ...