UIGestureRecognizer:能识别用户在某个view上面做的一些常见手势;
UIGestureRecognizer是一个抽象类,使用它的子类才能处理具体的手势
- UITapGestureRecognizer 轻击
- UILongPressGestureRecognizer 长按
- UISwipeGestureRecognizer 轻扫
- UIPanGestureRecognizer 拖动
- UIRotationGestureRecognizer 旋转
- UIPinchGestureRecognizer 捏合
前提
下面所有的操作都基于这部分:在viewcontroller里面添加一张imageView,viewcontroller里面引入这张图片,名为smileImageView
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *smileImageView;
@end
点击手势
在smileImageView添加轻击手势:
#pragma mark 点击手势
-(void)addTapGes{
UITapGestureRecognizer * tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGes:)];
[self.smileImageView addGestureRecognizer:tapGes];
}
-(void)tapGes:(UITapGestureRecognizer *)tap{
NSLog(@"%s",__func__);
}
长按手势
在smileImageView上面添加长按手势;
长按手势有多种状态,我们可以根据不同的状态做不同的处理
-(void)addLongGes{
UILongPressGestureRecognizer * longGes = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longGes:)];
[self.smileImageView addGestureRecognizer:longGes];
}
-(void)longGes:(UILongPressGestureRecognizer *)longGes{
//长按手势有多种状态,我们可以根据不同的状态做不同处理
if (longGes.state == UIGestureRecognizerStateBegan) {
NSLog(@"UIGestureRecognizerStateBegan");
}else if (longGes.state == UIGestureRecognizerStateChanged){
NSLog(@"UIGestureRecognizerStateChanged");
}else if (longGes.state == UIGestureRecognizerStateEnded){
NSLog(@"UIGestureRecognizerStateEnded");
}
}
轻扫手势
在smileImageView上面添加轻扫手势;
可以设置轻扫的方向,默认向右;
一个轻扫手势只能添加一个方向,如果想要有多个轻扫方向,就要有多个轻扫手势;
#pragma mark 轻扫手势
-(void)addSwipGes{
UISwipeGestureRecognizer * swipGes = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipGes:)];//不指定手势方向,那这个手势的方向默认向右
[self.smileImageView addGestureRecognizer:swipGes];
//手势方向默认向右,并且每个手势只能指定一个方向
UISwipeGestureRecognizer * swipGes1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipGes:)];
[self.smileImageView addGestureRecognizer:swipGes1];
swipGes.direction = UISwipeGestureRecognizerDirectionLeft;//设置方向向左
}
-(void)swipGes:(UISwipeGestureRecognizer *)swipGes{
if (swipGes.direction == UISwipeGestureRecognizerDirectionLeft) {
NSLog(@"UISwipeGestureRecognizerDirectionLeft");
}else if (swipGes.direction == UISwipeGestureRecognizerDirectionRight){
NSLog(@"UISwipeGestureRecognizerDirectionRight");
}
}
拖动手势
在smileImageView上面添加拖动手势;
拖动手势获取的偏移量是相对于最原始的点;
要做清0操作:不让偏移量进行累计,获取的就是相对于上一次的值,每一次走的值;
#pragma mark 拖动手势
-(void)addDrapGes{
UIPanGestureRecognizer * panGes = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGes:)];
[self.smileImageView addGestureRecognizer:panGes];
}
-(void)panGes:(UIPanGestureRecognizer *)panGes{
//获取偏移量,偏移量是相对于最原始的点
CGPoint transPoint = [panGes translationInView:self.view];
NSLog(@"%@",NSStringFromCGPoint(transPoint));//这个点是相对于最初始的原点
self.smileImageView.transform = CGAffineTransformTranslate(self.smileImageView.transform, transPoint.x, transPoint.y);
//清0操作(不让偏移量进行累加,获取的是相对于上一次的值,每一次走的值)
[panGes setTranslation:CGPointMake(0, 0) inView:self.smileImageView];
}
旋转手势
在smileImageView上面添加旋转手势,让smileImageView可以旋转;
获取的弧度是原始弧度;需要清0
-(void)addRotationGes{
UIRotationGestureRecognizer * rotationGes = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGes:)];
[self.smileImageView addGestureRecognizer:rotationGes];
}
-(void)rotationGes:(UIRotationGestureRecognizer *)rotationGes{
//获取旋转手势(已经是弧度)
//获取的是最原始的弧度
CGFloat rotationF = rotationGes.rotation;
self.smileImageView.transform = CGAffineTransformRotate(self.smileImageView.transform, rotationF);
//弧度清0
[rotationGes setRotation:0];
}
缩放手势
在smileImageView上面添加捏合手势,让smileImageView可以缩放;
取到的缩放比例也是相对于最原始的比例
#pragma mark 捏合手势
-(void)addPinchGes{
UIPinchGestureRecognizer * pinGes = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGes:)];
[self.smileImageView addGestureRecognizer:pinGes];
}
-(void)pinchGes:(UIPinchGestureRecognizer *)pinchGes{
//获取缩放比例(最原始的比例)
CGFloat scale = pinchGes.scale;
//缩放
self.smileImageView.transform = CGAffineTransformScale(self.smileImageView.transform, scale, scale);
[pinchGes setScale:1];
}
代理方法
举例1
手势有自己的代理方法,例如可以设置是否响应手势;
为轻击事件添加代理,图片右侧可以响应手势,左侧不可以
@interface ViewController ()//实现代理
@property (weak, nonatomic) IBOutlet UIImageView *smileImageView;
@end
#pragma mark 点击手势
-(void)addTapGes{
UITapGestureRecognizer * tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGes:)];
[self.smileImageView addGestureRecognizer:tapGes];
tapGes.delegate = self;//添加代理
}
#pragma mark 代理方法
//是否可以响应事件
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
CGPoint ponit = [touch locationInView:self.smileImageView];
if (ponit.x/self.smileImageView.frame.size.width >0.5 ) {//是图片右边,则可以响应事件,否则不可用
return YES;
}else{
return NO;
}
}
举例2
多个手势同时响应
例如既可以放大缩小,也可旋转。
//是否可以支持多个手势
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
#pragma mark 捏合手势
-(void)addPinchGes{
UIPinchGestureRecognizer * pinGes = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGes:)];
[self.smileImageView addGestureRecognizer:pinGes];
pinGes.delegate = self;//添加代理
}
-(void)pinchGes:(UIPinchGestureRecognizer *)pinchGes{
//获取缩放比例(最原始的比例)
CGFloat scale = pinchGes.scale;
//缩放
self.smileImageView.transform = CGAffineTransformScale(self.smileImageView.transform, scale, scale);
[pinchGes setScale:1];
}
#pragma mark 旋转手势
-(void)addRotationGes{
UIRotationGestureRecognizer * rotationGes = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGes:)];
[self.smileImageView addGestureRecognizer:rotationGes];
rotationGes.delegate = self;//添加代理
}
-(void)rotationGes:(UIRotationGestureRecognizer *)rotationGes{
//获取旋转手势(已经是弧度)
//获取的是最原始的弧度
CGFloat rotationF = rotationGes.rotation;
//旋转
self.smileImageView.transform = CGAffineTransformRotate(self.smileImageView.transform, rotationF);
//弧度清0
[rotationGes setRotation:0];
}