zoukankan      html  css  js  c++  java
  • ios 手势

    1. 手势
    将用户物理性的触屏操作变成对象存储起来,所有手势的父类 **UIGestureRecognizer **

    系统将一些有特点的触屏操作封装成不同的手势类型包括以下几种(6种):

    UISwipeGestureRecognizer 轻扫
    UILongPressGestureRecognizer 长按
    UIPinchGestureRecognizer 捏合
    UIPanGestureRecognizer 拖拽
    UIRotationGestureRecognizer 旋转

    如何使用手势?
    step1:创建指定手势的实例,在创建时设定当该手势发生时,系统会自动发什么消息
    ** step2.设置手势的核心属性
    ** step3.
    将手势添加到某个视图中,当用户在该视图上做了相应的动作,就会触发手势,系统会捕获并调用手势的事件方法

    **a.Taps手势 **
    **核心属性 **

    numberOfTapsRequired
    numberOfTouchesRequired

    b.Swipe 手势 (轻扫)
    核心属性
    direction 方向

    上面两个手势 都是 一次性手势,即手势发生过程中,响应方法只执行一次


    c. UILongPress 手势 长按
    核心属性
    minimumPressDuration 长按所需最小时间

    d. UIPan 手势 拖拽
    e. UIPich 手势 捏合
    f. UIRotation 手势 旋转

    Taps手势:

    - (void)viewDidLoad {
        [super viewDidLoad];
    //    1.创建手势对象
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
    
        //2.设置手势的核心属性
        tap.numberOfTapsRequired = 5; //点几次
        tap.numberOfTouchesRequired = 1; //几个触摸点
        //3.将手势添加到某个视图中,当用户在该视图上做了相应的动作,就会触发手势,系统会捕获并调用手势的事件方法
        [self.myView addGestureRecognizer:tap];
    }
    
    -(void)tap:(UITapGestureRecognizer*)gr {
       
        NSLog(@"点击手势触发,点中的位置是 %@",  NSStringFromCGPoint([gr locationInView:self.myView]));
    }
    

    Swipe 手势 (轻扫):

    - (void)viewDidLoad {
        [super viewDidLoad];
    //    创建手势对象
        UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
        
        //上下左右 设置 只 触发 左右
        swipe.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown;
    
        [self.view addGestureRecognizer:swipe];
    }
    
    -(void)swipe:(UISwipeGestureRecognizer*)gr {
        NSLog(@"清扫方向 %lu", gr.direction);
        //结束父视图编辑  收键盘
        [self.view endEditing:YES];
    }
    

    UILongPress 手势 长按:

    - (void)viewDidLoad {
        [super viewDidLoad];
    //    创建手势对象
        UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
        //设置长按触发最小时间
        longPress.minimumPressDuration = 2;
        [self.view addGestureRecognizer:longPress];
    }
    -(void)longPress:(UILongPressGestureRecognizer*)gr {
        NSLog(@"%@",  NSStringFromCGPoint([gr locationInView:self.view]));
        if (gr.state == UIGestureRecognizerStateBegan) {
                NSLog(@"开始长按");
        }else if(gr.state == UIGestureRecognizerStateChanged) {
                NSLog(@"移动");
        }else if(gr.state == UIGestureRecognizerStateEnded) {
                NSLog(@"长按手势结束");
        }
    }
    

    d. UIPan 手势 拖拽

    - (void)viewDidLoad {
        [super viewDidLoad];
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
        [self.view addGestureRecognizer:pan];
    }
    
    -(void)pan:(UIPanGestureRecognizer*)gr {
        NSLog(@"1--%@",NSStringFromCGPoint([gr locationInView:self.view]));
        NSLog(@"2--%@",NSStringFromCGPoint([gr translationInView:self.view]));
        //方式一
        if (gr.state == UIGestureRecognizerStateBegan) {
            self.startPos = self.imageView.center;
        }else {
            CGPoint translation = [gr translationInView:self.view];
            CGPoint center = self.imageView.center;
            center.x = self.startPos.x + translation.x;
            center.y = self.startPos.y + translation.y;
            self.imageView.center = center;
        }
        
        //方式二
    //    CGPoint translation = [gr translationInView:self.view];
    //    CGPoint center = self.imageView.center;
    //    center.x += translation.x;
    //    center.y += translation.y;
    //    self.imageView.center = center;
    //    [gr setTranslation:CGPointZero inView:self.view];
        
        //方式三
    //    if (gr.state == UIGestureRecognizerStateBegan) {
    //        //手势开始时 记录 起始位置
    //        self.startPos = [gr locationInView:self.view];
    //    }else if (gr.state == UIGestureRecognizerStateChanged){
    //        //获取本次 移动的位置
    //        CGPoint move = [gr locationInView:self.view];
    //        //图片的位置的等于当前位置 加上 移动位置-上次位置 的 便宜
    //        CGPoint center = self.imageView.center;
    //        center.x += move.x - self.startPos.x;
    //        center.y += move.y - self.startPos.y;
    //        self.imageView.center = center;
    //        //设置本次位置  为下次的 起始位置
    //        self.startPos = move;
    //    }
    }
    

    e. UIPich 手势 捏合

    - (void)viewDidLoad {
        [super viewDidLoad];
        UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];
        [self.view addGestureRecognizer:pinch];
    }
    
    -(void)pinch:(UIPinchGestureRecognizer*)gr {
        NSLog(@"速率 %.2f",gr.velocity);
        NSLog(@"缩放比 %.2f",gr.scale);
    }
    

    f. UIRotation 手势 旋转

    - (void)viewDidLoad {
        [super viewDidLoad];
        UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];
        [self.view addGestureRecognizer:rotation];
    }
    
    -(void)rotation:(UIRotationGestureRecognizer*)gr {
        NSLog(@"%.2f",gr.rotation);
    }
    
    成功的三大原则: 1、坚持 2、不要脸 3、坚持不要脸
  • 相关阅读:
    Spring中的资源加载
    分布式系统Paxos算法
    MySQL中MyISAM与InnoDB区别及选择(转)
    Unable to construct api.Node object for kubelet: can't get ip address of node master.example.com: lookup master.example.com on : no such host
    分库情况下的数据库连接注入
    Core源码(二) Linq的Distinct扩展
    B-Tree详解
    C#进阶之路(八)集合的应用
    重温CLR(十八) 运行时序列化
    重温CLR(十七)程序集加载和反射
  • 原文地址:https://www.cnblogs.com/xulinmei/p/7420203.html
Copyright © 2011-2022 走看看