一.UITouch
1 //任何视图都可以触发此方法 2 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ 3 NSLog(@"视图被触摸了"); 4 } 5 6 - (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ 7 NSLog(@"因意外停止了触摸"); 8 } 9 10 - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ 11 NSLog(@"结束触摸"); 12 } 13 14 //可以获得手指在视图移动的位置 15 - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ 16 // CGFloat floct = [touches anyObject]; 17 //手指触摸的点 18 NSLog(@"%@",[touches anyObject]); 19 //移动到的所有的点 20 // NSLog(@"%@",[touches allObjects]); 21 }
二.UITapGestureRecognizer 点击手势
创建一个view 创建手势 使用addGestureRecognizer:方法 将手势添加到view上
1 UIView * view = [[UIView alloc]initWithFrame:CGRectMake(20, 100, 200, 200)]; 2 view.backgroundColor = [UIColor redColor]; 3 4 //创建一个点击手势 5 UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClick:)]; 6 //设置点击次数 7 tap.numberOfTapsRequired = 1; 8 //把手势添加到控件上 9 [view addGestureRecognizer:tap]; 10 11 UITapGestureRecognizer * tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClicked:)]; 12 tap1.numberOfTapsRequired = 2; 13 [view addGestureRecognizer:tap1]; 14 15 16 17 [self.view addSubview:view];
三.UILongPressGestureRecognizer 长按手势
1 UIView * view = [[UIView alloc]initWithFrame:CGRectMake(20, 100, 200, 200)]; 2 view.backgroundColor = [UIColor redColor]; 3 4 //创建一个长按手势 5 UILongPressGestureRecognizer * longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(onClick:)]; 6 7 //设置长按最短时间 8 longPress.minimumPressDuration = 1; 9 //设置长按手指数量 10 longPress.numberOfTouchesRequired = 2; 11 12 //把手势添加到控件上 13 [view addGestureRecognizer:longPress];
四.UISwipeGestureRecognizer 滑动手势
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(50, 100, 100, 30)]; label.textColor = [UIColor grayColor]; label.text = @"滑动手势"; //创建滑动手势 UISwipeGestureRecognizer * swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(onClick:)]; //添加到控件上 [label addGestureRecognizer:swipe]; //打开label的交互 默认为NO 是NO的时候 手势不能被响应 label.userInteractionEnabled = YES; //设置滑动方向 每一个手势只能是一个方向 //如果需要多方向 需要多次创建 swipe.direction = UISwipeGestureRecognizerDirectionDown; /* typedef NS_OPTIONS(NSUInteger, UISwipeGestureRecognizerDirection) { UISwipeGestureRecognizerDirectionRight = 1 << 0, UISwipeGestureRecognizerDirectionLeft = 1 << 1, UISwipeGestureRecognizerDirectionUp = 1 << 2, UISwipeGestureRecognizerDirectionDown = 1 << 3 }; */ //设置滑动手指数量 swipe.numberOfTouchesRequired = 1; [self.view addSubview:label];
五.UIPanGestureRecognizer 拖动手势
1 - (void)createLabel{ 2 UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(50, 100, 100, 30)]; 3 label.textColor = [UIColor groupTableViewBackgroundColor]; 4 label.text = @"拖动手势"; 5 label.tag = 1; 6 //创建拖动手势 7 UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(onClick:)]; 8 //添加到控件上 9 [label addGestureRecognizer:pan]; 10 11 //打开label的交互 默认为NO 是NO的时候 手势不能被响应 12 label.userInteractionEnabled = YES; 13 14 15 16 [self.view addSubview:label]; 17 } 18 19 - (void)onClick:(UIPanGestureRecognizer *)pan{ 20 static CGPoint offPoint; 21 22 UILabel * label = [self.view viewWithTag:1]; 23 //获得手势的偏移量 24 CGPoint point = [pan translationInView:self.view]; 25 26 if (pan.state == UIGestureRecognizerStateBegan) { 27 offPoint = label.center; 28 return; 29 } 30 31 label.center = CGPointMake(point.x+offPoint.x, point.y+offPoint.y); 32 }
六.UIRotationGestureRecognizer 旋转手势
1 - (void)createLabel{ 2 UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(50, 100, 200, 60)]; 3 label.textColor = [UIColor groupTableViewBackgroundColor]; 4 label.text = @"旋转手势"; 5 label.tag = 1; 6 label.textAlignment = NSTextAlignmentCenter; 7 label.adjustsFontSizeToFitWidth = YES; 8 9 label.font = [UIFont systemFontOfSize:60]; 10 //创建旋转手势 11 UIRotationGestureRecognizer * rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(onClick:)]; 12 //添加到控件上 13 [label addGestureRecognizer:rotation]; 14 15 //打开label的交互 默认为NO 是NO的时候 手势不能被响应 16 label.userInteractionEnabled = YES; 17 [self.view addSubview:label]; 18 } 19 20 - (void)onClick:(UIRotationGestureRecognizer *)rotation{ 21 UILabel * label = [self.view viewWithTag:1]; 22 23 static CGFloat offFloat; 24 25 label.transform = CGAffineTransformMakeRotation(rotation.rotation +offFloat); 26 //结束时把上次旋转角度的记录下来 27 if (rotation.state == UIGestureRecognizerStateEnded) { 28 offFloat = offFloat + rotation.rotation; 29 } 30 }
七.UIPinchGestureRecognizer 捏合手势
1 - (void)createLabel{ 2 UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(50, 100, 200, 60)]; 3 label.textColor = [UIColor groupTableViewBackgroundColor]; 4 label.text = @"捏合手势"; 5 label.tag = 1; 6 label.textAlignment = NSTextAlignmentCenter; 7 label.adjustsFontSizeToFitWidth = YES; 8 9 label.font = [UIFont systemFontOfSize:60]; 10 //创建捏合手势 11 UIPinchGestureRecognizer * pinchGR = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(onClick:)]; 12 //添加到控件上 13 [label addGestureRecognizer:pinchGR]; 14 15 //打开label的交互 默认为NO 是NO的时候 手势不能被响应 16 label.userInteractionEnabled = YES; 17 [self.view addSubview:label]; 18 } 19 20 - (void)onClick:(UIPinchGestureRecognizer *)pinchGR{ 21 UILabel * label = [self.view viewWithTag:1]; 22 static CGFloat offFloat = 1; 23 label.transform = CGAffineTransformMakeScale(pinchGR.scale*offFloat, pinchGR.scale*offFloat); 24 //记录结束手势时的缩放比例 25 if (pinchGR.state == UIGestureRecognizerStateEnded) { 26 offFloat = offFloat* pinchGR.scale; 27 } 28 }
时间: 2024-10-07 05:30:29