zoukankan      html  css  js  c++  java
  • 手势识别器的使用

    //     1、轻拍手势识别器的使用

        UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTapGesture:)];

        //设置需要轻拍的次数

        tapGesture.numberOfTapsRequired=1;

        //设置需要触摸的个数:

        tapGesture.numberOfTouchesRequired=2;//使用Alt

        //添加手势识别器到被作用的视图

        [aView addGestureRecognizer:tapGesture];

        [tapGesture release];

    //    2、创建一个平移手势识别器

        UIPanGestureRecognizer *panGesture=[[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePanGesture:)]autorelease];

        [aView addGestureRecognizer:panGesture];

    //    3、创建一个旋转手势识别器

        UIRotationGestureRecognizer *rotationGesture=[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(handleRotationGesture:)];

        [aView addGestureRecognizer:rotationGesture];

    //    4、创建一个缩放手势识别器

        UIPinchGestureRecognizer *pinchGesture=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinchGesture:)];

        [aView addGestureRecognizer:pinchGesture];//捏合

        tapGesture.delegate=self;

        rotationGesture.delegate=self;

        pinchGesture.delegate=self;

    //    5、创建一个长按手势识别器

        UILongPressGestureRecognizer *longPressGesture=[[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPressGesture:)]autorelease];

        [aView addGestureRecognizer:longPressGesture];

        longPressGesture.minimumPressDuration=0.5;

    //    6、创建一个轻扫手势识别器

        UISwipeGestureRecognizer *swipeGesture=[[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeGesture:)]autorelease];

        [aView addGestureRecognizer:swipeGesture];

        swipeGesture.direction=UISwipeGestureRecognizerDirectionDown;

    //    7、创建一个屏幕边缘轻扫识别器

        UIScreenEdgePanGestureRecognizer *screenEdgeGesture=[[[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(handlescreenEdgeGesture:)]autorelease];

        [self.view addGestureRecognizer:screenEdgeGesture];

        screenEdgeGesture.edges=UIRectEdgeLeft;

    //当一个手势识别器对象被识别后,会询问代理对象,是否允许同时识别其他手势识别器。如果此协议方法返回yes,表示允许。默认不允许

    -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{

        return YES;

    }

    -(void)handlePanGesture:(UIPanGestureRecognizer *)sender{

        UIView *aView=sender.view;

        //获取手势在视图对应坐标系中产生的位移增量

        CGPoint point=[sender translationInView:aView.superview];

        //在上次视图形变的基础上产生新的形变

        aView.transform=CGAffineTransformTranslate(aView.transform, point.x, point.y);

      //将当前产生的增量归零

        [sender setTranslation:CGPointZero inView:aView.superview];

    }

    -(void)handleRotationGesture:(UIRotationGestureRecognizer *)sender{

        UIView *aView=sender.view;

        aView.transform=CGAffineTransformRotate(aView.transform, sender.rotation);

        sender.rotation=0;//将旋转产生的角度增量设置为0;

    -(void)handlePinchGesture:(UIPinchGestureRecognizer *)sender{

        UIView *aView=sender.view;

        aView.transform=CGAffineTransformScale(aView.transform, sender.scale, sender.scale);

        sender.scale=1;

    }

  • 相关阅读:
    浅析七种经典排序算法
    一个可编辑与新增博客园文章的 Python 脚本
    快速排序的几种实现方式
    如何查找某个网站的(如:有道云笔记)的接口
    一键导出「有道云笔记」所有笔记
    2020年启蒙及小学识字练字APP或小程序测评榜
    2020年部编版小学二年级语文上册知识点(完整版)
    2020年部编人教版小学语文一年级下册知识点汇总
    换个角度,程序员爸爸应该关注一下
    计算机基础知识-I/O篇
  • 原文地址:https://www.cnblogs.com/sxsy-2015/p/4901176.html
Copyright © 2011-2022 走看看