1、事件(UIEvent),是由硬件设备捕捉到用户对设备的操作,把这个操作抽象成一个事件对象
ios中三大事件:触
Touches
摸晃动事件Motion,远程控制事件RemoteControl;其中应有最广泛的是触摸事件
UIView是支持触摸的,由于UIView 内部没有实现跟触摸相关的方法,所以我们再点击UIView创建其子类进行方法实现
2、一般准备做题思路:先在 AppDelegate.m中建立一个 TouchVC对象
TouchViewController *touchVC = [[TouchViewController alloc]init];
//将touchVC指定self.window的为根视图控制器
self.window.rootViewController = touchVC;
//将touchVC指定self.window的为根视图控制器
self.window.rootViewController = touchVC;
[touchVC release];
然后在 TouchViewController.m中设置颜色
self.view.backgroundColor = [UIColor yellowColor];
————————————————————————————
以上方法为通常准备方法:
第一类型:点击移动视图
MoveView *moveView = [[MoveView alloc]initWithFrame:CGRectMake(110, 234, 100, 100)];
moveView.backgroundColor = [UIColor blackColor];
[self.view addSubview:moveView];
[self.view addSubview:moveView];
[moveView release];
第二类型:捏合视图对象
PinchView *pinchView = [[PinchView alloc]initWithFrame:CGRectMake(60, 184, 200, 200)];
pinchView.backgroundColor = [UIColor orangeColor];
[self.view addSubview:pinchView];
pinchView.backgroundColor = [UIColor orangeColor];
[self.view addSubview:pinchView];
[pinchView release];
————————————————————————————
首先总结点击移动视图:
TouchView.m
//如果想让一个视图对象对触摸事件作出反应,需要在这个类中的.m文件中实现跟触摸相关一些方法
//当手指在视图范围内移动的时候触发
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
// self.backgroundColor = [UIColor randomColor];
self.center = CGPointMake(arc4random_uniform(220 - 100 + 1) + 100, arc4random_uniform(468 - 100 +1) +100);
//取出手指对像
UITouch *f = [touches anyObject];
//当前手指的 位置
CGPoint location = [f locationInView:self];
//之前手指位置
CGPoint previousLocation = [f previousLocationInView:self];
CGPoint center = self.center;
CGFloat dx = location.x - previousLocation.x;
CGFloat dy = location.y - previousLocation.y;
self.center = CGPointMake(center.x + dx, center.y + dy);
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
// self.backgroundColor = [UIColor randomColor];
self.center = CGPointMake(arc4random_uniform(220 - 100 + 1) + 100, arc4random_uniform(468 - 100 +1) +100);
//取出手指对像
UITouch *f = [touches anyObject];
//当前手指的 位置
CGPoint location = [f locationInView:self];
//之前手指位置
CGPoint previousLocation = [f previousLocationInView:self];
CGPoint center = self.center;
CGFloat dx = location.x - previousLocation.x;
CGFloat dy = location.y - previousLocation.y;
self.center = CGPointMake(center.x + dx, center.y + dy);
NSLog(@"%s",__FUNCTION__);
TouchView.m
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
//1.获取手指对象
UITouch *finger = [touches anyObject];
{
//1.获取手指对象
UITouch *finger = [touches anyObject];
//2.获取手指当前在自身视图的位置
CGPoint currentLocation = [finger locationInView:self];
//3.获取手指之前在视图上的位置
CGPoint previousLocation = [finger previousLocationInView:self];
//4.计算手指在x轴和y轴上的增量
//手指在x轴上的增量
CGFloat dx = currentLocation.x - previousLocation.x;
//手指在y轴上的增量
//手指在x轴上的增量
CGFloat dx = currentLocation.x - previousLocation.x;
//手指在y轴上的增量
CGFloat dy = currentLocation.y - previousLocation.y;
//获取中心点,之前center的位置
CGPoint center = self.center;
self.center = CGPointMake(center.x + dx, center.y + dy);
}
CGPoint center = self.center;
self.center = CGPointMake(center.x + dx, center.y + dy);
}
@end
——————————————————————————————————————————
第二种类型捏合视图对象:
首先要把默认的单触点关掉
PinchView.m 继承自UIView
-(instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
//ios支持多点触摸,只是默认是单点触摸
self.multipleTouchEnabled = YES;
}
return self;
if (self = [super initWithFrame:frame]) {
//ios支持多点触摸,只是默认是单点触摸
self.multipleTouchEnabled = YES;
}
return self;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
//如果集合中touch集合中手指对象个数为1,就直接返回touchsMoved方法
if (1 == touches.count) {
return;//结束方法
}else{
//得到集合中所有手指对象,并使用数组存储(数组有序)
//如果集合中touch集合中手指对象个数为1,就直接返回touchsMoved方法
if (1 == touches.count) {
return;//结束方法
}else{
//得到集合中所有手指对象,并使用数组存储(数组有序)
NSArray *allTouchs = [touches allObjects];
//取出两个触点
UITouch *touch1 = [allTouchs firstObject];
UITouch *touch2 = [allTouchs lastObject];
//求出两个手指对象当前的位置
CGPoint firstTouch1 = [touch1 locationInView:self];
CGPoint secondTouch2 = [touch2 locationInView: self];
//求出当前两个手指的间距
CGFloat currentDistance = [self distanceFromeFirstPoint:firstTouch1 secondPoint:secondTouch2];
//求出之前两个手指的位置
CGPoint previousFirstPoint = [touch1 previousLocationInView:self];
CGPoint previousSecondPoint = [touch2 previousLocationInView:self];
//求出之前两个点的距离
UITouch *touch2 = [allTouchs lastObject];
//求出两个手指对象当前的位置
CGPoint firstTouch1 = [touch1 locationInView:self];
CGPoint secondTouch2 = [touch2 locationInView: self];
//求出当前两个手指的间距
CGFloat currentDistance = [self distanceFromeFirstPoint:firstTouch1 secondPoint:secondTouch2];
//求出之前两个手指的位置
CGPoint previousFirstPoint = [touch1 previousLocationInView:self];
CGPoint previousSecondPoint = [touch2 previousLocationInView:self];
//求出之前两个点的距离
CGFloat previousDistance = [self distanceFromeFirstPoint:previousFirstPoint secondPoint:previousSecondPoint];
//获取捏合前后两个手指间距的比例
CGFloat rate = currentDistance / previousDistance;
//缩放视图,如果视图的大小发生变化时,而中心点的位置不发生变化,修改bounds就可以了
self.bounds = CGRectMake(0, 0, self.bounds.size.width * rate, self.bounds.size.height * rate);
}
}
//封装一个计算距离的方法(sqrt求开方)
- (CGFloat)distanceFromeFirstPoint : (CGPoint)firstPoint secondPoint : (CGPoint)secondPoint{
CGFloat dx = firstPoint.x - secondPoint.x;
CGFloat dy = firstPoint.y - secondPoint.y;
//当前两点距离
return sqrt(dx * dx + dy * dy);
CGFloat rate = currentDistance / previousDistance;
//缩放视图,如果视图的大小发生变化时,而中心点的位置不发生变化,修改bounds就可以了
self.bounds = CGRectMake(0, 0, self.bounds.size.width * rate, self.bounds.size.height * rate);
}
}
//封装一个计算距离的方法(sqrt求开方)
- (CGFloat)distanceFromeFirstPoint : (CGPoint)firstPoint secondPoint : (CGPoint)secondPoint{
CGFloat dx = firstPoint.x - secondPoint.x;
CGFloat dy = firstPoint.y - secondPoint.y;
//当前两点距离
return sqrt(dx * dx + dy * dy);
}
================================================================================
响应者链:
AppDelegate.m
//创建 Responder对象
ResponderViewController *responderVC = [[ResponderViewController alloc]init];
//将responderVC指定为根控制器
self . window . rootViewController = responderVC;
self . window . rootViewController = responderVC;
[responderVC release];
ResponderViewController.m
1、 UIResponder 响应者类,继承自NSObject,它是一个响应者的基类,它提供了一些处理事件的方法
//什么是响应者(响应者对象):1.继承自UIResponder 2.能对ios事件中(触摸事件,晃动事件,远程事件)做出响应的对象就叫做响应者
// UILabel ,UIImageView 默认用户交互是关闭的
响应者链:确定事件作用的对象时,UIAppcation --->UIAppcationDelegate--->window--->视图控制器--->视图控制器上的子视图
响应事件:(有大到小)视图控制器上的子视图--->视图控制器--->window--->UIAppcationDelegate---UIAppcation 如果都不处理这个事件,事件就会被丢弃
ResponderView.m
ResponderView *redView = [[ResponderView alloc]initWithFrame:[UIScreenmainScreen].bounds];
redView.tag = 101;
redView.userInteractionEnabled = NO;
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView];
[redView release];
redView.tag = 101;
redView.userInteractionEnabled = NO;
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView];
[redView release];
ResponderView *yellowView = [[ResponderView alloc]initWithFrame:CGRectMake(30, 360, 320, 284)];
yellowView.tag = 102;
//关闭用户的交互造成响应者链就到这个位置断开了,事件只能找他的上一级处理,如果上一级都不处理,此事件就不了了之了
yellowView.userInteractionEnabled = YES;
yellowView.backgroundColor = [UIColor yellowColor];
[redView addSubview:yellowView];
[yellowView release];
ResponderView *greenView = [[ResponderView alloc]initWithFrame:CGRectMake(20, 20, 280, 244)];
greenView.tag = 103;
greenView.backgroundColor = [UIColor greenColor];
[yellowView addSubview:greenView];
[greenView release];
ResponderView *blueView = [[ResponderView alloc]initWithFrame:CGRectMake(20, 20, 240, 204)];
blueView.tag = 104;
blueView.backgroundColor = [UIColor blueColor];
[greenView addSubview:blueView];
yellowView.tag = 102;
//关闭用户的交互造成响应者链就到这个位置断开了,事件只能找他的上一级处理,如果上一级都不处理,此事件就不了了之了
yellowView.userInteractionEnabled = YES;
yellowView.backgroundColor = [UIColor yellowColor];
[redView addSubview:yellowView];
[yellowView release];
ResponderView *greenView = [[ResponderView alloc]initWithFrame:CGRectMake(20, 20, 280, 244)];
greenView.tag = 103;
greenView.backgroundColor = [UIColor greenColor];
[yellowView addSubview:greenView];
[greenView release];
ResponderView *blueView = [[ResponderView alloc]initWithFrame:CGRectMake(20, 20, 240, 204)];
blueView.tag = 104;
blueView.backgroundColor = [UIColor blueColor];
[greenView addSubview:blueView];
[blueView release];
}
ResponderView.m
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
switch (self.tag) {
case 101:
NSLog(@"红色视图");
break;
case 102:
NSLog(@"黄色视图");
break;
case 103:
NSLog(@"绿色视图");
break;
case 104:
NSLog(@"蓝色视图");
break;
default:
break;
case 101:
NSLog(@"红色视图");
break;
case 102:
NSLog(@"黄色视图");
break;
case 103:
NSLog(@"绿色视图");
break;
case 104:
NSLog(@"蓝色视图");
break;
default:
break;
}
==============================================
欢迎学习本文,未经博主许可,禁止转载!