UI - 手势

UIGestureRecognizer手势识别器

手势:有规律的触摸

UIGestureRecognizer抽象类:七种手势:轻拍(tap)长按(longPress)旋转(rotation)捏合(pinch)拖拽(pan)轻扫(swipe)屏幕边缘拖拽(screenEdgePan)

首先:添加图片 以便于观察手势

添加图片的步骤

UIImageView *imgV = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,200,200)];

imgV.center = self.view.center;

imgV.image = [UIImage imageNamed:@"blank"];//blank是图片的名称

self.view addSubview:imgV];

[imgV release];

//打开用户交互

imgV.userInteractionEnable = YES;

轻拍tap

创建对象

获取到轻拍手势 让 self调用tapAction:方法

UITapGestureREcognizer *tap = [[UITapGestureREcognizer alloc] initWithTarget:self action:@selector(tapAction:)];

添加手势

imgV addGestureRecognizer:tap];

内存管理

tap release];

点击次数

tap.numberOfTapsRequired = 2;

手指个数

tap.numberOfTouchsRequired = 2;

长按longPress

UILongPressGestureRecognizer *longPrss = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];

[imgV addGestureREcognizer:longPress];

[longPress release];

长按时间

longPress.minimumPressDuration = 1;


旋转rotation

UIRatationGestureRecognizer *rotation = [UIRatationGestureRecognizer alloc]initWithTarget:self action@selector(rotationAction:)];

[imgV addGestureREcognizer:rotation];

[rotation release];


捏合pinch

UIPinchGestureRecognizer *pinch = [UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];

[imgV addGestureREcognizer:pinch];

[pinch release];

拖拽pan

UIPanGestureRecognizer*pan = [[UIPanGestureRecognizeralloc]initWithTarget:selfaction:@selector(panAction:)];

[imgViewaddGestureRecognizer:pan];

[panrelease];

轻扫swipe

UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:selfaction:@selector(swipeAction:)];

默认只识别向右

设置方向时最多只能设置水平(左/右)或者垂直(上/下)

swipe.direction = UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft;//(按位或)

[imgView addGestureRecognizer:swipe];

[swipe release];

屏幕边缘拖拽screenEdgePan

UIScreenEdgePanGestureRecognizer*sep = [[UIScreenEdgePanGestureRecognizeralloc]initWithTarget:selfaction:@selector(sepAction:)];

需要设置拖拽的边缘

sep.edges=UIRectEdgeLeft;

一般这个手势添加在VC的view上

[self.viewaddGestureRecognizer:sep];

[seprelease];

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • UIGestureRecognizer手势识别器 手势:有规律的触摸 UIGestureRecognizer抽象类...
    dliys阅读 403评论 0 1
  • //七中手势:轻拍(tap) 长按(longPress) 旋转(rotation) 捏合(pinch) 拖拽(p...
    画个完美句号阅读 269评论 1 1
  • //UIGestureRecognizer 手势识别器 //七中手势:轻拍(tap) 长按(longPress)...
    肉肉要次肉阅读 215评论 0 1
  • 七种手势:轻拍(tap)、长按(longPress)、旋转(rotation)、捏合(pinch)、拖拽(pan)...
    青花_阅读 423评论 0 0
  • - (void)viewDidLoad { [super viewDidLoad]; // Do any addi...
    焦六金Jxx阅读 342评论 0 0