首页 > 代码库 > UIGestureRecognizer
UIGestureRecognizer
UIGestureRecognizer
#pragma mark -拖拽
- (void)addPan{
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizeralloc]initWithTarget:selfaction:@selector(pan:)];
[_imagViewaddGestureRecognizer:pan];
}
- (void)pan:(UIPanGestureRecognizer*)pan
{
// 获取手指移动的位置
CGPoint trans = [pantranslationInView:_imagView];
_imagView.transform= CGAffineTransformTranslate(_imagView.transform, trans.x, trans.y);
// 复位
[pan setTranslation:CGPointZeroinView:_imagView];
}
#pragma mark -放大、缩小
- (void)addPinch
{
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizeralloc]initWithTarget:selfaction:@selector(pinch:)];
// 设置代理的原因:想要同时支持多个手势
pinch.delegate= self;
[_imagViewaddGestureRecognizer:pinch];
}
- (void)pinch:(UIPinchGestureRecognizer*)pinch
{
_imagView.transform= CGAffineTransformScale(_imagView.transform, pinch.scale, pinch.scale);
// 复位
pinch.scale= 1;
}
#pragma mark -旋转
- (void)addRotation
{
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizeralloc]initWithTarget:selfaction:@selector(rotation:)];
rotation.delegate= self;
[_imagViewaddGestureRecognizer:rotation];
}
- (void)rotation:(UIRotationGestureRecognizer*)rotation
{
_imagView.transform= CGAffineTransformRotate(_imagView.transform, rotation.rotation);
// 复位
rotation.rotation= 0;
}
#pragma mark -轻扫
- (void)addSwipe
{
// 一个手势只能识别一个方向
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(swipe:)];
swipe.direction= UISwipeGestureRecognizerDirectionRight;
[_imagViewaddGestureRecognizer:swipe];
}
- (void)swipe:(UISwipeGestureRecognizer*)swipe
{
NSLog(@"swipe");
}
#pragma mark -长按
- (void)addLongPress
{
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizeralloc]initWithTarget:selfaction:@selector(longPress:)];
[_imagViewaddGestureRecognizer:longPress];
}
- (void)longPress:(UILongPressGestureRecognizer*)longPress
{
// 根据状态执行事件
if (longPress.state== UIGestureRecognizerStateBegan) {
NSLog(@"longPress");
}
}
#pragma mark -点击
- (void)addTap
{
UITapGestureRecognizer *tap = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(tap:)];
// 点按多少次才能触发手势
tap.numberOfTapsRequired= 2;
// 必须多少个手指触摸才能触发手势
// tap.numberOfTouchesRequired = 2;
tap.delegate= self;
[_imagViewaddGestureRecognizer:tap];
}
{
UITapGestureRecognizer *tap = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(tap:)];
// 点按多少次才能触发手势
tap.numberOfTapsRequired= 2;
// 必须多少个手指触摸才能触发手势
// tap.numberOfTouchesRequired = 2;
tap.delegate= self;
[_imagViewaddGestureRecognizer:tap];
}
- (void)tap:(UITapGestureRecognizer*)tap
{
NSLog(@"tap");
}
{
NSLog(@"tap");
}
注意:
1.手势的触摸要取消Use Auto Layout。
2.在UISwipeGestureRecognizer(轻扫)手势中,一个手势只能识别一个方向。
3.如果想要支持多个手势,要遵守UIGestureRecognizerDelegate协议,实现下面这个方法,就可以支持多手势操作。
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
{
return YES;
}
4.其中UIImageView要实现userInteractionEnabled。
UIGestureRecognizer
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。