UIEvent UIResponder UI_04

简介: 1、事件(UIEvent),是由硬件设备捕捉到用户对设备的操作,把这个操作抽象成一个事件对象   ios中三大事件:触Touches摸晃动事件Motion,远程控制事件RemoteControl;其中应有最广泛的是触摸事件UIView是支持触摸的,由于UIView内部没...
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 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];
     [moveView release];
第二类型:捏合视图对象
    PinchView *pinchView = [[PinchView alloc]initWithFrame:CGRectMake(60184200200)];
    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) + 100arc4random_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];
    //2.获取手指当前在自身视图的位置
    CGPoint currentLocation = [finger locationInView:self];
    //3.获取手指之前在视图上的位置
    CGPoint  previousLocation = [finger previousLocationInView:self];
    //4.计算手指在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);
}
@end
UIEvent <wbr>UIResponder <wbr>UI_04

——————————————————————————————————————————
第二种类型捏合视图对象:
首先要把默认的单触点关掉
PinchView.m  继承自UIView
-(instancetype)initWithFrame:(CGRect)frame{
    
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{
        
//得到集合中所有手指对象,并使用数组存储(数组有序)
    NSArray *allTouchs = [touches allObjects];
        //取出两个触点
        UITouch *touch1 = [allTouchs firstObject];
        
UITouch *touch2 = [allTouchs lastObject];
        
//求出两个手指对象当前的位置
        
CGPoint  firstTouch1 = [touch1 locationInView:self];
        
CGPoint secondTouch2 = [touch2 locationInViewself];
    
//求出当前两个手指的间距
        
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(00self.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);
}
UIEvent <wbr>UIResponder <wbr>UI_04

================================================================================
响应者链:
AppDelegate.m
//创建 Responder对象
    ResponderViewController *responderVC = [[ResponderViewController alloc]init];
    //将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];
 
    ResponderView *yellowView = [[ResponderView alloc]initWithFrame:CGRectMake(30360320284)];
    yellowView.
tag = 102;
    
//关闭用户的交互造成响应者链就到这个位置断开了,事件只能找他的上一级处理,如果上一级都不处理,此事件就不了了之了
    yellowView.
userInteractionEnabled = YES;
    yellowView.
backgroundColor = [UIColor yellowColor];
    [redView 
addSubview:yellowView];
    [yellowView 
release];
    
    
    
ResponderView *greenView = [[ResponderView alloc]initWithFrame:CGRectMake(2020280244)];
    greenView.
tag = 103;
    greenView.
backgroundColor = [UIColor greenColor];
    [yellowView 
addSubview:greenView];
    [greenView 
release];
    
    
ResponderView *blueView = [[ResponderView alloc]initWithFrame:CGRectMake(2020240204)];
    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;
    }
==============================================
欢迎学习本文,未经博主许可,禁止转载!
目录
相关文章
用&nbsp;Calenar&nbsp;获取日期
用&nbsp;Calenar&nbsp;获取日期
58 0
|
Apache
OSX&amp;nbsp;10.8+下开启Web&amp;nbsp;共享&amp;nbsp;的方法
MENU Home Archives About SUBSCRIBE MENU OSX 10.8+ Mountain Lion 下开启 Web Sharing(Web 共享)的方法 JUL 28, 2012 #OS X #how...
1241 0
|
Python
python&amp;nbsp;re&amp;nbsp;group()
python group() 正则表达式中,group()用来提出分组截获的字符串,()用来分组 import re a = "123abc456" print re.
1128 0
|
SQL Web App开发 测试技术
Web&amp;nbsp;in&amp;nbsp;Linux小笔记001
Linux灾难恢复:   Root密码修复 Centos single   Filesystem是硬盘文件根目录,无法再cd ..就像macitosh 硬盘图标 Pwd:显示绝对路径 MBR修复 模拟MBR被破坏和修复   C语言死循环程序   #include...
1451 0
|
索引
TableEdit&amp;nbsp;UI_10
1、tableView的编辑的步骤:  1.让tableView处于编辑状态,(默认所有的cell都处于编辑状态,默认下的编辑样式是删除)  2.设置哪些cell可以编辑  3.设置编辑的样式(删除,插入)  4.
716 0
UITableViewBase&amp;nbsp;UI_09
1、UITableView API文档总结:      1.UITableView的父类时,UIScrollView,所以它是可以滚动的,但是只能在竖直方向滚动.      2.UITableView是iOS中提供的用来以列表的形式展示数据,但是只有一列.
957 0
|
存储
DesignModeler&amp;nbsp;GestureRecgin…
DesignModeler : 设计模式     GestureRecginzer:手势识别 作者:韩俊强 原创版权地址:http://blog.sina.com.cn/s/blog_814ecfa90102vvm5.
828 0
中国梦&amp;nbsp;&amp;nbsp;每个农大人的梦
历经百年风霜,苦经岁月沧桑。农大,一个中原沃土上生长起来的大树,它在用它那不倒的生命力展示着农大的顽强与坚持,而这份苍劲和顽强,却来自于每个农大人,来自于他们的梦想,来自于他们的坚持,来自于他们的努力。
888 0
|
API iOS开发 开发者
2015&amp;nbsp;Objective-C&amp;nbsp;三大新特性
Overview 自 WWDC 2015 推出和开源 Swift 2.0 后,大家对 Swift 的热情又一次高涨起来,在羡慕创业公司的朋友们大谈 Swift 新特性的同时,也有很多像我一样工作上依然需要坚守着 Objective-C 语言的开发者们。
1231 0