iOS手势的实现方法

发布时间 - 2026-01-11 00:08:18    点击率:

本文实例为大家分享了iOS手势的具体实现代码,供大家参考,具体内容如下

效果

细节

1.UITouch

#import "ViewController_0.h"

@interface ViewController_0 ()

@property (nonatomic, strong)UILabel *label;

@end

@implementation ViewController_0

- (void)viewDidLoad {
  
  [super viewDidLoad];
  
  self.label          = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
  self.label.backgroundColor  = [UIColor yellowColor];
  self.label.layer.borderWidth = 1;
  [self.view addSubview:self.label];
  
  UILabel *textlabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 80, 200, 45)];
  textlabel.text   = @"拖动方块";
  [textlabel sizeToFit];
  textlabel.font   = [UIFont systemFontOfSize:12];
  [self.view addSubview:textlabel];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event {

  //1.拿到手势
  UITouch *touch = [touches anyObject];
  
  //2.拿到touch 所在view的坐标
  CGPoint point = [touch locationInView:self.view];
  
  //3.让label拿到坐标
  self.label.center = point;
  
  NSLog(@"1.手指接触到了屏幕");
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event {

  //1.拿到手势
  UITouch *touch = [touches anyObject];
  
  //2.拿到touch 所在view的坐标
  CGPoint point = [touch locationInView:self.view];
  
  //3.让label拿到坐标
  self.label.center = point;
  
  NSLog(@"2.手指在屏幕上移动");
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event {

   NSLog(@"3.手指刚离开屏幕");
}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event {

  NSLog(@"4.手势失效了");
}

@end

2.UITapGestureRecognizer

#import "ViewController_1.h"

@interface ViewController_1 ()

@property (nonatomic, strong) UILabel *label;

@end

@implementation ViewController_1

- (void)viewDidLoad {
  
  [super viewDidLoad];

  UILabel *textlabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 80, 200, 45)];
  textlabel.text   = @"电脑上操作tap手势 alt +shift 需要连续点击次数2次";
  [textlabel sizeToFit];
  textlabel.font   = [UIFont systemFontOfSize:12];
  [self.view addSubview:textlabel];
  
  self.label            = [[UILabel alloc] initWithFrame:CGRectMake(100, 150, 100, 100)];
  self.label.backgroundColor    = [UIColor orangeColor];
  self.label.userInteractionEnabled = YES;
  self.label.text          = @"0";
  self.label.textAlignment     = NSTextAlignmentCenter;
  [self.view addSubview:self.label];
  
  // 1.创建tap手势
  UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTap:)];
  tap.numberOfTapsRequired  = 2; //需要轻轻点击的次数
  tap.numberOfTouchesRequired = 2;//需要的手指数量 :2根手指alt +shift 需要匹配点击次数2次(其实直接用默认的就好)
  [self.label addGestureRecognizer:tap];
}

- (void)labelTap:(UITapGestureRecognizer *)tap {

  int num = [self.label.text intValue];
  num++;
  self.label.text = [NSString stringWithFormat:@"%d",num ];
}

@end

3.UILongPressGestureRecognizer

#import "ViewController_2.h"

@interface ViewController_2 () <UIGestureRecognizerDelegate>

@property (nonatomic, strong) UILabel *label;

@end

@implementation ViewController_2

- (void)viewDidLoad {
  
  [super viewDidLoad];
  
  UILabel *textlabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 80, 200, 45)];
  textlabel.text   = @"long长按手势";
  [textlabel sizeToFit];
  textlabel.font   = [UIFont systemFontOfSize:12];
  [self.view addSubview:textlabel];
  
  self.label            = [[UILabel alloc] initWithFrame:CGRectMake(40, 150, 200, 150)];
  self.label.backgroundColor    = [UIColor grayColor];
  self.label.userInteractionEnabled = YES;
  self.label.text          = @"0";
  self.label.textAlignment     = NSTextAlignmentCenter;
  [self.view addSubview:self.label];
  
  UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
  longPress.numberOfTapsRequired     = 1;
  longPress.numberOfTouchesRequired    = 1;
  longPress.minimumPressDuration     = 1.0;
  longPress.delegate  = self;
  [self.label addGestureRecognizer:longPress];
}

- (void)longPressAction:(UILongPressGestureRecognizer *)longPress {

  if(longPress.state == UIGestureRecognizerStateBegan) {
    
    NSLog(@"手势状态开始");
    
  } else if(longPress.state == UIGestureRecognizerStateEnded) {
    
    NSLog(@"手势状态结束");
  
  } else if(longPress.state == UIGestureRecognizerStateChanged) {
    
    NSLog(@"手势状态改变");
    
    NSInteger num = [self.label.text integerValue];
    num ++;
    self.label.text = [NSString stringWithFormat:@"%ld",(long)num];
  }
}

@end 

4.UISwipeGestureRecognizer

#import "ViewController_3.h"

@interface ViewController_3 ()

@property (nonatomic, strong) UILabel *label;

@end

@implementation ViewController_3

