• IOS开发(68)之捕获点击划屏手势


    1 前言

    手势其实是一组触摸事件的组合, 手势事件识别必须要添加到一个 UIView 这个类里面去,一个单独存在的视图可以添加多个手势识别器。一旦这个界面捕获到了一些手势动作,这个视图将会把这个手势动作传递给其他的手势识别器。
    一些触摸事件需要手机系统的支持,如下是 iOS SDK5 提供的 6 个手势识别器.
    • Swipe //划动
    • Rotation //转动
    • Pinch //收缩
    • Pan //摇动
    • Long press //长按
    • Tap //轻击
    最基础的框架为了能够处理手势的动作必须要按照如下步骤来进行操作:
     创建一个合适的手势识别器的对象。
     把这个手势识别器的对象绑定到一个视图上。
     添加一些捕获手势事件发生的方法。
    • 这个方法必须返回类型为空
    • 这个方法要么是无参数类型的,要么只能接受一个 UIGestureRecognizer 类型的参数。
    手势识别器一般可以分为两个大类,一个是单独的一个手势,一个是连贯的手势组合。单独的顾名思义, 就是一个手势之后就会有一个监听捕获的事件,然后来做相应的操作。连贯的就是一组手势动作,然后在进行 监听捕获事件进行相关的处理。的操作。连贯的就是一组手势动作,然后在进行 监听捕获事件进行相关的处理。
    参考代码
    - (void) tapRecognizer:(UITapGestureRecognizer *)paramSender{
    /* */
    }
    - (void) tapRecognizer{
    /* */
    }
    这节我们来学习UISwipeGestureRecognizer

    2 代码实例

    ZYViewController.m

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    	// Do any additional setup after loading the view, typically from a nib.
        /* 实例化手势对象 */
        self.swipeGestureRecognizer = [[UISwipeGestureRecognizer alloc]
                                       initWithTarget:self action:@selector(handleSwipes:)];
        /*从右向左滑动*/
        self.swipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
        /* 单个手指 */
        self.swipeGestureRecognizer.numberOfTouchesRequired = 1;
        /* 添加到view中 */
        [self.view addGestureRecognizer:self.swipeGestureRecognizer];
    }
    
    - (void) handleSwipes:(UISwipeGestureRecognizer *)paramSender{
        //按下
        if (paramSender.direction & UISwipeGestureRecognizerDirectionDown){
            NSLog(@"Swiped Down.");
        }
        //向左
        if (paramSender.direction & UISwipeGestureRecognizerDirectionLeft){
            NSLog(@"Swiped Left.");
        }
        //向右
        if (paramSender.direction & UISwipeGestureRecognizerDirectionRight){
            NSLog(@"Swiped Right.");
        }
        //抬起
        if (paramSender.direction & UISwipeGestureRecognizerDirectionUp){
            NSLog(@"Swiped Up.");
        }
    }

    运行结果

    鼠标在模拟器屏幕上从右向左移动后,控制台显示的结果

    2013-05-12 22:22:43.005 UISwipeGestureTest[734:c07] Swiped Left.

    3 结语

    以上是所有内容,希望对大家有所帮助。

    Demo实例下载:http://download.csdn.net/detail/u010013695/5363763

  • 相关阅读:
    显示在页面中间的加载gif
    10个超赞的jQuery图片滑块动画
    【Mybatis】mybatis设置指定列为空值
    【Centos】使用confluent将Mysql数据同步到clickhouse
    SpringBoot Mock测试RequestBody参数并包含其他参数接口
    【Linux】linux使用screen部署spring cloud项目
    Docker中部署jenkins
    【Mysql】Docker连接容器中的mysql 8报错 Public Key Retrieval is not allowed
    【MySQL】Windows下mysql的主从配置笔记
    This Gradle plugin requires a newer IDE able to request IDE model level 3. For Android Studio this means version 3.0+
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3074693.html
Copyright © 2020-2023  润新知