http://www.th7.cn/Program/IOS/201301/123195.shtml
使用复杂的触摸和手势
Apple有各种手势识别器的Class,下面,将使用几个手势识别器,实现:轻按、轻扫、张合、旋转(摇动暂不涉及)。每个手势都将有一个弹出式窗口的反馈。
在ViewController.m文件中,
1-点击事件
-(void)foundTap:(UITapGestureRecognizer *)recognizer{
UIAlertView* alert=[[UIAlertView alloc] initWithTitle:@"Hello" message:@"tapped" delegate:nil cancelButtonTitle:@"OK"otherButtonTitles:nil];
[alert show];
}
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *tapRecognizer; //创建一个轻按手势识别器
tapRecognizer=[[UITapGestureRecognizer alloc]initWithTarget:self
action:@selector(foundTap:)];
//初始化识别器,并使用函数指针(方法指针)触发同时实现方法
tapRecognizer.numberOfTapsRequired=1; //轻按对象次数(1)触发此行为
tapRecognizer.numberOfTouchesRequired=1; //要求响应的手指数
[self.view addGestureRecognizer:tapRecognizer]; //相应的对象添加控制器
}
仔细观察实现的方法,均是按着以下步骤:新建一个控制器实例--实例初始化--将其添加到类对象--实现函数指针中的方法(@selector()).
【若想获得轻按或轻扫手势坐标,可添加:
CGPoint location=[recognizer locationInView:<the view>
<the view>即为手势识别器的视图名;location有两个参数x和y】
2-轻扫:
接下来,我们按着上述步骤实现轻扫:
函数指针指向的方法:
-(void)foundSwipe:(UISwipeGestureRecognizer *)recognizer{
UIAlertView* alert=[[UIAlertView alloc] initWithTitle:@"Hello" message:@"swiped" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
还是viewDidLoad:
UISwipeGestureRecognizer *swipeRecognizer; //创建一个轻扫识别器
swipeRecognizer=[[UISwipeGestureRecognizer alloc]initWithTarget:self
action:@selector(foundSwipe:)];
//对象初始化 并有函数指针
swipeRecognizer.direction=
UISwipeGestureRecognizerDirectionRight|UISwipeGestureRecognizerDirectionLeft;
//手势相应 向左或向右滑动
swipeRecognizer.numberOfTouchesRequired=1; //扫动对象次数
[self.view addGestureRecognizer:swipeRecognizer]; //向对象添加控制器
相应扫动方向时,有四个方向可供选择:
UISwipeGestureRecognizerDirectionRight/Left/Up/Down;
3-张合:
将实现放大缩小
函数指针方法:
-(void)foundPinch:(UIPinchGestureRecognizer *)recognizer{ //注意此处的类
NSString *message;
double scale;
scale=recognizer.scale; //识别器的scale(刻度尺、尺度)
//可用scale结合openGLES的glScalef实现缩放
message=[[NSString alloc]initWithFormat:@"缩放:Scale:%1.2f,Velocity:%1.2f",recognizer.scale,recognizer.velocity];
UIAlertView* alert=[[UIAlertView alloc] initWithTitle:@"Hello" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
ViewDidLoad中:
UIPinchGestureRecognizer *pinchReconizer; //新建一个张合识别器对象
pinchReconizer=[[UIPinchGestureRecognizer alloc]initWithTarget:self
action:@selector(foundPinch)];
//初始化对象
[self.view addGestureRecognizer:pinchReconizer]; //添加控制器
4-旋转
首先Cocoa类使用弧度为单位,角度和弧度的转换关系:
角度=弧度*180/Pi
函数指针指向的方法:
-(void)foundRotation:(UIRotationGestureRecognizer *)recognizer{
NSString *message;
double rotation;
rotation=recognizer.rotation; //返回一个控制器角度
//在openGLES中可使用rotation结合glRoatef实现旋转
message=[[NSString alloc] initWithFormat:@"旋转,Radians:%1.2f,Velocity:%1.2f",
recognizer.rotation,recognizer.velocity];
UIAlertView* alert=[[UIAlertView alloc] initWithTitle:@"Hello" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
viewDidLoad如下:
UIRotationGestureRecognizer *rotationRecognizer; //新建旋转手势控制器对象
rotationRecognizer=[[UIRotationGestureRecognizer alloc]initWithTarget:self
action:@selector(foundRotation:)];
//初始化对象
[self.view addGestureRecognizer:rotationRecognizer]; //添加控制器