事件处理
来源:互联网 发布:淘宝试用装是真的吗 编辑:程序博客网 时间:2024/06/11 04:34
Touch
下面4个方法是UIResponder的方法 继承于UIResponder的类都可以重写此方法
// 开始触摸- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{}// 触摸时移动- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{}// 触摸结束- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{}// 触摸中断- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{ // 如来电话 小退出}
在开始触摸时 可以获取当前触摸点 和 前一个触摸点 相对于被触摸视图的坐标:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ NSLog(@"开始摸...");// UITouch 保存手指信息(触摸的点) UITouch *touch = [touches anyObject];// NSLog(@"%@",touch);// 取出当前触摸的点// 返回一个当前触摸的点 相对于传进去的参数View 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(@"抚摸中...");// 取出当前点// 取出当前点上一个点// 计算X轴的偏移量// 计算Y轴的偏移量// 改变视图原来的中心点 UITouch *touch = [touches anyObject]; CGPoint p1 = [touch locationInView:self]; CGPoint p2 = [touch previousLocationInView:self]; CGFloat x = p1.x - p2.x; 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];}
响应者链
响应者链 分为两个过程
1.查询过程
当你点击屏幕 先定位到应用程序->window->控制器->self.view->view的子视图一一进行查找 直到定位到被点击的子视图 查询过程结束
2.响应过程
首先 看本视图能不能处理事件(实现了touchBegan等方法 就叫做可以处理事件)->父视图->一层一层往下看能不能处理 直到window 如果都不能处理 该次点击事件 被遗弃(无效点击)
注意: UILabel UIImageView的交互 默认时关闭的
创建一个button 测试响应者链:
当button的交互属性被关闭时 点击按钮无法触发方法
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(200, 200, 100, 100); button.backgroundColor = [UIColor brownColor]; [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside]; // 关闭button交互 (阻断查询过程) button.userInteractionEnabled = NO; [self.view addSubview:button];
Motion
下面4个方法是UIResponder的方法 继承于UIResponder的类都可以重写此方法
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{ NSLog(@"开始晃动");}// 在结束晃动方法中 可以实现界面跳转- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{ NSLog(@"结束晃动");// 跳转界面 SecondViewController *secondVC = [[SecondViewController alloc] init]; [self presentViewController:secondVC animated:YES completion:nil]; [secondVC release];}- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event{ NSLog(@"中断晃动");}
如果要想通过晃动跳转回原来的视图 ,可以在当前视图的控制器中实现方法:
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{ [self dismissViewControllerAnimated:YES completion:nil];}
0 0
- 事件处理
- 事件处理
- 事件处理
- 处理事件
- 事件处理
- 事件处理
- 事件处理
- 事件处理
- 处理事件
- 事件处理
- 事件处理
- 事件处理
- 事件处理
- 事件处理
- 事件处理
- 事件处理
- 事件处理
- 事件处理
- JS中offsetTop、clientTop、scrollTop、offsetTop各属性介绍
- 线性表14 – 数据结构和算法19
- Pycharm 快捷键
- 项目4--利用遍历思想求解图问题
- 多重视图和路由
- 事件处理
- 线性表15|魔术师发牌问题和拉丁方阵 – 数据结构和算法20
- Linux的cat命令使用
- JDBC Statement
- Java EE 设计模式解析与应用——互动出版网
- cors与Nginx
- 第十一周项目--【二叉树的层次遍历算法】
- excel数据批量导出为sql脚本
- oracle autotrace 使用说明