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

    //
    //  ViewController.m
    //  手势12——26class
    //
    //  Created by lanqs on 14-12-26.
    //  Copyright (c) 2014年 Li_Qian. All rights reserved.
    //

    #import "ViewController.h"

    @interface ViewController ()

    {
        UIView * _shoushiView;
        UILabel * _showLabel;
    }
    - (void)initUserFace;
    @end

    @implementation ViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
        [self initUserFace];
        // Do any additional setup after loading the view, typically from a nib.
    }

    #pragma  mark --init

    - (void)initUserFace
    {
        _shoushiView = [[UIView alloc]initWithFrame:CGRectMake(00200200)];
        _shoushiView.center = self.view.center;
        _shoushiView.backgroundColor = [UIColor grayColor];
        
        [self.view addSubview:_shoushiView];
        
        _showLabel = [[UILabel alloc]initWithFrame:CGRectMake(00100100)];
        _showLabel.text = @"fdsaf";
        _showLabel.textAlignment = NSTextAlignmentCenter;
        _showLabel.center = CGPointMake(CGRectGetMidX(_shoushiView.bounds), CGRectGetMidY(_shoushiView.bounds));
        _showLabel.font = [UIFont systemFontOfSize:20];
        [self.view addSubview:_showLabel];
        
        //点击手势识别器
        
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(processGeture:)];
        
        tap.numberOfTouchesRequired = 1;//点击次数
        tap.numberOfTapsRequired = 1;//手指数量
        [_shoushiView addGestureRecognizer:tap];//添加手势识别器

        
        
    //长按 手势识别器
        
        UILongPressGestureRecognizer * longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(processGeture:)];
        longPress.minimumPressDuration = 1;//最短的触发时间
        
        [_shoushiView addGestureRecognizer:longPress];
        
        //滑动
        
        UISwipeGestureRecognizer * swipGesture =[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(processGeture:)];
        swipGesture.direction = UISwipeGestureRecognizerDirectionRight;//滑动方向的设置
        [_shoushiView addGestureRecognizer:swipGesture];
        
        
    //    //拖动
        UIPanGestureRecognizer * panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(processGeture:)];
        
        //当此两个手势冲突时,swipGesture有效
        [panGesture requireGestureRecognizerToFail:swipGesture];//防止拖动 与滑动的冲突人、
        [_shoushiView addGestureRecognizer:panGesture];
        
        //捏合
        
        UIPinchGestureRecognizer * pinchGesture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(processGeture:)];
        [_shoushiView addGestureRecognizer:pinchGesture];
        
        //旋转手势
        
        UIRotationGestureRecognizer * rotationGesture = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(processGeture:)];
        
        [_shoushiView addGestureRecognizer:rotationGesture];
        
    }

    //UIGestureRecognizer是所有手势的父类
    - (void)processGeture:(UIGestureRecognizer *)gesture
    {
        //判断手势的类型
        if ([gesture isKindOfClass:[UITapGestureRecognizer class]])
        {
            _showLabel.text = @"点击手势";
        }
        
        if ([gesture isKindOfClass:[UILongPressGestureRecognizer class]])
        {
             _showLabel.text = @"长按手势";
        }
        
        if ([gesture isKindOfClass:[UISwipeGestureRecognizer class]])
        {
            UISwipeGestureRecognizer * sw = (UISwipeGestureRecognizer *)gesture;
            
            if (sw.direction == UISwipeGestureRecognizerDirectionRight)//判断方向
            {
                   _showLabel.text = @"滑动手势";
            }
          
            NSLog(@"fdfg");
        }
        if ([gesture isKindOfClass:[UIPanGestureRecognizer class]])
        {
            _showLabel.text = @"拖动手势";
            
            //静态定义一个点.保存初始点
            static CGPoint startPoint;
            
            //手势状态gesture.state
    //        gesture.state = UIGestureRecognizerStateBegan;
            
    //表明pan这是一个拖动手势
            UIPanGestureRecognizer * pan = (UIPanGestureRecognizer *)gesture;
            if (pan.state == UIGestureRecognizerStateBegan)
            {
                startPoint = _shoushiView.center;
            }
            else if (pan.state == UIGestureRecognizerStateChanged)//表明点在改变
            {
                //translationInView拖动方法  是在那个视图上变化的量
                CGPoint translate = [pan translationInView:self.view];
                _shoushiView.center = CGPointMake(startPoint.x + translate.x, startPoint.y + translate.y);
            }
            
        }
        
        if ([gesture isKindOfClass:[UIPinchGestureRecognizer class]])
        {
            _showLabel.text = @"捏合手势";
            //用一个静态变量存储 上一次缩放的值
            static CGFloat lastScale;
            UIPinchGestureRecognizer * pin = (UIPinchGestureRecognizer *)gesture;
            NSLog(@"%f",pin.scale);
            if (pin.state == UIGestureRecognizerStateBegan)
            {
                lastScale = pin.scale;
            }
            else if (pin.state == UIGestureRecognizerStateChanged)
            {
                
                 CGFloat changeScale = (pin.scale - lastScale) /21;
                
                _shoushiView.transform = CGAffineTransformScale(_shoushiView.transform, changeScale, changeScale);
                lastScale = pin.scale;//重点  细节点
                
                
    //第二种用CGAffineTransMakeScale方式
            }
        }
        
        if ([gesture isKindOfClass:[UIRotationGestureRecognizer class]])
        {
            _showLabel.text = @"旋转手势";
            
            UIRotationGestureRecognizer * rotate = (UIRotationGestureRecognizer *)gesture;
            
            static CGFloat lastRotation;//幅度值
            
            if (rotate.state == UIGestureRecognizerStateBegan)
            {
                lastRotation = rotate.rotation;
            }
            else if (rotate.state == UIGestureRecognizerStateChanged)
            {
                   CGFloat changeRotation = rotate.rotation - lastRotation;
                _shoushiView.transform = CGAffineTransformRotate(_shoushiView.transform, changeRotation);
                
                lastRotation = rotate.rotation;
            }
        }
    }

    @end

  • 相关阅读:
    造数据
    自定义注解
    利用线程池,同步线程实现并发
    ThreadPoolExecutor 线程池
    velocity 模板
    [python] 解析xml文件
    url 中需要转义的字符
    Appium 坑
    TestNG 101
    【python】print · sys.stdout · sys.stderr
  • 原文地址:https://www.cnblogs.com/hxwj/p/4717627.html
Copyright © 2011-2022 走看看