(10) iphone 开发 用户点击,触摸和手势识别 解析

来源:互联网 发布:sql查询分析器怎么打开 编辑:程序博客网 时间:2024/05/18 02:49

用户对屏幕(人机交互)的所有操作都可称为事件。事件包括用户点击,触摸和手势识别等。

一:UIView及UIViewController都继承自UIResponder类,而具有在屏幕上显示功能的类及其控制器类(UIControl)也都继承自UIView,所以他们都时响应者(即所有视图和所由控件都是响应者)。

内容结构图:


二:响应着链:事件是向上传递的(这点类似于java中的异常处理:throw),当当前响应者处理不了事件的时,他会将此事件传递给他的父视图,逐级向更高一层(下一个对象)传递。

如果窗口处理不了当前事件,此事件将会被传递到应用程序的UIApplication实例。如果还是处理不了,此事件将进入睡眠状态。

响应者链图:


转发事件:从上面可以看到,事件是在逐个对象间的传递,当视图(如:表视图)不包含动作事件(如:轻扫手势)时,可能不会执行传递工作,这样其他对象将无法获得响应,阻止了其他视图的手势识别。这时就需要手动去传递:在下一个响应者上调用相同的方法来传递该对象,
-(void)responder:(UIEvent*)event{//如果可处理传递事件,执行此句:[self handleEvent:event ];//否则手动传递到下一个响应者(递归)[self.nextResponder responder:event ];}

三:  4个通知手势的方法:

1.开始触摸:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {    [super touchesMoved:touches withEvent:event];    //touches :动作的数量()  每个对象都是一个UITouch对象,而每一个事件都可以理解为一个手指触摸。    //获取任意一个触摸对象    UITouch *touch = [touches anyObject];    //获得触摸对象的点击数量,只捕捉一个触摸对象的点击。    NSInteger numTaps=[touch tapCount];    //获得触摸对象的数量    NSInteger numTouchs=[touches count];        //触摸对象的位置    CGPoint previousPoint = [touch previousLocationInView:self.view];
2.移动:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { }
3.离开:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { }

4.取消:  电话呼入等中断手势时,会调用此方法。
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { }

四:手势识别器(UIGestureRecognizer):可以理解成一个容器,里面可添加它的子类(如:捏合(UIPinchGestureRecognizer),轻扫(UISwiperGestureRecognizer),点击(UITapGestureRecognizer)),这些子类都是封装好的,可响应屏幕上的事件进行处理。当然也可自定义手势识别器的子类,来响应需要的事件,这样显得更灵活些。

1.调用系统的手势识别器 以捏合为例:

- (void)viewDidLoad {    [super viewDidLoad];    //实例化捏合手势识别器,将实例好的手势识别器对象作为实参传递到doPinch:。    UIPinchGestureRecognizer *pinch = [[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(doPinch:)] autorelease];    //将实例化捏合手势识别器加入视图识别器中。    [self.view addGestureRecognizer:pinch];}- (void)doPinch:(UIPinchGestureRecognizer *)pinch {    //如果开始触发    if (pinch.state == UIGestureRecognizerStateBegan) {      CGFloat  initialFontSize = label.font.pointSize;    } else {        //按照捏合比例扩大或缩小        label.font = [label.font fontWithSize:initialFontSize * pinch.scale];    }}

2.自定义手势识别器:

自定义MyGestureRecognizerView。

MyGestureRecognizerView.h

#import <UIKit/UIKit.h>@interface MyGestureRecognizerView : UIGestureRecognizer@end

MyGestureRecognizerView.m

#import "MyGestureRecognizerView.h"@implementation MyGestureRecognizerView- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {    [super touchesBegan:touches withEvent:event];        //可以填写自己所需要的识别机制。。。。。。        //获取任意一个触摸对象    UITouch *touch = [touches anyObject];    //获得触摸对象的点击数量,只捕捉一个触摸对象的点击。     NSInteger numTaps=[touch tapCount];    //获得触摸对象的数量     NSInteger numTouchs=[touches count];        //触摸对象的位置    CGPoint previousPoint = [touch previousLocationInView:self.view];        }- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {    [super touchesMoved:touches withEvent:event];       //可以填写自己所需要的识别机制。。。。。。。}@end

在主视图控制器中:

HelloViewController.h
#import <UIKit/UIKit.h>@interface HelloViewController : UIViewController@end

HelloViewController.m

#import "HelloViewController.h"#import "MyGestureRecognizerView.h"@interface HelloViewController ()@end@implementation HelloViewController- (void)viewDidLoad{        MyGestureRecognizerView *mygest=[[MyGestureRecognizerView alloc] initWithTarget:self action:@selector(doit:)];        [self.view addGestureRecognizer:mygest];    [super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.}-(void)doit:(MyGestureRecognizerView *)my{    }- (void)viewDidUnload{    [super viewDidUnload];    // Release any retained subviews of the main view.}- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);}@end