手势在iOS开发应用中并不算少见,但是应用的时候往往没报错,但是触发却没有效果,现在简单整理下.
1.所有的手势代理,都是
2.如果是图片使用手势,记得允许图片可以交互作用
3.敲击手势
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.view setBackgroundColor:[UIColor whiteColor]];
self.title = @"敲击手势";
self.imgView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 100, 300, 200)];
[self.imgView setImage:[UIImage imageNamed:@"img"]];
[self.view addSubview:self.imgView];
//图片允许交互,别忘记设置,否则手势没有反应
[self.imgView setUserInteractionEnabled:YES];
//1.创建一个"敲击手势"对象
UITapGestureRecognizer *tapGest = [[UITapGestureRecognizer alloc]init];
//2.设置敲击次数
tapGest.numberOfTapsRequired = 1;
//3.手指头数量,默认不写
// tapGest.numberOfTouchesRequired = 2;
//4.代理
tapGest.delegate =self;
//5.手势添加到View
[self.imgView addGestureRecognizer:tapGest];
//6.手势监听方法
[tapGest addTarget:self action:@selector(tapGestMethod:)];
}
/**
* 监听方法
*/
-(void)tapGestMethod:(UITapGestureRecognizer *)tapGest{
NSLog(@"可以点击了:%s",__func__);
}
/**
* 手势代理 (实现view只能左边点击,右边无效)
*/
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
CGPoint location = [touch locationInView:touch.view];
if (location.x<= touch.view.bounds.size.width/2) {
return YES;
}
return NO;
}
4.长按手势
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor whiteColor]];
self.title = @"长按手势";
self.imgView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 100, 80, 80)];
[self.imgView setImage:[UIImage imageNamed:@"img"]];
[self.view addSubview:self.imgView];
//图片允许交互,别忘记设置,否则手势没有反应
[self.imgView setUserInteractionEnabled:YES];
//初始化一个长按手势
UILongPressGestureRecognizer *longPressGest = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressView:)];
//长按等待时间
longPressGest.minimumPressDuration = 3;
//长按时候,手指头可以移动的距离
longPressGest.allowableMovement = 30;
[self.imgView addGestureRecognizer:longPressGest];
}
-(void)longPressView:(UILongPressGestureRecognizer *)longPressGest{
NSLog(@"%ld",longPressGest.state);
if (longPressGest.state==UIGestureRecognizerStateBegan) {
NSLog(@"长按手势开启");
} else {
NSLog(@"长按手势结束");
}
}
5.旋转手势
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor whiteColor]];
self.title = @"旋转手势";
self.imgView = [[UIImageView alloc]initWithFrame:CGRectMake(80, 100, 160, 160)];
[self.imgView setImage:[UIImage imageNamed:@"img"]];
[self.view addSubview:self.imgView];
//图片允许交互,别忘记设置,否则手势没有反应
[self.imgView setUserInteractionEnabled:YES];
//初始化一个旋转手势
UIRotationGestureRecognizer *rotationGest = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationView:)];
[self.imgView addGestureRecognizer:rotationGest];
}
-(void)rotationView:(UIRotationGestureRecognizer *)rotationGest{
//旋转角度.也是一个累加过程
NSLog(@"%f",rotationGest.rotation);
//设置图片旋转
self.imgView.transform = CGAffineTransformRotate(self.imgView.transform, rotationGest.rotation);
//清除旋转角度累计
rotationGest.rotation = 0;
}
.6.拖拽手势
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor whiteColor]];
self.title = @"拖拽手势";
self.imgView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 100, 80, 80)];
[self.imgView setImage:[UIImage imageNamed:@"img"]];
[self.view addSubview:self.imgView];
//图片允许交互,别忘记设置,否则手势没有反应
[self.imgView setUserInteractionEnabled:YES];
//初始化一个拖拽手势
UIPanGestureRecognizer *panGest = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panView:)];
[self.imgView addGestureRecognizer:panGest];
}
-(void)panView:(UIPanGestureRecognizer *)panGest{
//拖拽的距离(距离是一个累加)
CGPoint trans = [panGest translationInView:panGest.view];
NSLog(@"%@",NSStringFromCGPoint(trans));
//设置图片移动
CGPoint center = self.imgView.center;
center.x += trans.x;
center.y += trans.y;
self.imgView.center = center;
//清除累加的距离
[panGest setTranslation:CGPointZero inView:panGest.view];
}
7.轻扫手势
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor whiteColor]];
self.title = @"轻扫手势";
self.imgView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 100, 300, 200)];
[self.imgView setImage:[UIImage imageNamed:@"img"]];
[self.view addSubview:self.imgView];
//图片允许交互,别忘记设置,否则手势没有反应
[self.imgView setUserInteractionEnabled:YES];
//手势有四个方向,根据需要选择
//UISwipeGestureRecognizerDirectionRight 向右轻扫
//UISwipeGestureRecognizerDirectionLeft 向左轻扫
//UISwipeGestureRecognizerDirectionUp 向上
//UISwipeGestureRecognizerDirectionDown 向下
//初始化一个轻扫手势
UISwipeGestureRecognizer *swipeGest = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeView:)];
[self.imgView addGestureRecognizer:swipeGest];
}
-(void)swipeView:(UISwipeGestureRecognizer *)swipeGest{
NSLog(@"手势状态:%ld",swipeGest.state);
//如何判断开始和跟结束
}
8.捏合手势(放大/缩小)
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor whiteColor]];
self.title = @"捏合手势";
self.imgView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 100, 300, 200)];
[self.imgView setImage:[UIImage imageNamed:@"img"]];
//图片允许交互,别忘记设置,否则手势没有反应
[self.imgView setUserInteractionEnabled:YES];
UIPinchGestureRecognizer *pinTap = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinView:)];
[self.imgView addGestureRecognizer:pinTap];
[self.view addSubview:self.imgView];
}
-(void)pinView:(UIPinchGestureRecognizer *)pinGest{
//缩放的比例是一个"累加"过程
NSLog(@"%s----%f",__func__,pinGest.scale);
#warning 放大图片后, 再次缩放的时候,马上回到原先的大小
//self.imageView.transform = CGAffineTransformMakeScale(pinGest.scale, pinGest.scale);
self.imgView.transform = CGAffineTransformScale(self.imgView.transform, pinGest.scale, pinGest.scale);
// 让比例还原,不要累加
// 解决办法,重新设置scale
pinGest.scale = 1;
}
9.旋转+捏合+拖拽
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor whiteColor]];
self.title = @"旋转+捏合+拖拽";
self.imgView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 100, 300, 200)];
[self.imgView setImage:[UIImage imageNamed:@"img"]];
[self.view addSubview:self.imgView];
//图片允许交互,别忘记设置,否则手势没有反应
[self.imgView setUserInteractionEnabled:YES];
//默认情况下,控件只能监听一种手势
//如果要监听到多个手势,设置代理方法,告知允许多个手势存在
//旋转
UIRotationGestureRecognizer *rotaitonGest = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationView:)];
rotaitonGest.delegate =self;
[self.imgView addGestureRecognizer:rotaitonGest];
//捏合
UIPinchGestureRecognizer *pinchGest = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchView:)];
[self.imgView addGestureRecognizer:pinchGest];
//拖拽
UIPanGestureRecognizer *panGest = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panView:)];
[self.imgView addGestureRecognizer:panGest];
}
-(void)rotationView:(UIRotationGestureRecognizer *)rotationGest{
//旋转角度
//旋转的角度也一个累加的过程
NSLog(@"旋转角度 %f",rotationGest.rotation);
// 设置图片的旋转
self.imgView.transform = CGAffineTransformRotate(self.imgView.transform, rotationGest.rotation);
// 清除 "旋转角度" 的累
rotationGest.rotation = 0;
}
-(void)pinchView:(UIPinchGestureRecognizer *)pinchGest{
//设置图片缩放
self.imgView.transform = CGAffineTransformScale(self.imgView.transform, pinchGest.scale, pinchGest.scale);
// 还源
pinchGest.scale = 1;
}
-(void)panView:(UIPanGestureRecognizer *)panGest{
//拖拽的距离(距离是一个累加)
CGPoint trans = [panGest translationInView:panGest.view];
NSLog(@"%@",NSStringFromCGPoint(trans));
//设置图片移动
CGPoint center = self.imgView.center;
center.x += trans.x;
center.y += trans.y;
self.imgView.center = center;
//清除累加的距离
[panGest setTranslation:CGPointZero inView:panGest.view];
}
/**
* 多个手势同时存在的代理,不能忘记
*/
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}