实现手势识别的功能
点击,拖,拉,双击
手势事件识别必须要添加到一个UIView这个类里面去,一个单独存在的视图可以添加多个手势识别器。
IOS SDK5提供的6个手势识别器:Swipe,Rotation,Pinch,Pan,Long press,Tap。
步骤:
创建一个合适的手势识别器的对象
把这个手势识别器的对象绑定到一个视图上。
添加一些捕获手势事件发生的方法。(这个方法必须返回类型为空,这个方法要么是无参数类型的,要么只能接手一个UIGestureRecognizer类型的参数)
-(void)tapRecognizer:(UITapGestureRecognizer *)paramSender{}
-(void)tapRecognizer{}
手势识别器一般可分为两个大类,一个是单独的一个手势,一个是连贯的手势组合。
UIGestureRecognizer类有一个state属性的变量,这个变量代表了不同状态的手势以便手势识别器能够很好的进行辨别,单独的手势和连贯的手势识别器都有一组不同的状态值。
单独是手势识别器可以传递如下状态值。
1.UIGestureRecognizerStatePossible
2.UIGestureRecognizerstateRecognized
3.UIGestureRecognizerStateFailed
一组连贯的手势可以传递如下的状态值
1.UIGestureRecognizerStatePossible
2.UIGestureRecognizerStateBegan
3.UIGestureRecognizerStateChanged
4.UIGestureRecognizerStateEnded
5.UIGestureRecognizerStateFailed
捕获点击划屏的手势
创建一个UISwipeGestureRecognizer类型的对象,然后把它添加到你的对应的视图中。
#import "ViewController.h"
@interface ViewController ()
@property(nonatomic,strong)UISwipeGestureRecognizer *swipeGestureRecognizer;
@end
@implementation ViewController
@synthesize swipeGestureRecognizer;
- (void)viewDidLoad
{
[super viewDidLoad];
swipeGestureRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipes:)];
swipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
swipeGestureRecognizer.numberOfTouchesRequired = 1;//手指个数
[self.view addGestureRecognizer:swipeGestureRecognizer];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)handleSwipes:sender{
NSLog(@"left");
}
@end
捕获旋转的手势
创建一个UIRotationGestureRecognizer的监视器来监听这个旋转的手势,然后在绑定到你的视图中。代码如下:
@property(nonatomic,strong)UILabel *helloLabel;
@property(nonatomic,strong)UIRotationGestureRecognizer *rotationGestureRecognizer;
@property(nonatomic,unsafe_unretained)CGFloat rotationAngleInRadians;
- (void)viewDidLoad
{
[super viewDidLoad];
helloLabel = [[UILabel alloc]initWithFrame:CGRectZero];
helloLabel.text = @"hello world";
helloLabel.font = [UIFont systemFontOfSize:16.0];
[helloLabel sizeToFit];
helloLabel.center = self.view.center;
[self.view addSubview:helloLabel];
rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(handleRotations:)];
[self.view addGestureRecognizer:rotationGestureRecognizer];
}
-(void)handleSwipes:(UIRotationGestureRecognizer *)paramSender{
if(helloLabel == nil){
return;
}
helloLabel.transform = CGAffineTransformMakeRotation(rotationAngleInRadians + paramSender.rotation);
if(paramSender.state == UIGestureRecognizerStateEnded){
self.rotationAngleInRadians += paramSender.rotation;
}
}
rotationGestureRecognizer 旋转手势的监听捕获实例对象
rotationAngleInRadians 在旋转的时候需要为我们标签对象设置一个位置对象信息,当我们旋转一次的时候我们都会把一个新的位置保存在这个对象里面,达到一个旋转效果。
捕获类似拖拽手势
利用UIPanGestureRecognizer这个手势识别器
@property(nonatomic,strong)UIPanGestureRecognizer *panGestureRecognizer;
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect labelFrame = CGRectMake(0.0f, 0.0f, 150.0f, 100.0f);
helloLabel = [[UILabel alloc]initWithFrame:labelFrame];
helloLabel.text = @"hello world";
helloLabel.backgroundColor = [UIColor blackColor];
helloLabel.textColor = [UIColor whiteColor];
helloLabel.textAlignment = UITextAlignmentCenter;
helloLabel.userInteractionEnabled = YES;
[self.view addSubview:helloLabel];
panGestureRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePanGestures:)];
panGestureRecognizer.minimumNumberOfTouches = 1;
panGestureRecognizer.maximumNumberOfTouches = 1;
[helloLabel addGestureRecognizer:panGestureRecognizer];
}
-(void)handlePanGestures:(UIRotationGestureRecognizer *)paramSender{
if(paramSender.state != UIGestureRecognizerStateFailed && paramSender.state != UIGestureRecognizerStateFailed){
CGPoint location = [paramSender locationInView:paramSender.view.superview];
paramSender.view.center = location;
}
}
通过使用locationInView这个方法,来获取我们手势坐标,为了能够捕获到多个手势的位置,我们需要使用locationOfTouch:inView:这个方法。然后通过使用UIPanGestureRecognizer这个对象的minimumNumberOfTouches和maximumNumberOfTouches参数。你就可以在同一个时间来捕获多个手指的拖拽的动作了。