iOS触摸手势——UITouch
2016-05-16 14:37
453 查看
</pre><pre name="code" class="objc">// 触摸开始 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"touchesBegan - touch count = %d", [touches count]); // 获得触摸点的集合,可以判断多点触摸事件 for (UITouch *touch in event.allTouches) { // 获取当前触摸坐标 CGPoint currentPoint = [touch locationInView:self.view]; NSLog(@"touch.locationInView = {%2.3f, %2.3f}", currentPoint.x, currentPoint.y); // 获取当前触摸前一个坐标 CGPoint previousPoint = [touch previousLocationInView:self.view]; NSLog(@"touch.previousLocationInView = {%2.3f, %2.3f}", previousPoint.x, previousPoint.y); // touch.phase,触摸事件的阶段 NSLog(@"touch.phase = %d", touch.phase); // touch.tapCount,触摸事件的轻碰次数,可以判断双击事件 NSLog(@"touch.tapCount = %d", touch.tapCount); } UITouch *touch = [touches anyObject]; // 单击或双击时,改变背景颜色 if (1 == touch.tapCount) { self.view.backgroundColor = [UIColor blueColor]; } // 双击时,设置背景颜色为红色 if (2 == touch.tapCount) { self.view.backgroundColor = [UIColor redColor]; } // 获取触摸区域的子视图 // 获取当前视图控制器的坐标 CGPoint currentPoint = [touch locationInView:self.view]; for (UIView *oneView in self.view.subviews) { if ([oneView isKindOfClass:[UIImageView class]]) { // 是否选中图标(当前坐标是否包含所选图标) if (CGRectContainsPoint(oneView.frame, currentPoint)) { // 获取当前被选择图标 UIView *selectedView = (UIView *)oneView; // 被选中视图放在最上面 [self.view bringSubviewToFront: selectedView]; } } } }
// 触摸移动 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"touchesMoved - touch count = %d", [touches count]); // 获得触摸点的集合,可以判断多点触摸事件 for(UITouch *touch in event.allTouches) { // 获取当前触摸坐标 CGPoint currentPoint = [touch locationInView:self.view]; NSLog(@"touch.locationInView = {%2.3f, %2.3f}", currentPoint.x, currentPoint.y); // 获取当前触摸前一个坐标 CGPoint previousPoint = [touch previousLocationInView:self.view]; NSLog(@"touch.previousLocationInView = {%2.3f, %2.3f}", previousPoint.x, previousPoint.y); // touch.phase,触摸事件的阶段 NSLog(@"touch.phase = %d", touch.phase); // touch.tapCount,触摸事件的轻碰次数,可以判断双击事件 NSLog(@"touch.tapCount = %d", touch.tapCount); } UITouch *touch = [touches anyObject]; // 获取当前视图控制器的坐标 CGPoint currentPoint = [touch locationInView:self.view]; // 获取上一次位置 CGPoint previousPoint = [touch previousLocationInView:self.view]; // 坐标偏移量 float moveX = currentPoint.x - previousPoint.x; float moveY = currentPoint.y - previousPoint.y; }
// 触摸结束 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"touchesEnded - touch count = %d", [touches count]); // 获得触摸点的集合,可以判断多点触摸事件 for (UITouch *touch in event.allTouches) { // 获取当前触摸坐标 CGPoint currentPoint = [touch locationInView:self.view]; NSLog(@"touch.locationInView = {%2.3f, %2.3f}", currentPoint.x, currentPoint.y); // 获取当前触摸前一个坐标 CGPoint previousPoint = [touch previousLocationInView:self.view]; NSLog(@"touch.previousLocationInView = {%2.3f, %2.3f}", previousPoint.x, previousPoint.y); // touch.phase,触摸事件的阶段 NSLog(@"touch.phase = %d", touch.phase); // touch.tapCount,触摸事件的轻碰次数,可以判断双击事件 NSLog(@"touch.tapCount = %d", touch.tapCount); } UITouch *touch = [touches anyObject]; if (2 == touch.tapCount) { // 双击 } }
// 触摸意外结束(如来电话) - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"touchesCancelled - touch count = %d", [touches count]); // 获得触摸点的集合,可以判断多点触摸事件 for (UITouch *touch in event.allTouches) { // 获取当前触摸坐标 CGPoint currentPoint = [touch locationInView:self.view]; NSLog(@"touch.locationInView = {%2.3f, %2.3f}", currentPoint.x, currentPoint.y); // 获取当前触摸前一个坐标 CGPoint previousPoint = [touch previousLocationInView:self.view]; NSLog(@"touch.previousLocationInView = {%2.3f, %2.3f}", previousPoint.x, previousPoint.y); // touch.phase,触摸事件的阶段 NSLog(@"touch.phase = %d", touch.phase); // touch.tapCount,触摸事件的轻碰次数,可以判断双击事件 NSLog(@"touch.tapCount = %d", touch.tapCount); } }
相关文章推荐
- UI自动化
- 《UNIX环境高级编程》第二版:Apue.h文件引入
- L2-005. 集合相似度 unique的应用 去重 vector
- iOS触摸手势知识介绍(UITouch & UIGestureRecognizer)
- IOS开发中UIFont字体设置
- leetcode.187. Repeated DNA Sequences
- iOS 如何让UITableView显示时自动滚动到底部(不闪动)
- UGUI研究院之SpritePacker打包参数(四)
- UGUI研究院之全面理解图集与使用(三)
- UITextField 的Placeholder的字体的颜色大小
- 21: Arithmetic Sequence--HZAU(dp)
- UITableView Section标题不滑动问题
- Android学习笔记——AlertDialogbuilder和AlertDialog
- 【Arduino官方教程第一辑】示例程序 4-6 串口控制LED
- HBuilder的常用快捷键
- STL 整理(map、set、vector、list、stack、queue、deque、priority_queue)zhuanzai
- 用 QueryPerformanceFrequency 和 QueryPerformanceCounter 进行高精度计时
- PAT (Advanced Level) 1017. Queueing at Bank (25)
- poj 2524 Ubiquitous Religions(宗教信仰)
- 使用RTEMS Source Builder 编译RTEMS-4.11的开发工具链