Gesture手势

作者: ly渐行渐远 | 来源:发表于2015-12-04 20:40 被阅读309次

    手势识别器

    手势识别器是对触摸事件做了封装,我们无需自己去判断某个手势是否触发,手势识别器本身起到了识别作用,我们把重心放在识别之后要做什么操作上面。手势识别器是iOS中比较抽象的一个类,用于识别一个手势。
    手势识别器有7个子类:轻拍手势、平移手势、轻扫手势、缩放手势、旋转手势、长按手势以及屏幕边界平移手势。一旦指定的手势被识别,我们可以执行我们自己定义好的操作。

    UIView *gestureView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
    gestureView.center = self.window.center;//将当前视图放置在window中间
    gestureView.backgroundColor = [UIColor grayColor];//设置背景色
    [self.window addSubview:gestureView];//添加到window上```
    
    ##UITapGestureRecognizer
    轻拍手势识别器,能识别轻拍操作
    ```//轻拍手势
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    //给创建好的视图添加手势
    [gestureView addGestureRecognizer:tapGR];
    [tapGR setNumberOfTapsRequired:2];//设置轻怕次数
    tapGR.numberOfTouchesRequired = 1;//设置轻怕次数```
    ```//轻扫手势的回调方法
    - (void)swipeAction: (UISwipeGestureRecognizer *)sender
    {
        NSLog(@"123");
        //创建一个警示框
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示框" message:@"内容" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alertView show];//展示警示框
        
    }```
    
    ##UILongPressGestureRecognizer
    长按手势识别器,能识别长按操作。
    ```//长按手势
    UILongPressGestureRecognizer *longPGR = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longAction:)];
    [gestureView addGestureRecognizer:longPGR];```
    ```//长按手势的回调方法
    - (void)longAction: (UILongPressGestureRecognizer *)sender
    {
        //直接声明,长按手势的回调方法会执行两次,因为长按状态下有开始和结束两种状态,如果不加判断,系统会认为结束也就是另一个长按的开始,所以加一句在长按结束时再执行,开始时不执行
        if (sender.state == UIGestureRecognizerStateEnded) {
            NSLog(@"000");
        }
        else if (sender.state == UIGestureRecognizerStateBegan)
        {
            NSLog(@"123");
        }
    }```
    
    ##UIPinchGestureRecognizer
    捏合手势识别器,能识别捏合操作。
    ```//捏合手势
    UIPinchGestureRecognizer *pinchGR = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
    [gestureView addGestureRecognizer:pinchGR];```
    ```//捏合手势的回调方法
    - (void)pinchAction: (UIPinchGestureRecognizer *)sender
    {
        //通过捏合手势得到缩放比率
        float scale = sender.scale;
        //得到该手势得到的视图
        UIView *view = sender.view;
        //2D仿射变换函数的缩放函数来实现视图的放大缩小
        //是在原有基础上来改变当前的视图
        /**
         *  @param t#>  <#t#> description#> 现有的视图的transform
         *  @param sx#> <#sx#> description#> x轴上的缩放比率
         *  @param sy#> <#sy#> description#> y轴上的缩放比率
         *
         */
        view.transform = CGAffineTransformScale(view.transform, scale, scale);
        //是在视图最初的tramsfrom状态上改变,不管执行多少次,都是以该视图最初的transform状态为基础来改变
        //每次捏合动作完毕之后,让此次捏合值复原,使得它每次捏合都是从1倍开始
        sender.scale = 1;
    }```
    
    ##UIRotationGestureRecognizer
    旋转手势识别器,能识别旋转操作。
    ```//旋转手势
    UIRotationGestureRecognizer *rotationGR = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
    [gestureView addGestureRecognizer:rotationGR];
    
    - (void)rotationAction: (UIRotationGestureRecognizer *)sender
    {
        //通过手势得到旋转角度
        float rota = sender.rotation;
        //得到该手势作用的视图
        UIView *view = sender.view;
        //通过2D仿射变换函数中的旋转函数来使得当前视图旋转
        view.transform = CGAffineTransformRotate(view.transform, rota);
        //复原
        sender.rotation = 0; 
    }```
    
    ##UIPanGestureRecognizer
    平移手势识别器,能识别拖拽操作。
    ```//平移手势
    UIPanGestureRecognizer *panGP =[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
    [gestureView addGestureRecognizer:panGP];```
    ```//平移手势的回调方法
    - (void)panAction: (UIPanGestureRecognizer *)sender
    {
        //得到手势当前所在视图
        UIImageView *imageView = (UIImageView *)sender.view;
        //得到手势在视图上移动的偏移量
        CGPoint late = [sender translationInView:imageView.superview];
        //通过2D方式变换函数中的平移函数使得当前函数平移
        imageView.transform = CGAffineTransformTranslate(imageView.transform, late.x, late.y);
        [sender setTranslation:CGPointZero inView:imageView.superview];   
    }```
    
    
    ##UISwipeGestureRecognizer
    轻扫手势识别器,能识别拖拽操作。
    ```//轻扫手势
    UISwipeGestureRecognizer *swipeGR = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
    //设置轻扫的方向,默认值是从左往右,最多同时支持(左右)或者(上下)
    swipeGR.direction = UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionUp;
    [gestureView addGestureRecognizer:swipeGR];```
    ```//轻扫手势的回调方法
    - (void)swipeAction: (UISwipeGestureRecognizer *)sender
    {
        NSLog(@"实现了轻扫手势");
    }```
    
    ##UIScreenEdgePanGestureRecognizer
    屏幕边缘轻扫识别器,是iOS7中新增的手势。
    ```//边缘轻扫手势
    UIScreenEdgePanGestureRecognizer *screenEdgePanGr = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(edgeAction:)];
    screenEdgePanGr.edges = UIRectEdgeAll;
    [gestureView addGestureRecognizer:screenEdgePanGr];```
    ```- (void)edgeAction: (UIScreenEdgePanGestureRecognizer *)sender
    {
        NSLog(@"成功触发了屏幕边缘手势");
    }```
    
    #使得多个手势可以同时响应的代理方法(必须对手势指定代理)
    ```- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
    {
        //返回值为yes的时候,当执行一个手势的操作的时候,也可以执行其它手势的操作
        return YES;
    }```
    
    
    

    相关文章

      网友评论

      本文标题:Gesture手势

      本文链接:https://www.haomeiwen.com/subject/ojjjhttx.html