• 各种手势的总结


    首先该ViewController 要遵守手势的协议

    @interface ViewController () <UIGestureRecognizerDelegate>

    {

        UIImageView *_imageView;

    }

    常用的手势

    /*

     UITapGestureRecognizer Tap(点击)

     UIPinchGestureRecognizer Pinch(捏合/二指往內或往外拨动)

     UIRotationGestureRecognizer Rotation(旋转)

     UISwipeGestureRecognizer Swipe(滑动,快速移动)

     UIPanGestureRecognizer Pan (拖移,慢速移动)

     UILongPressGestureRecognizer LongPress(长按)

     */

    //alt/option 会出现两个手指 在模拟器

    - (void)creatView{

        _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(60, 100, 200, 300)];

        _imageView.image = [UIImage imageNamed:@"girl.jpg"];

        //想让_imageView能和用户交互 比如点击 旋转等等 那么这时

        //第一步必须打开用户交互

        _imageView.userInteractionEnabled = YES;

        //然后再增加手势就可以

        [self.view addSubview:_imageView];

    }

     一、滑动手势的使用

    #pragma mark - 滑动

    - (void)creatGuesture5 {

        //滑动手势  2048

        UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeClick:)];

        //设置方向

        swipe.direction = UISwipeGestureRecognizerDirectionLeft;

        /*

         UISwipeGestureRecognizerDirectionRight 向右滑动

         UISwipeGestureRecognizerDirectionLeft

         UISwipeGestureRecognizerDirectionUp

         UISwipeGestureRecognizerDirectionDown

         */

        //一个手势只能设置一个方向,四个方向都有滑动手势,必须创建四个滑动手势

        

        

        [_imageView addGestureRecognizer:swipe];

        [swipe release];

    }

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

        NSLog(@"滑动");

        switch (swipe.direction) {

            case UISwipeGestureRecognizerDirectionLeft:

            {

                NSLog(@"左滑动");

            }

                break;

            case UISwipeGestureRecognizerDirectionRight:

            {

                NSLog(@"右滑动");

            }

                break;

            case UISwipeGestureRecognizerDirectionUp:

            {

                NSLog(@"上滑动");

            }

                break;

            case UISwipeGestureRecognizerDirectionDown:

            {

                NSLog(@"下滑动");

            }

                break;

                

            default:

                break;

        }

    }

    //2048 的滑动

    #import "RootViewController.h"

    @interface RootViewController ()

    @end

    @implementation RootViewController

    {

        UILabel *_label;

    }

    - (void)dealloc{

        [_label release];

        [super dealloc];

    }

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

    {

        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

        if (self) {

            // Custom initialization

        }

        return self;

    }

    - (void)creatLabel{

        

        _label = [[UILabel alloc] initWithFrame:CGRectMake(110, 100, 100, 100)];

        _label.text = @"2";

        _label.layer.masksToBounds = YES;

        _label.layer.cornerRadius = 5;

        _label.textAlignment = NSTextAlignmentCenter;

        _label.backgroundColor = [UIColor redColor];

        [self.view addSubview:_label];

        

    }

    - (void)viewDidLoad

    {

        [super viewDidLoad];

        // Do any additional setup after loading the view.

        

        [self creatLabel];

        UISwipeGestureRecognizerDirection num[4] = {

            UISwipeGestureRecognizerDirectionRight,

            UISwipeGestureRecognizerDirectionLeft,

            UISwipeGestureRecognizerDirectionUp,

            UISwipeGestureRecognizerDirectionDown

        };

        //创建四个方向的滑动手势

        for (int i = 0; i < 4; i++) {

            //创建一个滑动手势

            UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];

            //设置手势方向

            swipe.direction = num[i];

            

            //给当前界面增加滑动手势

            [self.view addGestureRecognizer:swipe];

            [swipe release];

        }

            

    }

    //当手指在屏幕上 上下左右滑动时候会触发这个函数

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

       

        switch (sw.direction) {//手势的方向

                //向右滑动

            case UISwipeGestureRecognizerDirectionRight:

            {

                _label.center = CGPointMake(_label.center.x+100, _label.center.y);

            }

                break;

                //向左滑动

            case UISwipeGestureRecognizerDirectionLeft:

            {

             _label.center = CGPointMake(_label.center.x-100, _label.center.y);

            }

                break;

                //向上滑动

            case UISwipeGestureRecognizerDirectionUp:

            {

                _label.center = CGPointMake(_label.center.x, _label.center.y-100);

            }

                break;

                //向下滑动

            case UISwipeGestureRecognizerDirectionDown:

            {

                _label.center = CGPointMake(_label.center.x, _label.center.y+100);

            }

                break;

            

            default:

                break;

        }

    }

    二、旋转、捏合、点击

    #pragma mark - 手势协议

    //是否允许 两个手势同时有效 (代理的方法)

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

        NSLog(@"%@-%@",[gestureRecognizer class],[otherGestureRecognizer class]);

        return YES;

    }

    #pragma mark - 旋转手势

    - (void)creatGuesture3 {

        UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationClick:)];

        

        //设置代理 (如果要两个手势同时有效) 旋转和捏合 同时有效

        rotation.delegate = self;

        

        [_imageView addGestureRecognizer:rotation];

        [rotation release];

    }

    //旋转过程中一直调用

    - (void)rotationClick:(UIRotationGestureRecognizer *)rotation {

        NSLog(@"r:%f",rotation.rotation);//手势旋转的角度

        //每次都是相对 当前的 旋转 (当前的都是参照物)

        _imageView.transform = CGAffineTransformRotate(_imageView.transform, rotation.rotation);

        

        //相对的弧度应该重置为0

        rotation.rotation = 0;

    }

    #pragma mark - 捏合手势

    - (void)creatGuesture2 {

        //捏合手势 两个手指

        UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchClick:)];

       

        //设置代理 (如果要两个手势同时有效)

        pinch.delegate = self;

        

        

        [_imageView addGestureRecognizer:pinch];

        [pinch release];

    }

    //只要捏合就会一直调用

    - (void)pinchClick:(UIPinchGestureRecognizer *)pinch {

        NSLog(@"捏合%f",pinch.scale);

        //可以进行 缩放

        //pinch.scale获取手势的比例

        //相当于最原始的

        //_imageView.transform = CGAffineTransformMakeScale(pinch.scale, pinch.scale);

        //相对对当前的_imageView.transform

        _imageView.transform = CGAffineTransformScale(_imageView.transform, pinch.scale, pinch.scale);

        //把手势比例重置为1 应该每次都是相对的

        pinch.scale = 1;

    }

    #pragma mark - 点击手势

    - (void)creatGuesture1 {

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];

        //设置 点击次数(1表示单击 2 表示双击)

        tap.numberOfTapsRequired = 2;

        //设置手指的个数

        tap.numberOfTouchesRequired = 1;

        

        //UIGestureRecognizer 所有手势的父类

        //把手势加到图片视图

        [_imageView addGestureRecognizer:tap];

        [tap release];

    }

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

        NSLog(@"点击手势");

    }

     三、拖动、拖拽手势

    #pragma mark - 移动/拖拽

    - (void)creatGuesture4 {

        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panClick:)];

        

        [_imageView addGestureRecognizer:pan];

        [pan release];

    }

    - (void)panClick:(UIPanGestureRecognizer *)pan {

        //获取 拖拽手势相对于self.view 的 偏移量

        CGPoint curPoint = [pan translationInView:self.view];

        NSLog(@"%@",NSStringFromCGPoint(curPoint));

        

        CGPoint center = _imageView.center;

        center.x += curPoint.x;

        center.y += curPoint.y;

        _imageView.center = center;

        

        //把偏移量 重新设置为0  CGPointZero表示 0 0

        [pan setTranslation:CGPointZero inView:self.view];

    }

    四、长按手势

    #pragma mark - 长按手势

    - (void)creatGuesture6 {

        // 长按 删除 / 语音

        

        UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(pressClick:)];

        [_imageView addGestureRecognizer:longPress];

        [longPress release];

    }

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

        NSLog(@"长按");

        //会触发两次 一个长按开始的时候 还有就是长按结束的时候

        //我们要处理这种现象

        //UIGestureRecognizerStateBegan //开始

        //UIGestureRecognizerStateEnded 结束

        if (longPress.state == UIGestureRecognizerStateBegan) {

            NSLog(@"长按开始");

        }

    }

  • 相关阅读:
    枚举类型或运算
    设置字体同时为粗体、斜体
    批量处理任务进度条控制—基于BackgroundWorker
    PyCharm终端执行python脚本报错:ModuleNotFoundError: No module named 'lib.apilib'
    PyCharm用pytest模式执行python脚本不生成allure测试报告
    Python3+Appium基于安卓平台的app自动化
    JMeter上一个接口返回数据作为下一个接口的参数应用
    JMeter安装及语言切换
    Python之序列化和反序列化
    Requests库发送post请求,传入接口参数后报JSON parse error
  • 原文地址:https://www.cnblogs.com/PengFei-N/p/4703155.html
Copyright © 2020-2023  润新知