- (void)viewDidLoad {
  
  [super viewDidLoad];
 
  UILabel *textlabel   = [[UILabel alloc] initWithFrame:CGRectMake(10, 80, 200, 45)];
  textlabel.text     = @"swipe手势 向右滑动➕1,你也可以设置左划上划下划";
  textlabel.numberOfLines = 0;
  textlabel.font     = [UIFont systemFontOfSize:12];
  [self.view addSubview:textlabel];
  
  self.label            = [[UILabel alloc] initWithFrame:CGRectMake(100, 150, 100, 100)];
  self.label.backgroundColor    = [UIColor grayColor];
  self.label.userInteractionEnabled = YES;
  self.label.text          = @"0";
  self.label.textAlignment     = NSTextAlignmentCenter;
  [self.view addSubview:self.label];

  UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
  swipeGesture.direction         = UISwipeGestureRecognizerDirectionRight;//默认就是向右划
  [self.label addGestureRecognizer:swipeGesture];
}

-(void)swipeAction:(UISwipeGestureRecognizer *)swipe {

  if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
    
    NSLog(@"现在响应左划手势");
    
  } else if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
    
    NSLog(@"现在响应右划手势");
    
    NSInteger num  = [self.label.text integerValue];
    num ++;
    self.label.text = [NSString stringWithFormat:@"%ld",(long)num];
    
  } else if (swipe.direction == UISwipeGestureRecognizerDirectionUp) {
    
    NSLog(@"现在响应上划手势");
    
  } else if (swipe.direction == UISwipeGestureRecognizerDirectionDown) {
    
    NSLog(@"现在响应下划手势");
  }
}

@end

 5.UIPanGestureRecognizer

#import "ViewController_4.h"

@interface ViewController_4 ()

@property (nonatomic, strong) UILabel *label;

@end

@implementation ViewController_4

- (void)viewDidLoad {
  
  [super viewDidLoad];
  
  UILabel *textlabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 80, 200, 45)];
  textlabel.text   = @"pan手势,拖动方块";
  [textlabel sizeToFit];
  textlabel.font   = [UIFont systemFontOfSize:12];
  [self.view addSubview:textlabel];

  self.label            = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
  self.label.backgroundColor    = [UIColor grayColor];
  self.label.userInteractionEnabled = YES;
  self.label.text          = @"0";
  self.label.textAlignment     = NSTextAlignmentCenter;
  [self.view addSubview:self.label];

  UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
  [self.label addGestureRecognizer:pan];
}

- (void)panAction:(UIPanGestureRecognizer *)pan {
  
  CGPoint offset  = [pan locationInView:self.view];
  self.label.center = offset;
}

@end

6.UIRotationGestureRecognizer

#import "ViewController_5.h"

@interface ViewController_5 ()

@property (nonatomic, strong) UILabel *label;

@end

@implementation ViewController_5

- (void)viewDidLoad {
  
  [super viewDidLoad];
  
  UILabel *textlabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 80, 200, 45)];
  textlabel.text   = @"模拟器测试旋转手势时,按住 option键,再用触摸板或鼠标操作";
  textlabel.font   = [UIFont systemFontOfSize:12];
  textlabel.numberOfLines = 0;
  [self.view addSubview:textlabel];

  self.label            = [[UILabel alloc] initWithFrame:CGRectMake(40, 200, 200, 50)];
  self.label.backgroundColor    = [UIColor grayColor];
  self.label.userInteractionEnabled = YES;
  self.label.text          = @"we are friends";
  self.label.textAlignment     = NSTextAlignmentCenter;
  [self.view addSubview:self.label];
  
  //(模拟器测试捏合和旋转手势时,按住 option 键,再用触摸板或鼠标操作)
  UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
  [self.view addGestureRecognizer:rotation];

}

- (void)rotationAction:(UIRotationGestureRecognizer *)rotation {
  
  self.label.transform = CGAffineTransformRotate(self.label.transform, rotation.rotation);
  rotation.rotation = 0; // 这个很重要!!!!!

//  static float orginState;
//  
//  self.label.transform = CGAffineTransformMakeRotation(rotation.rotation + orginState);
//  
//  if (rotation.state == UIGestureRecognizerStateEnded) {
//    
//    orginState = orginState + rotation.state;
//    self.label.transform = CGAffineTransformMakeRotation(rotation.rotation);
//  }
}

@end

7.UIPinchGestureRecognizer

#import "ViewController_6.h"

@interface ViewController_6 ()

@property (nonatomic, strong) UILabel *label;

@end

@implementation ViewController_6

- (void)viewDidLoad {
  
  [super viewDidLoad];
 
  UILabel *textlabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 80, 200, 45)];
  textlabel.text   = @"模拟器测试捏合手势时,按住 option键,再用触摸板或鼠标操作";
  textlabel.font   = [UIFont systemFontOfSize:12];
  textlabel.numberOfLines = 0;
  [self.view addSubview:textlabel];
  
  self.label            = [[UILabel alloc] initWithFrame:CGRectMake(100, 250, 80, 80)];
  self.label.backgroundColor    = [UIColor grayColor];
  self.label.userInteractionEnabled = YES;
  self.label.text          = @"0";
  self.label.textAlignment     = NSTextAlignmentCenter;
  [self.view addSubview:self.label];
  
