UI-事件处理
2015-10-17 19:48
531 查看
补充:按钮字符串替换
UIViewController是MVC设计模式的核心。MVC是一个框架级的设计模式。
M是Model主要⽤用于建⽴数据模型(即数据的结构)
V是View,我们能看到的所有控件都view,view主要的功能是展示数据。
C是控制器,主要是控制M和V的通信。
View:在自定义视图中创建控件,初始化控件,设置一些基本的属性。
Controller:在控制器中写一些视图的触发事件
userInteractionEnabled属性:是控制是否阻断响应的属性(BOOL)
UITouch的两个方法:
- (CGPoint)locationInView:(UIView *)view;----返回当前触摸点的坐标
- (CGPoint)previousLocationInView:(UIView *)view;----返回当前的触摸点之前一个点的坐标(看API文档 有解释)
2. 响应者链:
响应事件查询顺序:UIApplication-->window-->viewController-->view-->所有的子视图
事件处理顺序:被点击的子视图-->view-->viewController-->window-->UIApplication-->事件作废
// 按钮在 显示和隐藏两个字符串之间替换 点击一下 替换一下 - (void)buttonAction:(UIButton *)sender { #pragma 方法一: // if (sender.selected) { // selected 的默认值是NO 第一次点击无效果 // [sender setTitle:@"隐藏" forState:UIControlStateNormal]; //// sender.selected = NO; // }else { // [sender setTitle:@"显示" forState:UIControlStateNormal]; //// sender.selected = YES; // } // sender.selected = !sender.selected; // BOOL值可以取非 #pragma 方法二: // sender.selected = !sender.selected; // BOOL值可以取非 // sender.selected ? [sender setTitle:@"隐藏" forState:UIControlStateNormal] : [sender setTitle:@"显示" forState: UIControlStateNormal]; #pragma 方法三: sender.selected = !sender.selected; // BOOL值可以取非 [sender setTitle:sender.selected?@"隐藏":@"显示" forState:UIControlStateNormal]; }
UIViewController是MVC设计模式的核心。MVC是一个框架级的设计模式。
M是Model主要⽤用于建⽴数据模型(即数据的结构)
V是View,我们能看到的所有控件都view,view主要的功能是展示数据。
C是控制器,主要是控制M和V的通信。
View:在自定义视图中创建控件,初始化控件,设置一些基本的属性。
Controller:在控制器中写一些视图的触发事件
1. 触摸事件:
#pragma Touch--触摸的时候开始发送消息 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { // 集合里存储对象---NSSet NSLog(@"触摸------开始"); } -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"over"); } -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint point1 = [touch locationInView:self.rootV.touchView]; CGPoint point2 = [touch previousLocationInView:self.rootV.touchView]; CGPoint center = self.rootV.touchView.center; CGFloat x = point1.x - point2.x; CGFloat y = point1.y - point2.y; // 判断范围......最大的横纵坐标值 CGFloat MaxX = [UIScreen mainScreen].bounds.size.width; CGFloat MaxY = [UIScreen mainScreen].bounds.size.height; CGFloat radius = self.rootV.touchView.layer.cornerRadius;// 半径 // 视图的四个边界值 CGFloat leftX = center.x + x - radius; CGFloat rightX = center.x + x + radius; CGFloat topY = center.y + y - radius; CGFloat downY = center.y + y + radius; if (rightX <= MaxX && leftX >= 0 ) { if (topY >= 0 && downY <= MaxY) { self.rootV.touchView.center = CGPointMake(center.x + x, center.y + y); } } } -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { }
userInteractionEnabled属性:是控制是否阻断响应的属性(BOOL)
UITouch的两个方法:
- (CGPoint)locationInView:(UIView *)view;----返回当前触摸点的坐标
- (CGPoint)previousLocationInView:(UIView *)view;----返回当前的触摸点之前一个点的坐标(看API文档 有解释)
2. 响应者链:
响应事件查询顺序:UIApplication-->window-->viewController-->view-->所有的子视图事件处理顺序:被点击的子视图-->view-->viewController-->window-->UIApplication-->事件作废
3.控制晃动(运动事件)
- (void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{ //检测到摇动 NSLog(@"这个是检测有没有摇晃的 反正你也看不到 →_→ →_→"); } - (void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{ NSLog(@"摇晃结束~ ~ ~"); if (event.subtype == UIEventSubtypeMotionShake) { UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"TEST" message:@"机智如我" delegate:nil cancelButtonTitle:@"我本来就机智" otherButtonTitles:@"我确定我很机智",nil]; [alertView show]; } } - (void) motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event{ //摇动取消 }
4. 屏幕旋转:
//获得当前方向 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { return toInterfaceOrientation == UIDeviceOrientationPortrait; } //开启旋转 - (BOOL)shouldAutorotate { return YES; }
相关文章推荐
- storm 开发系列二 Clojue版本
- SPOJ GSS4 Can you answer these queries IV
- 提高Interface Builder高效工作的8个技巧
- CSU1661: Query Mutiple
- addTarget各个参数的含义:
- UI---UIImageView
- UI中横屏竖屏切换的一些方法(转)
- UI系统类继承树
- IOS 开发笔记-基础 UI(8)控件连线的 strong 和 weak 设定
- SPOJ GSS3 Can you answer these queries III
- SPOJ GSS2 Can you answer these queries II
- Guid
- Request和Response详解
- request对象和response对象
- EasyUI - SearchBox 搜索框
- UI----Touch
- 定制Segue - OverlaySegue
- EasyUI - pagination 分页组件
- UI--Gesture
- NGUI实现输入框和进度条