iOS开发笔记之多点触控(四) 可靠的多点触控,为每个View分配唯一触摸对象
来源:互联网 发布:finale 2016 mac 编辑:程序博客网 时间:2024/06/05 20:28
每个View需要分配有效的触摸以避免第三方触摸的干扰。实现方法如下
.h文件,接口定义两个变量
#import <UIKit/UIKit.h>@interface BBSViewController : UIViewController{ UITouch *touch1; UITouch *touch2;}@end
.m文件,在touchesBegan里为view分配一个特定触摸对象(仅当它还未分配时)。
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event //首次在屏幕上检测到触摸时调用{ NSLog(@"touchesBegan"); for (UITouch *touch in touches) {// NSLog(@" - %p",touch); //获取根视图内触摸点的point CGPoint touchPoint = [touch locationInView:self.view]; //约束两个view的活动范围 if (touch1 == nil && touchPoint.y < self.view.frame.size.height/2) { touch1 = touch; _view1.center = CGPointMake(touchPoint.x, _view1.center.y); }else if (touch2 == nil && touchPoint.y > self.view.frame.size.height/2) { touch2 = touch; _view2.center = CGPointMake(touchPoint.x, _view2.center.y); } }}
在touchesMoved方法里,忽略所有未绑定View的触摸
//以上对触摸进行了初始化,并未处理沿着屏幕移动的触摸。所以,只需要在touchesMoved方法里调用touchesBegan的处理方法来改写移动球拍的逻辑即可。-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event //如果触摸移动到了新的位置则会调用此方法{ NSLog(@"touchesMoved"); for (UITouch *touch in touches) {// NSLog(@" - %p",touch);// [self touchesBegan:touches withEvent:event]; CGPoint touchPoint = [touch locationInView:self.view]; if (touch == touch1) { _view1.center = CGPointMake(touchPoint.x, _view1.center.y); } else if (touch == touch2) { _view2.center = CGPointMake(touchPoint.x, _view2.center.y); } }}
及时释放已绑定了View的触摸,避免手指一离开屏幕就永久失去对View的控制。
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event//当触摸离开屏幕调用此方法{ NSLog(@"touchesEnded"); for (UITouch *touchin touches) { NSLog(@" - %p",touch); if (touch == touch1)touch1 = nil; else if (touch ==touch2) touch2 = nil; }}-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event//如系统决定取消此次触摸,那可能就不调用touchesEnded方法了,在这种情况下会调用touchesCancelled方法{ NSLog(@"touchesCancelled");// for (UITouch *touch in touches)// {// NSLog(@" - %p",touch);// } [self touchesEnded:touches withEvent:event];}
这样多余的触摸将不会影响现有的View的位置。
转载请注明原著:http://blog.csdn.net/marvindev
下一篇介绍iOS开发笔记之摇动手势
0 0
- iOS开发笔记之多点触控(四) 可靠的多点触控,为每个View分配唯一触摸对象
- iOS开发笔记之多点触控(二) 开启多点触控的方法
- iOS开发笔记之多点触控(三) 调用UITouch对象方法——locationInView,多点移动多个对象
- iOS开发笔记之多点触控(一)处理触摸的4个方法
- ios开发之多点触摸
- ios开发之多点触摸
- ios开发之多点触摸
- ios实例开发精品文章推荐(8.12)11个处理触摸事件和多点触摸的JS库
- 【Android】多点触控(判断触摸点是否在view上)
- 手机电阻触摸屏”,“电容触摸屏”,“多点触摸技术”的工作原理及Android多点触控开发
- cocos2dx iOS多点触摸
- ios多点触摸范例
- Android游戏开发之单点触摸与多点触摸的响应方式(二十三)
- Android游戏开发之单点触摸与多点触摸的响应方式(二十三)
- IOS之多点触摸
- iOS开发:处理多点触摸与手势检测
- HTML5触摸事件(多点、单点触控)
- 基于Visual C++2010与windows SDK fo windows7开发Windows 7的多点触摸特性应用程序(1)--多点触控
- JDK5新特性
- Linux之V4L2基础编程
- 多线程
- 5
- 面向对象
- iOS开发笔记之多点触控(四) 可靠的多点触控,为每个View分配唯一触摸对象
- 二叉查找树的简单实现
- Java知识点总结
- libv4l 库
- Node.Js路由
- OpenCV在ARM上的移植
- 创建SDL2 ios xcode工程
- (终极篇)解决android手机adb offline问题
- 应用:带文档注释的ArrayTool类