热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

iOS如何实现手势

这篇文章主要为大家展示了“iOS如何实现手势”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“iOS

这篇文章主要为大家展示了“iOS如何实现手势”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“iOS如何实现手势”这篇文章吧。

具体内容如下

效果

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 *)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 *)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 *)touches withEvent:(nullable UIEvent *)event {

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

- (void)touchesCancelled:(NSSet *)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 () 

@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 开发技巧:TabBarController 自定义与本地通知设置
    本文介绍了如何在 iOS 中自定义 TabBarController 的背景颜色和选中项的颜色,以及如何使用本地通知设置应用程序图标上的提醒个数。通过这些技巧,可以提升应用的用户体验。 ... [详细]
  • 本文介绍了如何在iOS应用中自定义导航栏按钮,包括使用普通按钮和图片生成导航条专用按钮的方法。同时,探讨了在不同版本的iOS系统中实现多按钮布局的技术方案。 ... [详细]
  • IneedtofocusTextCellsonebyoneviaabuttonclick.ItriedlistView.ScrollTo.我需要通过点击按钮逐个关注Tex ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • 从 .NET 转 Java 的自学之路:IO 流基础篇
    本文详细介绍了 Java 中的 IO 流,包括字节流和字符流的基本概念及其操作方式。探讨了如何处理不同类型的文件数据,并结合编码机制确保字符数据的正确读写。同时,文中还涵盖了装饰设计模式的应用,以及多种常见的 IO 操作实例。 ... [详细]
  • Kubernetes 持久化存储与数据卷详解
    本文深入探讨 Kubernetes 中持久化存储的使用场景、PV/PVC/StorageClass 的基本操作及其实现原理,旨在帮助读者理解如何高效管理容器化应用的数据持久化需求。 ... [详细]
  • 本文介绍如何使用 Android 的 Canvas 和 View 组件创建一个简单的绘图板应用程序,支持触摸绘画和保存图片功能。 ... [详细]
  • 本文详细介绍了Grand Central Dispatch (GCD) 的核心概念和使用方法,探讨了任务队列、同步与异步执行以及常见的死锁问题。通过具体示例和代码片段,帮助开发者更好地理解和应用GCD进行多线程开发。 ... [详细]
  • 深入解析SpringMVC核心组件:DispatcherServlet的工作原理
    本文详细探讨了SpringMVC的核心组件——DispatcherServlet的运作机制,旨在帮助有一定Java和Spring基础的开发人员理解HTTP请求是如何被映射到Controller并执行的。文章将解答以下问题:1. HTTP请求如何映射到Controller;2. Controller是如何被执行的。 ... [详细]
  • 探讨了在 Spring MVC 框架下,JSP 页面使用 标签时遇到的数据无法正确显示的问题,并提供了可能的原因和解决方案。 ... [详细]
  • 深入理解 SQL 视图、存储过程与事务
    本文详细介绍了SQL中的视图、存储过程和事务的概念及应用。视图为用户提供了一种灵活的数据查询方式,存储过程则封装了复杂的SQL逻辑,而事务确保了数据库操作的完整性和一致性。 ... [详细]
  • 在使用 DataGridView 时,如果在当前单元格中输入内容但光标未移开,点击保存按钮后,输入的内容可能无法保存。只有当光标离开单元格后,才能成功保存数据。本文将探讨如何通过调用 DataGridView 的内置方法解决此问题。 ... [详细]
  • 2023年京东Android面试真题解析与经验分享
    本文由一位拥有6年Android开发经验的工程师撰写,详细解析了京东面试中常见的技术问题。涵盖引用传递、Handler机制、ListView优化、多线程控制及ANR处理等核心知识点。 ... [详细]
  • 本文深入探讨了C++对象模型中的一些细节问题,特别是虚拟继承和析构函数的处理。通过具体代码示例和详细分析,揭示了书中某些观点的不足之处,并提供了更合理的解释。 ... [详细]
  • 本文将详细探讨Linux pinctrl子系统的各个关键数据结构,帮助读者深入了解其内部机制。通过分析这些数据结构及其相互关系,我们将进一步理解pinctrl子系统的工作原理和设计思路。 ... [详细]
author-avatar
Jin_木_木_176
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有