UIGestureRecognizer 手势识别器,是常用手势的父类
可以手写代码,也可以拖拽应用手势。
1.触摸Touch
四个方法:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { _label1.text = @"触摸开始"; //1.获得触摸屏幕的手指 UITouch *touch = [touches anyObject]; //2.得到当前手指所在的位置的坐标 _startPoint = [touch locationInView:self.view]; //3.显示坐标 _label1.text = [NSString stringWithFormat:@"开始:x=%.1f,y=%.1f",_startPoint.x,_startPoint.y]; //5.获得触摸屏幕次数 int tapCount = touch.tapCount; //6.获得点击手指数目 int fingerCount = touches.count; _label4.text = [NSString stringWithFormat:@"触摸次数:%i,手指数:%i",tapCount,fingerCount]; } -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { //只要有移动就进入此方法 _label2.text = @"触摸move"; //1.获得触摸屏幕的手指 UITouch *touch = [touches anyObject]; //2.得到当前手指所在的位置的坐标 CGPoint point = [touch locationInView:self.view]; //3.显示坐标 _label2.text = [NSString stringWithFormat:@"x=%.1f,y=%.1f",point.x,point.y]; } -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { _label3.text = @"触摸end"; //1.获得触摸屏幕的手指 UITouch *touch = [touches anyObject]; //2.得到当前手指所在的位置的坐标 _endPoint = [touch locationInView:self.view]; //3.显示坐标 _label3.text = [NSString stringWithFormat:@"x=%.1f,y=%.1f",_endPoint.x,_endPoint.y]; //4.计算位移 fabsf()计算绝对值 float distancesX = fabsf(_endPoint.x - _startPoint.x); float distancesY = fabsf(_endPoint.y - _startPoint.y); _label4.text = [NSString stringWithFormat:@"结束:x方向位移:%.1f,y方向位移:%.1f",distancesX,distancesY]; //7.判断最后的点是否在图片里面 if (CGRectContainsPoint(_imageView.frame, _endPoint)) { _label4.text = @"抬起时在图片里面"; }else{ _label4.text = @"抬起时不在图片里面"; } //默认接受单点触碰事件,多点触碰需要开启 self.view.multipleTouchEnabled = YES; } -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { _label.text = @"触摸cancel"; }
2.清扫
//1.创建轻扫手势对象,为它添加执行手势时候的方法 UISwipeGestureRecognizer *swipeR = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRightAction:)]; //2.设定轻扫手势的方向 /* UISwipeGestureRecognizerDirectionRight = 1 << 0, UISwipeGestureRecognizerDirectionLeft = 1 << 1, UISwipeGestureRecognizerDirectionUp = 1 << 2, UISwipeGestureRecognizerDirectionDown = 1 << 3 */ swipeR.direction = UISwipeGestureRecognizerDirectionRight; //3.将手势添加到视图上 [self.view addGestureRecognizer:swipeR]; //不能为一个手势添加多个轻扫方向!!! //向左清扫 UISwipeGestureRecognizer *swipeL = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeftAction:)]; swipeL.direction = UISwipeGestureRecognizerDirectionLeft; [self.view addGestureRecognizer:swipeL]; //向上清扫 UISwipeGestureRecognizer *swipeU = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeUpAction:)]; swipeU.direction = UISwipeGestureRecognizerDirectionUp; [self.view addGestureRecognizer:swipeU]; //向下清扫 UISwipeGestureRecognizer *swipeD = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeDownAction:)]; swipeD.direction = UISwipeGestureRecognizerDirectionDown; [self.view addGestureRecognizer:swipeD];
3.点击
//1.创建单击手势 UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleAction:)]; //2.1设置点击的手指数 [singleTap setNumberOfTouchesRequired:1]; //2.2设置点击次数 [singleTap setNumberOfTapsRequired:1]; //3.手指加到视图上 [self.view addGestureRecognizer:singleTap]; //一个手指双击 UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleAction:)]; [doubleTap setNumberOfTouchesRequired:1]; [doubleTap setNumberOfTapsRequired:2]; [self.view addGestureRecognizer:doubleTap]; //双击的时候可以响应双击时间,但是同时也被看成是两次单击事件,所以需要设定手势的优先级 [singleTap requireGestureRecognizerToFail:doubleTap]; //单击优先级低于双击的优先级别 //两个手指的双击事件 UITapGestureRecognizer *doubleTapEve = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapEveAction:)]; [doubleTapEve setNumberOfTouchesRequired:2]; [doubleTapEve setNumberOfTapsRequired:2]; [self.view addGestureRecognizer:doubleTapEve]; //1个手指的三击事件 UITapGestureRecognizer *tripleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tripleTapAction:)]; [tripleTap setNumberOfTouchesRequired:1]; [tripleTap setNumberOfTapsRequired:3]; [self.view addGestureRecognizer:tripleTap]; [doubleTap requireGestureRecognizerToFail:tripleTap];
4.长按
// 1.创建长按的手势 UILongPressGestureRecognizer *press = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(pressAction:)]; //2.1设置长按的时间 press.minimumPressDuration = 1; //2.2设置手指数量 [press setNumberOfTouchesRequired:1]; //3.把手势加到视图上 //如果加到UILabel、UIImageView等上面,需要开启用户交互才能使用 _label.userInteractionEnabled = YES; [_label addGestureRecognizer:press]; // [self.view addGestureRecognizer:press];
5.捏合
- (void)viewDidLoad { [super viewDidLoad]; //1.创建捏合手势(默认捏合手指数为2) UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)]; //2.加到视图上 [_imageView addGestureRecognizer:pinch]; _imageView.userInteractionEnabled = YES; } -(void)pinchAction:(UIPinchGestureRecognizer*)pinch{ _imageView.transform = CGAffineTransformMakeScale(pinch.scale, pinch.scale); if (pinch.scale == UIGestureRecognizerStateEnded) { _imageView.transform = CGAffineTransformMakeScale(1, 1); } }
6.旋转
//1.创建旋转手势 UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)]; //2.添加手势 _imageView.userInteractionEnabled = YES; [_imageView addGestureRecognizer:rotation]; } -(void)rotationAction:(UIRotationGestureRecognizer*)rotation{ _imageView.transform = CGAffineTransformMakeRotation(rotation.rotation); }
7.拖拽
- (void)viewDidLoad { [super viewDidLoad]; //1.创建拖拽的手势 UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)]; //2.把手势加到视图上 [_imageView addGestureRecognizer:pan]; _imageView.userInteractionEnabled = YES; } -(void)panAction:(UIPanGestureRecognizer*)pan{ //1.取到图片中点坐标 CGPoint centerPoint = _imageView.center; //2.获取拖拽手势相对于当前视图的偏移位置 CGPoint point = [pan translationInView:self.view]; //3.计算图片新的坐标 _imageView.center = CGPointMake(centerPoint.x+point.x, centerPoint.y+point.y); //4.每拖拽一点,就将translation设置为CGPointZero,否则每次拖拽都会在原有基础上累加 if (pan.state == UIGestureRecognizerStateChanged) { [pan setTranslation:CGPointZero inView:self.view]; } }
时间: 2024-09-29 09:50:15