9.UITouch 保存手指的信息(触摸的点
2015-11-14 17:58
585 查看
UITouch 保存手指的信息(触摸的点)
视图触摸事件TouchView
1.首先创建一个继承于View的类@interface TouchView : UIView
2.把根视图的View换成自定义视图TouchView
需要让TouchView 来处理事件 (响应事件)
实现响应者类中的方法 来捕获触摸事件
TouchView *touchView = [[TouchView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
touchView.backgroundColor = [UIColor redColor];
[self.view addSubview:touchView];
3.在TouchView.m处理触摸事件
开始触摸触发这个方法
- (void)touchesBegan:(NSSet )touches withEvent:(UIEvent )event
{
NSLog(@"开始摸");
UITouch 保存手指的信息(触摸的点)
UITouch *touch = [touches anyObject];
取出当前 触摸的点
返回一个当前触摸的点 相对于传进去的参数View. selfx相当于自定的视图TouchView
CGPoint p1 = [touch locationInView:self];
NSLog(@”%@”,NSStringFromCGPoint(p1));
返回当前点的上一个的点 相对于串进去的参数View
CGPoint p2 = [touch previousLocationInView:self];
NSLog(@”%@”,NSStringFromCGPoint(p2));
}
触摸中持续触发
- (void)touchesMoved:(NSSet )touches withEvent:(UIEvent )event
{
NSLog(@”触摸中”);
UITouch 保存手指的信息(触摸的点)
UITouch *touch = [touches anyObject];
取出当前点
CGPoint p1 = [touch locationInView:self];
取出当前点的上一个点
CGPoint p2 = [touch previousLocationInView:self];
计算当前点和上一个点的X轴的偏移量
CGFloat x = p1.x - p2.x;
计算当前点和上一个点的y轴的偏移量
CGFloat y = p1.y - p2.y;
实现视图跟随触摸位置改变
self.center = CGPointMake(self.center.x + x, self.center.y + y);
实现随机变颜色
self.backgroundColor = [UIColor colorWithRed:(arc4random()% 256/255.0) green:(arc4random()%256/255.0) blue:(arc4random()%256/255.0) alpha:1];
}
(void)touchesEnded:(NSSet )touches withEvent:(UIEvent )event
{
NSLog(@”抚摸结束”);
}
(void)touchesCancelled:(NSSet )touches withEvent:(UIEvent )event
{
NSLog(@”抚摸被中断 例如触摸中来电话 可以触发中断, 例如小退出”);
}
相关文章推荐
- ui总结
- kendo ui的treeView节点点击事件修改和grid的配置的一点总结
- 九度OJ 1262:Sequence Construction puzzles(I)_构造全递增序列 (DP)
- 九度OJ 1262:Sequence Construction puzzles(I)_构造全递增序列 (DP)
- UI第四天 触摸 响应者链 target-action
- TI DSP TMS320C66x学习笔记之通用并行端口uPP(二)
- iOS UI CodeSnippet
- Teamviewer QuickSupport - 在电脑上远程连接控制手机/平板的软件神器 (Android/iOS)
- 爬爬爬之路:UI(四) 晃动响应 触摸响应 响应者链 显示动态图片
- sicily 1209. Sequence Sum Possibi
- Guest Speaker on 2015 WinHEC Shenzhen 秋季大会
- ural 1247. Check a Sequence
- [Leetcode]Range Sum Query 2D - Immutable
- GPUImage API文档之GPUImageFramebufferCache类
- UITableView插入Section
- 关于UITableView的间隔问题(header和footer)和分割线
- iOS UITableView head view与cell view文字垂直居中,文字右对齐
- 随意细解:UI -- 事件处理
- Java小程序之GUI开发简单前台登录界面
- UI基础-事件处理