Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >【iOS学习】——手势识别

【iOS学习】——手势识别

作者头像
LeeCen
发布于 2018-10-11 08:44:24
发布于 2018-10-11 08:44:24
1.5K00
代码可运行
举报
文章被收录于专栏:LeeCenLeeCen
运行总次数:0
代码可运行

iOS 手势

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    1.如果一个控件继承于 UIControl,那么它将不需要手势
    2.所有控件都可以添加手势
    [控件 addGestureRecognizer: ]
    3.iOS 系统提供的手势有哪些
 
    UITapGestureRecognizer 点击
    UISwipeGestureRecognizer 轻扫
    UIPanGestureRecognizer 拖动
    UIRotationGestureRecognizer 旋转
    UIPinchGestureRecognizer 捏合
    UILongPressGestureRecognizer 长按
 
    4.iOS 自定义的手势都是具有相同的父类  UIGestureRecognizer
    5.UIGestureRecognizer 这个手势父类是如何封装?
    
    UIGestureRecognizer  这个父类可以看做一个抽象类,并不具备手势的具体功能,但是它提供了子类共有的初始化方法、属性、代理
    <共有的初始化方法>
    - (instancetype)initWithTarget:(nullable id)target action:(nullable SEL)action 
 
    <2>手势的状态  state
    <3>手势是否有效 enable
    <4>代理 delegate
    <5>被添加手势的 view

手势.gif

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
/** 图片 */
@property (nonatomic,strong) UIImageView *imageView;
/** 数组 */
@property (nonatomic,strong) NSArray *images;
/** 图片张数 */
@property int count;
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
     _count = 0;
     _images = @[[UIImage imageNamed:@"00"],[UIImage imageNamed:@"01"],[UIImage imageNamed:@"02"]];
     _imageView = [[UIImageView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width / 2 - 50, [UIScreen mainScreen].bounds.size.height / 2 -100, 100, 200)];
     _imageView.image = _images[_count];
     _imageView.contentMode = UIViewContentModeScaleAspectFit;
     [self.view addSubview:_imageView];
  • 手势需要开启用户交互
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    //手势需要开启用户交互
    _imageView.userInteractionEnabled = YES;

点击手势

  • 单击手势
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    //单击手势
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapAction:)];
    [_imageView addGestureRecognizer:singleTap];
  • 双击手势 添加 numberOfTapsRequired属性 能判断点击次数
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapAction:)];
    //判断点击次数
    doubleTap.numberOfTapsRequired = 2;
    [singleTap requireGestureRecognizerToFail:doubleTap];
    [_imageView addGestureRecognizer:doubleTap];
  • 三击手势
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    UITapGestureRecognizer *threeTop = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(TapGestureAction:)];
    threeTop.numberOfTapsRequired = 3;
    [doubleTap requireGestureRecognizerToFail:threeTop];
    [_imageView addGestureRecognizer:threeTop];
  • 点击事件方法
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    -(void)TapGestureAction:(UITapGestureRecognizer *)sender
    {
        if (sender.numberOfTapsRequired == 1) {
            NSLog(@"单击");
    }
        else if (sender.numberOfTapsRequired == 2) {
            NSLog(@"双击");
    }
        else
    {
            NSLog(@"三击");
    }
    }

轻扫手势

  • 向左轻扫
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
    leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft; //左
    [_imageView addGestureRecognizer:leftSwipe];
  • 向右轻扫
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
    rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;
    [_imageView addGestureRecognizer:rightSwipe];
  • 轻扫手势(左右)的事件方法
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
-(void)swipeAction:(UISwipeGestureRecognizer *)sender
{
    switch (sender.direction) {
        case UISwipeGestureRecognizerDirectionLeft:
        {
            NSLog(@"向左轻扫");
            if (_count > _images.count - 2) {
                
                //扫到最右一张弹出警示框
                UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"最后一张" preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction *sure = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
                
                [alertController addAction:sure];
                [self presentViewController:alertController animated:YES completion:nil];
                
                break;
            }
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:1.0f];
            [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:_imageView cache:YES];
            [UIView commitAnimations];
            
            
               _imageView.image = _images[++_count];
        }
            break;
            case UISwipeGestureRecognizerDirectionRight:
        {
            NSLog(@"向右轻扫");
            if (_count < _images.count - 2) {
                UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"第一张" preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction *sure = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
                
                [alertController addAction:sure];
                [self presentViewController:alertController animated:YES completion:nil];
                
                break;
            }
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:1.0f];
            [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:_imageView cache:YES];
            [UIView commitAnimations];
            
            _imageView.image = _images[--_count];
        }
        default:
            break;
    }
}