//  (模拟器测试捏合和旋转手势时,按住 option 键,再用触摸板或鼠标操作)
  UIPinchGestureRecognizer * pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pichGesture:)];
  [self.view addGestureRecognizer:pinch];
}

- (void)pichGesture:(UIPinchGestureRecognizer *)pinch {
  
  static float originScale = 1;
  
  //手势缩放返回的scale 是相对于上一次的
  self.label.transform = CGAffineTransformMakeScale(pinch.scale * originScale , pinch.scale*originScale);
  
  if (pinch.state == UIGestureRecognizerStateEnded) {
    
    originScale = originScale * pinch.scale;
  }
}

@end

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


# iOS  # 手势  # IOS中各种手势操作实例代码  # iOS 用Swipe手势和动画实现循环播放图片示例  # iOS手势密码的实现方法  # IOS 七种手势操作(拖动、捏合、旋转、点按、长按、轻扫、自定义)详解及实例代码  # IOS中的七种手势小结  # iOS轻点、触摸和手势代码开发  # 使用Swift代码实现iOS手势解锁、指纹解锁实例详解  # 基于JS实现Android  # iOS一个手势动画效果  # IOS 单击手势的添加实现代码  # 鼠标  # 再用  # 触摸板  # 拖动  # 就好  # 很重要  # 相对于  # 大家分享  # 你也可以  # 具体内容  # 大家多多  # 屏幕上  # NSSet  # lt  # touchesBegan  # nullable  # UIEvent  # withEvent  # gt  # touches 


相关栏目: 【 网站优化151355 】 【 网络推广146373 】 【 网络技术251813 】 【 AI营销90571


相关推荐: Laravel Debugbar怎么安装_Laravel调试工具栏配置指南  Laravel怎么在Blade中安全地输出原始HTML内容  Python自然语言搜索引擎项目教程_倒排索引查询优化案例  Laravel如何生成PDF或Excel文件_Laravel文档导出工具与使用教程  简单实现jsp分页  百度浏览器网页无法复制文字怎么办 百度浏览器复制修复  网站视频制作书签怎么做,ie浏览器怎么将网站固定在书签工具栏?  品牌网站制作公司有哪些,买正品品牌一般去哪个网站买?  Linux系统命令中screen命令详解  绝密ChatGPT指令:手把手教你生成HR无法拒绝的求职信  进行网站优化必须要坚持的四大原则  Laravel如何处理文件下载请求?(Response示例)  北京网页设计制作网站有哪些,继续教育自动播放怎么设置?  魔方云NAT建站如何实现端口转发?  IOS倒计时设置UIButton标题title的抖动问题  如何使用 jQuery 正确渲染 Instagram 风格的标签列表  Laravel API路由如何设计_Laravel构建RESTful API的路由最佳实践  Laravel路由Route怎么设置_Laravel基础路由定义与参数传递规则【详解】  香港服务器建站指南:免备案优势与SEO优化技巧全解析  深圳网站制作平台,深圳市做网站好的公司有哪些?  Laravel模型关联查询教程_Laravel Eloquent一对多关联写法  Laravel Artisan命令怎么自定义_创建自己的Laravel命令行工具完全指南  音响网站制作视频教程,隆霸音响官方网站?  Edge浏览器如何截图和滚动截图_微软Edge网页捕获功能使用教程【技巧】  如何安全更换建站之星模板并保留数据?  如何快速生成可下载的建站源码工具?  谷歌浏览器如何更改浏览器主题 Google Chrome主题设置教程  Python文本处理实践_日志清洗解析【指导】  网站制作壁纸教程视频,电脑壁纸网站?  独立制作一个网站多少钱,建立网站需要花多少钱?  如何在阿里云购买域名并搭建网站?  在Oracle关闭情况下如何修改spfile的参数  Laravel如何发送系统通知_Laravel Notifications实现多渠道消息通知  如何选择PHP开源工具快速搭建网站?  Laravel如何集成Inertia.js与Vue/React?(安装配置)  Laravel Admin后台管理框架推荐_Laravel快速开发后台工具  Laravel如何集成第三方登录_Laravel Socialite实现微信QQ微博登录  Laravel Pest测试框架怎么用_从PHPUnit转向Pest的Laravel测试教程  Laravel如何创建自定义Artisan命令?(代码示例)  javascript中的try catch异常捕获机制用法分析  Laravel如何连接多个数据库_Laravel多数据库连接配置与切换教程  如何快速查询网站的真实建站时间?  如何快速搭建高效简练网站?  Python文件流缓冲机制_IO性能解析【教程】  nginx修改上传文件大小限制的方法  Python高阶函数应用_函数作为参数说明【指导】  Laravel如何部署到服务器_线上部署Laravel项目的完整流程与步骤  Win11搜索栏无法输入_解决Win11开始菜单搜索没反应问题【技巧】  Javascript中的事件循环是如何工作的_如何利用Javascript事件循环优化异步代码?  Swift开发中switch语句值绑定模式