UIEvent
2015-08-04 09:27
477 查看
UIEvent : 事件
分三类: 触摸 晃动 远程遥控
UIResponder 响应者类
iOS中,所有能响应事件(触摸, 晃动, 远程事件)的对象都是响应者
系统定义了一个抽象的父类UIResponder来表示响应者, 其子类都是响应者
2.创建一个继承于UIView 可以随着触摸移动的myView, 并且点击myView, 随机更换myView颜色
3.有关键盘上return, 用如下方法
分三类: 触摸 晃动 远程遥控
UITouch
(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ NSLog(@"触摸开始"); // 点击空白处, 回收键盘 [self.myTextField resignFirstResponder]; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ NSLog(@"触摸移动"); } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ NSLog(@"触摸结束"); } - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{ NSLog(@"触摸被取消"); }
晃动
-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{ // 摇一摇 随机更换背景颜色 self.view.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1]; NSLog(@"晃动开始"); } -(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{ NSLog(@"晃动结束"); } -(void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event{ NSLog(@"晃动取消"); }
UIResponder 响应者类
iOS中,所有能响应事件(触摸, 晃动, 远程事件)的对象都是响应者
系统定义了一个抽象的父类UIResponder来表示响应者, 其子类都是响应者
例:
1.点击一个继承于UIButton类的button按钮,实现让文本框弹出键盘的操作(void)click:(UIButton *)button{ [self.myTextField becomeFirstResponder]; //第一响应者 }
2.创建一个继承于UIView 可以随着触摸移动的myView, 并且点击myView, 随机更换myView颜色
// 1.创建一个UIView类文件 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ // 随机生成背景颜色 self.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1]; // 集合里的元素个数 NSLog(@"%ld", touches.count); // 集合里有一个触摸类的对象 UITouch *touch = [touches anyObject]; // 通过触摸对象获取相应的视图的当前位置 self.startPoint = [touch locationInView:self]; NSLog(@"%g", self.startPoint.x); } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ // 通过移动,找到变换,然后让MyView也进行相应的调整, 从而实现视图随手移动的效果 // 获取触摸的对象 UITouch *touch = [touches anyObject]; // 获取移动之后的坐标 CGPoint movePonit = [touch locationInView:self]; // 坐标的变化 CGFloat dx = movePonit.x - self.startPoint.x; CGFloat dy = movePonit.y - self.startPoint.y; // 设置视图的移动变化 self.center = CGPointMake(self.center.x + dx, self.center.y + dy); } // 2.在视图控制器中创建myView对象, 实现该功能 MyView *myView = [[MyView alloc]initWithFrame:CGRectMake(100, 150, 150, 90)]; myView.backgroundColor = [UIColor redColor]; [self.view addSubview:myView]; [myView release];
3.有关键盘上return, 用如下方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField{ // 1.点击return, 回收键盘 [self.textField resignFirstResponder ]; // 2.点击return, 换行, 最后一个 回收键盘 UITextField *myTextField1 = (UITextField *)[self.view viewWithTag:1001]; UITextField *myTextField2 = (UITextField *)[self.view viewWithTag:1002]; UITextField *myTextField3 = (UITextField *)[self.view viewWithTag:1003]; UITextField *myTextField4 = (UITextField *)[self.view viewWithTag:1004]; UITextField *myTextField5 = (UITextField *)[self.view viewWithTag:1005]; if ([myTextField1 isFirstResponder]) { [myTextField2 becomeFirstResponder]; }else if ([myTextField2 isFirstResponder]){ [myTextField3 becomeFirstResponder]; }else if ([myTextField3 isFirstResponder]){ [myTextField4 becomeFirstResponder]; }else if ([myTextField4 isFirstResponder]){ [myTextField5 becomeFirstResponder ]; }else{ [myTextField5 resignFirstResponder]; } return YES; }
相关文章推荐
- POJ 3907 Build Your Home
- JS 模块化使用 [Require JS]
- [leetcode]Unique Paths(C)
- Error:svn: E175002: connection refused by the server svn: E175002: OPTIONS request failed on '
- Unique Binary Search Trees II
- 25.UITouch
- HDOJ Fruit 2152【母函数】
- Havok_2014-1-0_Pc_Xs_User_Guide(2.5.3-保存接触点)
- Havok_2014-1-0_Pc_Xs_User_Guide(1.1.2-基础系统)
- IOS开发基础之UIImage
- UE4信息交互-蓝图类与关卡对象的信息交互
- UISlider
- UISegmentControl
- SPOJ QTREE4 Query on a tree IV (边分治 + 堆)
- POJ_1986_Distance Queries(LCA+tarjan)
- iOS开发——UI进阶篇(十四)modal
- SPOJ PTO7J Query on a tree III(dfs序+主席树)
- SPOJ QTREE2 Query on a tree II (倍增LCA)
- 想要App做得美,这些UI/UX设计趋势你要知道啊
- SPOJ QTREE Query on a tree (树链剖分)