拖动手势

  • 拖动
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureAction:)];
    [_imageView addGestureRecognizer:panGesture];
  • 拖动事件方法
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
-(void)panGestureAction:(UIPanGestureRecognizer *)sender
{
    NSLog(@"拖动");
    //转换坐标系
   CGPoint point = [sender translationInView:self.view];
    _imageView.center = CGPointMake(_imageView.center.x + point.x, _imageView.center.y + point.y);
    [sender setTranslation:CGPointZero inView:self.view];
}

旋转手势

  • 旋转
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    UIRotationGestureRecognizer *rotationGestur = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
    [_imageView addGestureRecognizer:rotationGestur];
  • 旋转事件方法
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    -(void)rotationAction:(UIRotationGestureRecognizer *)sender
    {
        NSLog(@"旋转");
        _imageView.transform = CGAffineTransformMakeRotation(sender.rotation);
    }

捏合手势

  • 捏合
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGestureAction:)];
    [_imageView addGestureRecognizer:pinchGesture];
  • 捏合事件方法
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
-(void)pinchGestureAction:(UIPinchGestureRecognizer *)sender
{
    NSLog(@"捏合");
    _imageView.transform = CGAffineTransformMakeScale(sender.scale,sender.scale);
}

长按事件

  • 长按
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
    [_imageView addGestureRecognizer:longPress];
  • 长按事件方法
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
-(void)longPressAction:(UILongPressGestureRecognizer *)sender
{
    NSLog(@"长按");
    
    if (sender.state == UIGestureRecognizerStateBegan) {
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        
        UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alertController addAction:cancle];
        
        
        UIAlertAction *savePhoto = [UIAlertAction actionWithTitle:@"保存到相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            UIImageWriteToSavedPhotosAlbum(_imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
            
        }];
        [alertController addAction:savePhoto];
        
        
        UIAlertAction *openPhoto = [UIAlertAction actionWithTitle:@"打开相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            //资源类型为打开相册
            picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            picker.delegate = self;
            //选择后的图片可以被编辑
            picker.allowsEditing = YES;
            
            [self presentViewController:picker animated:YES completion:nil];
            
        }];
        [alertController addAction:openPhoto];
        
        
        UIAlertAction *turnonCamer = [UIAlertAction actionWithTitle:@"打开相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            picker.delegate = self;
            picker.allowsEditing = YES;
            
            [self presentViewController:picker animated:YES completion:nil];
        }];
        [alertController addAction:turnonCamer];
        
        [self presentViewController:alertController animated:YES completion:^{
            
        }];
    }
}
  • 保存照片的事件方法
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    NSString *msg = nil;
    if (!error) {
        msg = @"保存成功";
    }
    else
    {
        msg = @"图片保存失败";
    }
    
    NSLog(@"%@",msg);
    
}
  • 打开相册和打开相机的事件方法
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
    {
    //打开相机
    if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
        
        _imageView.image = info[@"UIImagePickerControllerEditedImage"];
    }
    //打开相册
    else
    {
        NSLog(@"info = %@",info);
        //获取永华编辑之后的图片
        _imageView.image = info[@"UIImagePickerControllerEditedImage"];
       
    }
    [self dismissViewControllerAnimated:YES completion:nil];
    }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016.02.25 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
iOS开发之手势识别
  感觉有必要把iOS开发中的手势识别做一个小小的总结。在上一篇iOS开发之自定义表情键盘(组件封装与自动布局)博客中用到了一个轻击手势,就是在轻击TextView时从表情键盘回到系统键盘,在TextView中的手是用storyboard添加的。下面会先给出如何用storyboard给相应的控件添加手势,然后在用纯代码的方式给我们的控件添加手势,手势的用法比较简单。和button的用法类似,也是目标动作回调,话不多说,切入今天的正题。总共有六种手势识别:轻击手势(TapGestureRecognizer),
