iOS开发笔记之多点触控(三) 调用UITouch对象方法——locationInView,多点移动多个对象
2014-01-15 23:48
501 查看
屏幕上新建两个view,实现两个手指在屏幕上移动两个view,通过规则约束两个view的活动范围。
关键在于调用UITouch对象的方法——lacationInView。此方法返回View的相对于根视图的触摸位置。返回值是一个CGPoint类型,是一个包含X坐标和Y坐标的结构体。我让两个view在屏幕上下两个半区水平移动。利用CGPointMake来快速初始化新的CGPoint结构体。
下图为两个view可以分别沿着水平方向移动
转载请注明原著:http://blog.csdn.net/marvindev
下一篇介绍iOS开发笔记之多点触控(四) 可靠的多点触控,为每个View分配唯一触摸对象
关键在于调用UITouch对象的方法——lacationInView。此方法返回View的相对于根视图的触摸位置。返回值是一个CGPoint类型,是一个包含X坐标和Y坐标的结构体。我让两个view在屏幕上下两个半区水平移动。利用CGPointMake来快速初始化新的CGPoint结构体。
- (void)viewDidLoad { [super viewDidLoad]; self.view.multipleTouchEnabled = TRUE; // Do any additional setup after loading the view, typically from a nib. _view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 40, 40, 40)]; _view1.backgroundColor = [UIColor blueColor]; [self.view addSubview:_view1]; _view2 = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 60, 40, 40)]; _view2.backgroundColor = [UIColor yellowColor]; [self.view addSubview:_view2]; } -(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 (touchPoint.y < self.view.frame.size.height/2) { _view1.center = CGPointMake(touchPoint.x, _view1.center.y); }else { _view2.center = CGPointMake(touchPoint.x, _view2.center.y); } } } //以上对触摸进行了初始化,并未处理沿着屏幕移动的触摸。所以,只需要在touchesMoved方法里调用touchesBegan的处理方法来改写移动球拍的逻辑即可。 -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event //如果触摸移动到了新的位置则会调用此方法 { NSLog(@"touchesMoved"); for (UITouch *touch in touches) { // NSLog(@" - %p",touch); [self touchesBegan:touches withEvent:event]; } } -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event//当触摸离开屏幕调用此方法 { NSLog(@"touchesEnded"); for (UITouch *touch in touches) { NSLog(@" - %p",touch); } } -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event//如系统决定取消此次触摸,那可能就不调用touchesEnded方法了,在这种情况下会调用touchesCancelled方法 { NSLog(@"touchesCancelled"); for (UITouch *touch in touches) { NSLog(@" - %p",touch); } }
下图为两个view可以分别沿着水平方向移动
转载请注明原著:http://blog.csdn.net/marvindev
下一篇介绍iOS开发笔记之多点触控(四) 可靠的多点触控,为每个View分配唯一触摸对象
相关文章推荐
- iOS开发笔记之多点触控(四) 可靠的多点触控,为每个View分配唯一触摸对象
- iOS开发笔记——TableView Separatorinset 分割线从边框顶端开始
- iOS开发笔记--解决UITableView中Cell重用机制导致内容出错的方法总结
- iOS开发笔记--iOS应用架构谈 view层的组织和调用方案
- 移动项目开发笔记(Asp.net控件之间的方法的互相调用)
- IOS开发笔记之二十五——OC中父类与子类之间相互调用方法的总结
- iOS开发:Xcode 7之后viewForHeaderInSection、viewForFooterInSection设置内容不产生效果的解决方法
- IOS UISplitViewController之 解决 密码框添加之后 调用子视图的转向方法之时 对象为空
- iOS开发笔记--iOS应用架构谈 view层的组织和调用方案
- ios开发笔记-循环调用/执行一个方法
- IOS开发笔记之二十一——一种跨类调用方法的案例
- iOS开发tableview为什么没有调用heightForRowAtIndexPath方法
- 【iOS】tableView:viewForHeaderInSection: 方法未调用
- Android开发 activity里面的WebView加载js调用windows.location.reload()会弹出浏览器的解决方法
- iOS开发笔记之七十——如何访问对象的私有方法和变量
- IOS开发学习笔记027-UITableView 使用模型对象
- ios开发笔记之十二 --viewDidload的方法的基本介绍用代码设置按钮的属性
- iOS开发笔记--解决UITableView中Cell重用机制导致内容出错的方法总结
- iOS开发笔记之多点触控(二) 开启多点触控的方法
- iOS开发笔记--解决UITableView中Cell重用机制导致内容出错的方法总结