事件
事件,是由硬件捕捉的⼀个表⽰⽤户操作设备的对象。分三类:触摸事件、晃动事件、远程控制事件。
触摸事件:⽤户通过触摸设备屏幕操作对象、输⼊数据。⽀持多点
触摸,包含1个到多个触摸点。
触摸
需要定义UIView⼦类TouchView,实现触摸相关的⽅法
开始触摸
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"开始摸"); // 在这里写开始触摸需要实现的代码 }
触摸中
例:实现view跟随⼿指偏移进⾏移动
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { // UITouch保存手指的信息(触摸的点) UITouch *touch = [touches anyObject]; // 取出当前触摸的点 // 返回一个当前触摸的点,相对于传进去的参数view CGPoint p1 = [touch locationInView:self]; NSLog(@"%@",NSStringFromCGPoint(p1)); // 返回当前点的上一个点,相对于传进去的参数view CGPoint p2 = [touch previousLocationInView:self]; NSLog(@"%@",NSStringFromCGPoint(p2)); // 计算x轴的偏移量 CGFloat x = p1.x - p2.x; // 计算y轴的偏移量 CGFloat y = p1.y - p2.y; // 方法1:改变视图原来的点 // self.frame = CGRectMake(self.frame.origin.x + x, self.frame.origin.y + y, self.frame.size.width, self.frame.size.height); // 随机变颜色 // [0,255] self.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:arc4random()%2]; // 方法2:取出点,改变中心点 self.center = CGPointMake(self.center.x + x, self.center.y + y); }
触摸结束
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { // 写触摸结束时需要实现的代码 }
触摸中断
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"触摸中断,例如触摸中来电话可以出发中断这个方法,例如小退出"); }
晃动
摇一摇功能
例:创建两个ViewController,晃动后跳转view,再晃动,跳回来。
在RootViewController的viewDidLoad方法中新建myView,添加到self.view中
UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(120, 400, 100, 20)]; myView.backgroundColor = [UIColor greenColor]; [self.view addSubview:myView];
在对应的晃动事件中实现功能
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event { NSLog(@"结束晃动"); // 新建跳转的界面 SecondViewController *secondVC = [[SecondViewController alloc]init]; // 跳转 [self presentViewController:secondVC animated:YES completion:nil]; }
还有开始晃动和中断晃动的方法,一般使用的是结束晃动的方法
-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event { NSLog(@"开始晃动"); } -(void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event { NSLog(@"中断晃动"); }
在SecondViewController中实现,摇一摇,返回上一个界面的功能。
// 这里还是使用晃动结束的方法 - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event { [self dismissViewControllerAnimated:YES completion:nil]; }
响应者链
响应者链 分为两个过程
查询过程:
当你点击屏幕,先定位到:应用程序 -> window -> ViewController ->self.view ->view上的子视图一一查找,直到定位到被点击的子视图。查询过程结束。响应过程:
首先,看本视图,能不能处理事件(实现了touchBegin等方法,就叫做可以处理事件) -> 父视图 -> 一层一层往下看能不能处理,直到window。如果都不能处理,该次点击事件被遗弃(无效点击)
例:创建一个button,测试响应者链
UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
button.frame = CGRectMake(120, 300, 100, 50);
[button setTitle:@"点我" forState:(UIControlStateNormal)];
button.backgroundColor = [UIColor cyanColor];
[button addTarget:self action:@selector(buttobClick:) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:button];
添加button的点击事件:
- (void)buttobClick:(UIButton *)button
{
NSLog(@"你点我了");
}
关闭button的交互,阻断查询过程。默认交互是打开的。
button.userInteractionEnabled = NO;
注意:UILabel UIImageView的交互默认是关闭的
例:将UIButton作为UILabel的⼦视图
// 创建一个label
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(120, 300, 100, 50)];
label.backgroundColor = [UIColor cyanColor];
[self.view addSubview:label];
[label release];
// 创建一个button
UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
button.frame = CGRectMake(30, 10, 50, 30);
[button setTitle:@"点我" forState:(UIControlStateNormal)];
button.backgroundColor = [UIColor grayColor];
[button addTarget:self action:@selector(buttobClick:) forControlEvents:(UIControlEventTouchUpInside)];
[label addSubview:button];
button的点击事件与上面一样
结果:运行时按钮的点击无效,因为label上的按钮交互是默认关闭的,需要添加:
label.userInteractionEnabled = YES;
把交互打开才能使用。
另,需要设置label的text属性
label.text = @"aa";
若不写text属性,可以实现按钮的点击事件,但是不显示按钮。