lizelu
2018/01/11
2.7K0
iOS开发之手势识别
iOS手势与变形
手势在用户交互中有着举足轻重的作用,这篇文字简单的介绍了iOS中的手势,并通过手势对控件进行变形处理。若有错误,或不同的见解,请指正! 手势 ---- iOS手势分为下面这几种: UITapGestureRecognizer(点按) UIPanGestureRecognizer(拖动) UIScreenEdgePanGestureRecognizer (边缘拖动) UIPinchGestureRecognizer(捏合) UIRotationGestureRecognizer(旋转) UILongPr
BY
2018/05/11
1.9K0
iOS 小技能:响应者链的事件传递过程、手势识别器的使用步骤、抽屉效果的实现
为了完成手势识别,必须借助于手势识别器UIGestureRecognizer。利用UIGestureRecognizer,能轻松识别用户在某个view上面做的一些常见手势。
公众号iOS逆向
2022/08/22
9170
iOS 小技能:响应者链的事件传递过程、手势识别器的使用步骤、抽屉效果的实现
iOS中手势的应用1. 四类事件的主要方法2. 响应者链3. 手势识别功能(Gesture Recognizer)4. 手势的使用
iOS设备现如今大受欢迎的最重要原因之一就在于其开创了触控操作的潮流。发展到现在,无论是Android还是iPhone,现在APP与用户进行交互,基本上都是依赖于各种各样的触控事件。例如用户对屏幕进行了侧滑,APP就需要对这个手势进行相应的处理,给用户一个反馈。这些相应的事件就都是在UIResponder中定义的。 广告插播的措不及防:如果您要是觉得这篇文章让您有点收获,随手点个赞会让俺兴奋好久吶。 UIResponder大体有四类事件:触摸、加速计、远程控制、按压(iOS9.0以后出来的,3DTou
stanbai
2018/06/28
2.4K0
常用代码/Code
1、Alert - (void)showAlert{ UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定要这样做么" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" styl
Helloted
2022/06/07
3710
iOS 调用系统相机和选择相册照片
相机界面不显示中文问题: 在info.plist 添加Localizations 选择Chinese(simplified) 即可
ppppy
2022/11/15
1K0
iOS 调用系统相机和选择相册照片
UIGestureRecognizer  手势识别一、概念介绍二、UIView 的分类三、UIGestureRecognizer 抽象类四、UIGestureRecognizerDelegate 代理
一、概念介绍 UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,使用它的子类才能处理具体的手势 UITapGestureRecognizer(轻触,点按) UILongPressGestureRecognizer(长按) UISwipeGestureRecognizer(轻扫手势) UIRotationGestureRecognizer(旋转手势) UIPanGestureRecognizer(拖拽手势) UIPinchGestureRecognizer(捏合手势,缩
用户2141756
2018/05/18
3.2K0
iOS实现视频和图片的上传
这里有事先创建两个可变数组uploadArray, uploadedArray, 一个存放准要上传的内容, 一个存放上传完的内容
周希
2019/10/15
2K0
【IOS开发进阶系列】手势专题
        iPhone中处理触摸屏的操作,在3.2之前是主要使用的是由UIResponder而来的如下4种方式:
江中散人_Jun
2023/10/16
5510
【IOS开发进阶系列】手势专题
从相册中选择或拍照设置并上传头像图片设置头像
相信很多app中都有通过拍照或者从相册中选择的方式设置并上传头像的功能。如下是我之前一个项目中通过相册或者拍照获取图片的一个功能(照片来源于网络)。现在把代码贴出来,大家使用时(点击imageView
VV木公子
2018/06/05
6.7K0
iOS开发中的手势体系——UIGestureRecognizer分析及其子类的使用
        在iOS系统中,手势是进行用户交互的重要方式,通过UIGestureRecognizer类,我们可以轻松的创建出各种手势应用于app中。关于UIGestureRecognizer类,是对iOS中的事件传递机制面向应用的封装,将手势消息的传递抽象为了对象。有关消息传递的一些讨论,在前面的博客中有提到:
珲少
2018/08/15
2.1K0
iOS开发中的手势体系——UIGestureRecognizer分析及其子类的使用
iOS_38_手势
默认是会调用其[super touchesXXX],这个super就是上一个响应者
全栈程序员站长
2022/07/06
9650
iOS_38_手势
iOS-手势UIGestureRecognier详解一. 手势UIGestureRecognier简介二. 手势的抽象类——UIGestureRecognizer三. UIGestureRecogni
一. 手势UIGestureRecognier简介 iOS 3.2之后,苹果推出了手势识别功能(Gesture Recognizer),在触摸事件处理方面,大大简化了开发者的开发难度。利用UIGestureRecognizer,能轻松识别用户在某个view上面做的一些常见手势。UIGestureRecognizer是一个抽象类,对iOS中的事件传递机制面向应用进行封装,将手势消息的传递抽象为了对象。其中定义了所有手势的基本行为,使用它的子类才能处理具体的手势。 二. 手势的抽象类——UIGesture
xx_Cc
2018/05/10
2.6K0
iOS学习——UIAlertController详解
  在开发中,弹出提示框是必不可少的。这两天项目中统一对已经被iOS API废弃的UIAlertView和UIActionSheet进行替换,我们知道,UIAlertView和UIActionSheet都已经被iOS的API所废弃了。在两者的API中都建议用UIAlertController替代,并通过设置不同的类型风格来选择是原先的UIAlertView或UIActionSheet的形式。   之前项目中一直用的都是原先的UIAlertView和UIActionSheet风格,所以对UIAlertCont
mukekeheart
2018/03/01
3.5K0
iOS学习——UIAlertController详解
iOS14开发-触摸与手势识别
用于描述触摸的窗口、位置、运动和力度。一个手指触摸屏幕,就会生成一个 UITouch 对象,如果多个手指同时触摸,就会生成多个 UITouch 对象。
YungFan
2021/05/10
2.4K0
iOS开发——头像设置及本地沙盒保存,圆形头像显示
现在的APP中,对于头像的设置,我们大多采用圆形头像,并且需要支持从照相机获取或者从相册中选择用户需要的头像,并且保存在本地或者服务器中。
Originalee
2018/08/30
1.8K0
WKWebView的使用
WKWebView的使用 前言 最近项目中的UIWebView被替换为了WKWebView,因此来总结一下WKWebView的使用。 示例Demo:WKWebView的使用 本文将从以下几方面介绍WKWebView: 1、WKWebView涉及的一些类 2、WKWebView涉及的代理方法 3、网页内容加载进度条的实现 4、JS和OC的交互 5、本地HTML文件的实现 一、WKWebView涉及的一些类 WKWebView:网页的渲染与展示 注意: #import <WebKit/WebKi
且行且珍惜_iOS
2018/06/19
3.1K0
遮罩 HUD 指示器 蒙板 弹窗
遮罩 HUD 指示器 蒙板 弹窗 UIAlertView的使用<代理方法处理按钮点击> UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"警告" message:@"是否要删除它?" delegate:self cancelButtonTitle:@"是" otherButtonTitles:@"否", nil]; //加登录框 alertView.alertViewStyle = UIAlertViewStyleLoginA
用户1941540
2018/05/11
1.3K0
iOS图片缩小放大scollView实现代码
现在给大家分享我的项目中可以直接使用的组件,需要引入 afnetworking等第三方框架
用户8671053
2021/10/29
2.2K0
iOS·长按保存图片到相册:系统原生UIActionSheet与UIAlertView,UIAlertController等方案
场景: 在一个VC中,为一个UICollectionViewCell中的图片添加长按图片保存的事件。 长按保存图片 前提:infoPlist中添加相应权限:Privacy - Photo Libr
陈满iOS
2018/09/10
1.8K0
iOS·长按保存图片到相册:系统原生UIActionSheet与UIAlertView,UIAlertController等方案
推荐阅读
相关推荐
iOS开发之手势识别
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验