您的位置:首页 > 移动开发 > IOS开发

iOS手势识别的简单应用

2016-06-22 11:15 459 查看
摘要: UITapGestureRecognizer(敲击)UIPinchGestureRecognizer(捏合,用于缩放)UIPanGestureRecognizer(拖拽)UISwipeGestureRecognizer(轻扫)UIRotationGestureRecognizer(旋转)UILongPressGestureRecognizer(长按)

六种手势

1.手势分类UITapGestureRecognizer(敲击)设置numberOfTapsRequired点击的次数结合代理(BOOL) gestureRecognizer可以选定可点击的区域UIPinchGestureRecognizer(捏合,用于缩放)注意复位问题UIPanGestureRecognizer(拖拽)UISwipeGestureRecognizer(轻扫)UIRotationGestureRecognizer(旋转)UILongPressGestureRecognizer(长按)长按的两次响应问题的解决2.对每一个手势的注意点及代码实现进行说明UITapGestureRecognizer(敲击)
#pragma mark - tap敲击
/**
*  1.numberOfTapsRequired设置点击几次响应
*  2.代理与(BOOL) gestureRecognizer结合,可确定图片哪个区域可以点,哪些不能点
*/
-(void) setTap{
//创建点按手势
//initWithTarget:谁监听这个事件呢,一般控制器
UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
//delegate
tap.delegate=self;
//click counts
tap.numberOfTapsRequired=2;
//添加手势,用的是多态addGestureRecognizer:(UIGestureRecognizer*)gestureRecognizer(父类)
[self.imageView addGestureRecognizer:tap];
}

-(void) tap:(UITapGestureRecognizer *) tap{
NSLog(@"%s",__func__);
}
#pragma mark - UIGestureRecognizerDelegate//与tap结合-(BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch )touch{//获取当前触摸点在self.imageView的哪个位置CGPoint curP=[touch locationInView:self.imageView];if (curP.x<self.imageView.bounds.size.width0.5) {return YES;}else{return NO;}}
```
UIPinchGestureRecognizer(捏合,用于缩放)
#pragma mark - pinch捏合,用于缩放-(void) setPinch{UIPinchGestureRecognizer *pinch=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];pinch.delegate=self;[self.imageView addGestureRecognizer:pinch];}-(void) pinch:(UIPinchGestureRecognizer *) pinch{NSLog(@"%s",func);self.imageView.transform=CGAffineTransformScale(self.imageView.transform, pinch.scale, pinch.scale);pinch.scale=1;}```UIPanGestureRecognizer(拖拽)
#pragma mark - pan拖拽
/**
*  复位是指返回到起始的状态,再能过相对上一个状态tansform来解决一些问题
*/
-(void) setPan{
UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];

[self.imageView addGestureRecognizer:pan];
}

-(void) pan:(UIPanGestureRecognizer *) pan{
NSLog(@"%s",__func__);
//    CGPoint curP=[pan locationInView:self.imageView];
//获取图片移动距离
CGPoint curP=[pan translationInView:self.imageView];
self.imageView.transform=CGAffineTransformTranslate(self.imageView.transform, curP.x, curP.y);
//复位
[pan setTranslation:CGPointZero inView:self.imageView];
}
UISwipeGestureRecognizer(轻扫)
#pragma mark - swipe轻扫
-(void) setSwipe{
//添加向左滑动
UISwipeGestureRecognizer *swipe=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
swipe.direction=UISwipeGestureRecognizerDirectionLeft;
[self.imageView addGestureRecognizer:swipe];
//添加向右滑动
UISwipeGestureRecognizer *swipeDown=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
swipeDown.direction=UISwipeGestureRecognizerDirectionRight;
[self.imageView addGestureRecognizer:swipeDown];

}
-(void) swipe:(UISwipeGestureRecognizer *)swipe{
NSLog(@"%s",__func__);
}
UIRotationGestureRecognizer(旋转)
#pragma mark - rotation旋转
-(void) setRotation{
UIRotationGestureRecognizer *rotation=[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];
rotation.delegate=self;
[self.imageView addGestureRecognizer:rotation];
}
-(void) rotation:(UIRotationGestureRecognizer *) rotation{
NSLog(@"%s",__func__);
NSLog(@"%f",rotation.rotation);
self.imageView.transform=CGAffineTransformRotate(self.imageView.transform, rotation.rotation);
[rotation setRotation:0];
//    rotation.rotation=0;

}
UILongPressGestureRecognizer(长按)
#pragma mark - longPress长按
-(void) setLongPress{
UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
[self.imageView addGestureRecognizer:longPress];
}
/**
*  长按的一些注意点,那就是长按时触发一次,离开时再触发一次
*  加个判断,结束与开始选一个触发的时间
*  @param longPress 传参
*/
-(void) longPress:(UILongPressGestureRecognizer *) longPress{
if (longPress.state==UIGestureRecognizerStateBegan) {
NSLog(@"%s",__func__);
}

}
当同时多个手势可以处理时(代理)
/**是否支持多个手势同时处理@param gestureRecognizer      。。@param otherGestureRecognizer 。。@return return value bool*/-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{return YES;}
3.源代码的地址github地址
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息