iOS之 手势识别GestureRecognizer

来源:互联网 发布:vb调用bartender打印 编辑:程序博客网 时间:2024/05/27 20:22

总共有六种手势识别:

  • 轻击手势(TapGestureRecognizer),
  • 轻扫手势 (SwipeGestureRecognizer),
  • 长按手势(LongPressGestureRecognizer),
  • 拖动手势(PanGestureRecognizer),
  • 捏合手势(PinchGestureRecognizer),
  • 旋转手势(RotationGestureRecognizer);

1,轻击手势(TapGestureRecognizer)

//新建tap手势UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];//设置点击次数和点击手指数tapGesture.numberOfTapsRequired = 1; //点击次数tapGesture.numberOfTouchesRequired = 1; //点击手指数[self.view addGestureRecognizer:tapGesture];//轻击手势触发方法-(void)tapGesture:(id)sender{//轻击后要做的事情}

2,长按手势(LongPressGestureRecognizer)

//添加长摁手势UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesture:)];//设置长按时间longPressGesture.minimumPressDuration = 0.5; //(2秒)[self.view addGestureRecognizer:longPressGesture];//常摁手势触发方法-(void)longPressGesture:(id)sender{UILongPressGestureRecognizer *longPress = sender;if (longPress.state == UIGestureRecognizerStateBegan){UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"长按触发" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles: nil];[alter show];}}

说明:手势的常用状态如下

开始:UIGestureRecognizerStateBegan
改变:UIGestureRecognizerStateChanged
结束:UIGestureRecognizerStateEnded
取消:UIGestureRecognizerStateCancelled
失败:UIGestureRecognizerStateFailed

3,轻扫手势(SwipeGestureRecognizer)

//添加轻扫手势UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];//设置轻扫的方向swipeGesture.direction = UISwipeGestureRecognizerDirectionRight; //默认向右[self.view addGestureRecognizer:swipeGesture];//添加轻扫手势UISwipeGestureRecognizer *swipeGestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];//设置轻扫的方向swipeGestureLeft.direction = UISwipeGestureRecognizerDirectionLeft; //默认向右[self.view addGestureRecognizer:swipeGestureLeft];//轻扫手势触发方法-(void)swipeGesture:(id)sender{UISwipeGestureRecognizer *swipe = sender;if (swipe.direction == UISwipeGestureRecognizerDirectionLeft){//向左轻扫做的事情}if (swipe.direction == UISwipeGestureRecognizerDirectionRight){//向右轻扫做的事情}}

4,捏合手势(PinchGestureRecognizer)

//添加捏合手势UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGesture:)];[self.view addGestureRecognizer:pinchGesture];////捏合手势触发方法-(void) pinchGesture:(id)sender{UIPinchGestureRecognizer *gesture = sender;//手势改变时if (gesture.state == UIGestureRecognizerStateChanged){//捏合手势中scale属性记录的缩放比例_imageView.transform = CGAffineTransformMakeScale(gesture.scale, gesture.scale);}//结束后恢复if(gesture.state==UIGestureRecognizerStateEnded){[UIView animateWithDuration:0.5 animations:^{_imageView.transform = CGAffineTransformIdentity;//取消一切形变}];}}

5,拖动手势(PanGestureRecognizer)

//添加拖动手势UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];[self.view addGestureRecognizer:panGesture];//拖动手势-(void) panGesture:(id)sender{UIPanGestureRecognizer *panGesture = sender;CGPoint movePoint = [panGesture translationInView:self.view];//做你想做的事儿}

6,旋转手势(RotationGestureRecognizer)

//添加旋转手势UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGesture:)];[self.view addGestureRecognizer:rotationGesture];//旋转手势-(void)rotationGesture:(id)sender{UIRotationGestureRecognizer *gesture = sender;if (gesture.state==UIGestureRecognizerStateChanged){_imageView.transform=CGAffineTransformMakeRotation(gesture.rotation);}if(gesture.state==UIGestureRecognizerStateEnded){[UIView animateWithDuration:1 animations:^{_imageView.transform=CGAffineTransformIdentity;//取消形变}];}}
0 0
原创粉丝点击