UI第四天:事件处理

来源:互联网 发布:手机手柄连接软件 编辑:程序博客网 时间:2024/06/05 11:21
⼀、事件的基本概念
UIEvent:事件,是由硬件捕捉的⼀个表⽰⽤户操作设备的对象。
分三类:触摸事件、晃动事件、远程控制事件
触摸事件:⽤户通过触摸设备屏幕操作对象、输⼊数据。⽀持多点 触摸,包含1个到多个触摸点

⼆、触摸的基本概念
UIView⽀持触摸事件(因为继承于UIResponder),⽽且⽀持多 点触摸。
需要定义UIView⼦类,实现触摸相关的⽅法。
touches..began、touches..moved、touches...ended、 touches..canceled。
⼿势:有规律的触摸。
UITouch代表触摸在屏幕上的⼀根⼿指。
可以获取触摸时间和触摸 位置。
如何获取touch对象。
touches集合中包含了视图上的所有⼿势
//需要让touchview响应事件
   
// 实现响应者类中的方法来捕捉出摸事件
   
TouchView *touch = [[TouchViewalloc]initWithFrame:CGRectMake(100,100,100,100)];
    touch.
backgroundColor = [UIColorredColor];
    [
self.viewaddSubview:touch];
    [touch release];

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
}
实现一个可拖动可变色的TouchView
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    // touch 保留手指信息(触摸的点)
    UITouch *touch = [touches anyObject];
   // NSLog(@"%@",touch);
    //取出当前触摸的点
    //返回一个当前触摸的点相对于传进去的参数view
    CGPoint p = [touch locationInView:self.window];
    NSLog(@"%@",NSStringFromCGPoint(p));
    CGPoint p1 = [touch previousLocationInView:self.window] ;
    NSLog(@"%@",NSStringFromCGPoint(p1));
    CGPoint p2;
    p2.x = p.x - p1.x ;
    p2.y = p.y - p1.y ;
    NSLog(@"%@",NSStringFromCGPoint(self.center));
    self.center = CGPointMake(self.center.x + p2.x, self.center.y +p2.y);
   
    self.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0  blue:arc4random()%256/255.0  alpha:1];
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"触摸中断比如小退");
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"触摸结束");
}
// UIActionSheet的使用方法是一个从下往上的弹框用于删除时
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"%@",[actionSheet buttonTitleAtIndex:buttonIndex]);
   
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:@"确认删除" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:@"额外", nil];
    [sheet showInView:self.window];
    [sheet release];

三、响应者链
由多个响应者对象组成的链。
什么是响应者
UIResponder。响应者类。
iOS中所有能响应事件(触摸、晃动、远程事件)的对象都是响应 者。
系统定义了⼀个抽象的⽗类UIResponder来表⽰响应者。其⼦类都 是响应者。
检测触碰视图
硬件检测到触摸操作,会将信息交给UIApplication,开始检测。
UIApplication -> window -> viewController -> view -> 检测所有⼦ 视图 最终确认触碰位置,完成响应者链的查询过程。
处理触碰事件
检测到响应者后,实现touchesBegan:withEvent:等⽅法,即处理事 件。
如果响应者没有处理事件,事件会向下传递。如果没有响应者处理, 则丢弃触摸事件。
事件处理的顺序与触摸检测查询相反。
触摸的⼦视图 -> view -> viewController -> window -> UIApplication
阻断响应者链
响应者链可以被打断。⽆法完成检测查询过程。
视图类的属性 : userInteractionEnabled。关闭后能阻断查询过 程。
总结
今天我们学习了事件和触摸的基本概念
学习如何响应和处理触摸事件
学习了响应者和响应者链

 /*
    
响应者链分为两个过程
     1.
查询过程
    
当你点击屏幕
    
先定位到应用程序->window->控制器->self.view->view子视图一一查找直到定位被点击的子视图查询过程结束
     2.
响应过程
    首先 看本视图能不能处理事件(实现了touchBange等方法就叫做可以处理事件)->父视图->一层一层往下看能不能处理直到window 如果都不能处理该次点击事件 被遗弃(无效点击)
    //    button.userInteractionEnabled = NO;阻断button查询来阻断响应者链
     注意:UILabel UIImageView的交互默认是关闭的
     */
//晃动事件
//开始晃动
-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    NSLog(@"开始晃动");
   
}
//中止晃动
-(void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    NSLog(@"中止晃动");
}
//结束晃动
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    NSLog(@"结束晃动");
    //跳转界面
    SecondViewController *secondVC = [[SecondViewController alloc]init];
    [self presentViewController:secondVC animated:YES completion:nil];
}

自定义UIview来自己写一个button实现button方法
//为了方便下面调用声明成属性
@property(nonatomic,retain)id target;
@property(nonatomic,assign)SEL action;//方法类型
-(instancetype)initWithFrame:(CGRect)frame target:(id)target action:(SEL)action;

//重写初始化方法
-(instancetype)initWithFrame:(CGRect)frame target:(id)target action:(SEL)action
{
    self = [super initWithFrame:frame];
    if (self) {
        self.target =target;
        self.action =action;
    }
    return self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
   
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
   
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    //NSLog(@"点击");
    //使用self.target 对象调用action方法
    //让一个对象去调用这个对象类里的方法
    //object 可携带的参数
    [self.target performSelector:self.action withObject:self];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent
*)event
{
   
}
0 0
原创粉丝点击