美文网首页
5.2 GestureRecognizer---UI手势

5.2 GestureRecognizer---UI手势

作者: 草根小强 | 来源:发表于2019-04-15 16:09 被阅读0次

    GestureRecognizer---UI手势

    #import "AppDelegate.h"
    #import "HomeViewController.h"
    
    @interface AppDelegate ()
    
    @end
    
    @implementation AppDelegate
    
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        // Override point for customization after application launch.
        _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        _window.backgroundColor = [UIColor whiteColor];
        [_window makeKeyAndVisible];
        
        HomeViewController *home = [[HomeViewController alloc] init];
        UINavigationController *nav =[[UINavigationController alloc] initWithRootViewController:home];
        
        
        _window.rootViewController = nav;
        return YES;
    }
    
    

    基础控制器

    #import <UIKit/UIKit.h>
    #define COLOR [UIColor colorWithRed:arc4random()%255 / 255.0 green:arc4random()%255 / 255.0 blue:arc4random()%255 / 255.0 alpha:1]
    @interface BasseViewController : UIViewController
    
    @end
    
    #import "BasseViewController.h"
    
    @interface BasseViewController ()
    
    @end
    
    @implementation BasseViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.title = NSStringFromClass([self class]);
        
        //随机颜色
        //取值范围0 - 1 arc4random() % 255取值0-254 个值  然后除 255.0 就是0-1范围,
        
        //取值范围 0 - 1
        UIColor *color = [UIColor colorWithRed:arc4random()%255 / 255.0 green:arc4random()%255 / 255.0 blue:arc4random()%255 / 255.0 alpha:1];
        self.view.backgroundColor = color;
        
        //产生一个灰度颜色  只包含黑白两种颜色   1 表示黑  0表示白
    //    UIColor *color1 = [UIColor colorWithWhite:0.5 alpha:1];
        
        
    }
    
    @end
    

    导航栏视图控制器,

    #import <UIKit/UIKit.h>
    
    @interface HomeViewController : UIViewController
    
    @end
    
    #import "HomeViewController.h"
    
    @interface HomeViewController ()
    
    @end
    
    @implementation HomeViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        NSArray *arr = @[@"TouchViewController",@"TapViewController",@"LongPressViewController",@"SwipeViewController",@"PanViewController",@"RotationViewController",@"PinchViewController",@"DoubleViewController"];
        for (int i = 0; i < arr.count; i ++) {
            UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 84 + 64*i, [UIScreen mainScreen].bounds.size.width, 44)];
            button.backgroundColor = [UIColor blueColor];
            [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
            
            [button setTitle:arr[i] forState:UIControlStateNormal];
            [self.view addSubview:button];
        }
    }
    - (void)buttonClicked:(UIButton *)button{
        
        Class class = NSClassFromString(button.titleLabel.text);
        
        UIViewController *vc = [[class alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
    @end
    

    根试图控制器

    #import <UIKit/UIKit.h>
    
    @interface HomeViewController : UIViewController
    
    @end
    
    #import "HomeViewController.h"
    
    @interface HomeViewController ()
    
    @end
    
    @implementation HomeViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        NSArray *arr = @[@"TouchViewController",@"TapViewController",@"LongPressViewController",@"SwipeViewController",@"PanViewController",@"RotationViewController",@"PinchViewController",@"DoubleViewController"];
        for (int i = 0; i < arr.count; i ++) {
            UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 84 + 64*i, [UIScreen mainScreen].bounds.size.width, 44)];
            button.backgroundColor = [UIColor blueColor];
            [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
            
            [button setTitle:arr[i] forState:UIControlStateNormal];
            [self.view addSubview:button];
        }
    }
    - (void)buttonClicked:(UIButton *)button{
        
        Class class = NSClassFromString(button.titleLabel.text);
        
        UIViewController *vc = [[class alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
    @end
    

    单击手势

    #import "BasseViewController.h"
    
    @interface TouchViewController : BasseViewController
    
    @end
    
    #import "TouchViewController.h"
    
    @interface TouchViewController ()
    
    @end
    
    @implementation TouchViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 84, 120, 60)];
        label.text= @"让子弹多飞一下";
        label.tag = 11;
        label.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:label];
    }
    static CGPoint  labelSrcOrigin;
    static CGPoint  userSrcOrigin;
    
    - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
        //如何获取用户触摸的点
        
    //    //1. 单点
    //    //获取触摸类型
    //    UITouch *touch = [touches anyObject];
    //    //获取视图对象的地点
    //    CGPoint point = [touch locationInView:self.view];
    //    
    //    //打印触摸点的地址
    //    //NSStringFromCGPoint(point) 将point转换成字符
    //    NSLog(@"%@",NSStringFromCGPoint(point));
        //2. 多点
        for (UITouch *touch in [event allTouches]){
        CGPoint point = [touch locationInView:self.view];
            NSLog(@"%@",NSStringFromCGPoint(point));
        }
        UILabel *label = (id)[self.view viewWithTag:11];
        labelSrcOrigin = label.frame.origin;
        UITouch *touch = [touches anyObject];
        userSrcOrigin =  [touch locationInView:self.view];
       }
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{   
        UITouch * touch = [touches anyObject];
        CGPoint pstPoint = [touch locationInView:self.view];
        //用户触摸移动产生的偏差量
        CGFloat _x = pstPoint.x - userSrcOrigin.x;
        CGFloat _y = pstPoint.y - userSrcOrigin.y;   
        UILabel * label = (id)[self.view viewWithTag:11];
        label.frame = CGRectMake(labelSrcOrigin.x + _x, labelSrcOrigin.y + _y, label.frame.size.width, label.frame.size.height);
    }
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
      }
    - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
    
    }
    @end
    

    双击手势

    
        
     #import "TapViewController.h"
    
    @interface TapViewController ()
    
    @end
    
    @implementation TapViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
    //    UIGestureRecognizer 手势父类
    //    [重点] 任何一个手势对象 不能被添加到两个视图上
      
    //    第一种
    //    //双击手势
    //    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapped:)];
    //    doubleTap.numberOfTapsRequired = 2;
    //    
    //    //所有UIView 的对像都有一个添加手势的方法
    //    [self.view addGestureRecognizer:doubleTap];
        
    //    //双击手势
       
        
        UITapGestureRecognizer * doubleTgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapped:)];
       
        doubleTgr.numberOfTapsRequired = 2;
        [self.view addGestureRecognizer:doubleTgr];
        
        
    //
        UITapGestureRecognizer * tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
        //numberOfTapsRequired = 1为单击
        tgr.numberOfTapsRequired = 1;
        //需要几只手指共同点击(这里是两只手指)
        tgr.numberOfTouchesRequired = 2;
        //响应tgr必须在doubleTgr不成立的情况下
        [tgr requireGestureRecognizerToFail:doubleTgr];
        
        // 所有UIView的对象都有一个添加手势的方法
    //    [self.view addGestureRecognizer:tgr];
        
        
        
        
        
    //    UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
    //    //区分单机和双击   1为单机  2 为双击  3 为三击
    //    tgr.numberOfTapsRequired = 1;
    //    tgr.numberOfTapsRequired = 2;
    //    //响应tgr必须在doubleTgr不成立的情况下
    //    [tgr requireGestureRecognizerToFail:doubleTap];
    //
    //    //需要几只手指共同点击
    //    tgr.numberOfTapsRequired = 2;
    //    [self.view addGestureRecognizer:tgr];
    //    
        
    }
    - (void)tapped:(UITapGestureRecognizer *)tgr{
        
        
        self.view.backgroundColor =[UIColor yellowColor];
        
    }
    - (void)doubleTapped:(UITapGestureRecognizer *)tgr{
        
        self.view.backgroundColor = [UIColor whiteColor];
       }
    @end
    

    长按手势

    #import "LongPressViewController.h"
    #define COLOR [UIColor colorWithRed:arc4random()%255 / 255.0 green:arc4random()%255 / 255.0 blue:arc4random()%255 / 255.0 alpha:1]
    @interface LongPressViewController ()
    
    @end
    
    @implementation LongPressViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        
        //创建长按手势对象
        
        UILongPressGestureRecognizer *logr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(pressed:)];
        //长按时长
        logr.minimumPressDuration = 3;
        //需要的手指数
        logr.numberOfTouchesRequired = 2;
        //将手势添加到视图上
        [self.view addGestureRecognizer:logr];
        
        
        
    }
    /*
     手势的状态:
         手势开始
         手势结束
         手势失败
     
     */
    
    - (void)pressed:(UILongPressGestureRecognizer *)logr{
        //代表手势开始
        if (logr.state == UIGestureRecognizerStateBegan) {
            self.view.backgroundColor = [UIColor whiteColor];
        }
        
    }
    @end
    

    滑动手势

    #import "SwipeViewController.h"
    
    @interface SwipeViewController ()
    
    @end
    
    @implementation SwipeViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        //滑动手势
        UISwipeGestureRecognizer *sgr = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipped:)];
    //    UISwipeGestureRecognizerDirectionRight
    //    UISwipeGestureRecognizerDirectionLeft
    //    UISwipeGestureRecognizerDirectionUp
    //    UISwipeGestureRecognizerDirectionDown
        //滑动的方向
        //右滑
        //一个滑动手势只能监听一个方向的滑动 (默认方向为右)
        sgr.direction = UISwipeGestureRecognizerDirectionRight;
        [self.view addGestureRecognizer:sgr];
    }
    - (void)swipped:(UISwipeGestureRecognizer *)swip{
        self.view.backgroundColor = [UIColor orangeColor];
    }
    @end
    

    捏合手势

    #import "PinchViewController.h"
    
    @interface PinchViewController ()
    
    @end
    
    @implementation PinchViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 81, 150, 180)];
        imageView.image = [UIImage imageNamed:@"QFImage_2016-01-26_14.48.9"];
        imageView.tag = 11;
         imageView.userInteractionEnabled = YES;
        [self.view addSubview:imageView];
        //【捏合手势】
        UIPinchGestureRecognizer * pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinching:)];
        
        [imageView addGestureRecognizer:pinch];
    }
    
    - (void)pinching:(UIPinchGestureRecognizer *)pinch {
        //每一秒执行一次
        //1,  1 :
        //2,  1 : 1.2    1
        //3,  1 : 1.2    1
        
        //2W   4W   16W    8
        //   2    4        8
        //获取图片的tag 也就是 图片
        UIImageView * imageView = (id)[self.view viewWithTag:11];
        
        //imageView.transform 表示的是时间
        //pinch.scale 表示的是x
        //pinch.scale表示的是y
        imageView.transform = CGAffineTransformScale(imageView.transform, pinch.scale, pinch.scale);
        
        //缩放比例执行完一次就置为与当前值一致的比例
        pinch.scale = 1;
        
        //    static CGAffineTransform trans;
        //    if (pinch.state == UIGestureRecognizerStateBegan) {
        //        trans = imageView.transform;
        //    }
        //    //获取缩放比
        //    CGFloat scale = pinch.scale;
        //
        //    imageView.transform = CGAffineTransformScale(trans, scale, scale);
    }
    
    @end
    

    旋转手势

    #import "RotationViewController.h"
    
    @interface RotationViewController ()
    
    @end
    
    @implementation RotationViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 81, 150, 180)];
        imageView.image = [UIImage imageNamed:@"QFImage_2016-01-26_14.48.9"];
        imageView.tag = 11;
         imageView.userInteractionEnabled = YES;
        [self.view addSubview:imageView];
        //旋转手势
        UIRotationGestureRecognizer *rgr = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotating:)];
    //    [imageView  addGestu]
        [imageView addGestureRecognizer:rgr];
     
        
    }
    - (void)rotating:(UIRotationGestureRecognizer *)rgr {
        
        UIImageView * imageView = (id)[self.view viewWithTag:11];
        /*
         imageView.transform = CGAffineTransformRotate(imageView.transform, rgr.rotation);
         
         rgr.rotation = 0;*/
        
        
        static CGAffineTransform trans;
        if (rgr.state == UIGestureRecognizerStateBegan) {
            //手势开始时,记录形变状态
            trans = imageView.transform;
        }
        //获取与初始状态的形变差
        CGFloat del = rgr.rotation;
        //根据初始状态与形变差获得当前状态
        //第一个参数为形变基础
        //第二个参数为形变量
        imageView.transform = CGAffineTransformRotate(trans, del);
        
    }
    @end
    

    拖动手势

    #import "PanViewController.h"
    
    @interface PanViewController ()
    
    @end
    
    @implementation PanViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        //拖动手势
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 80, 120, 60)];
        label.text =@"xiaoqiang";
        label.tag = 11;
        label.userInteractionEnabled = YES;
        [self.view addSubview:label];
        
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panned:)];
        [label addGestureRecognizer:pan];
        
    }
    
    - (void)panned:(UIPanGestureRecognizer *)pan{
        //1.在手势开始时,记录初始状态
        //2.根据手势,获取发生变化的差值
        //3.根据差值得到当前状态
        UILabel *label = (id)[self.view viewWithTag:11];
        
        static CGPoint srcOrigin;
        if (pan.state == UIGestureRecognizerStateBegan) {
            srcOrigin = label.frame.origin;
        }
        
        CGPoint delPoint = [pan translationInView:self.view];
        
        //当前位置 = 初始位置 + 差值
        label.frame = CGRectMake(srcOrigin.x +delPoint.x, srcOrigin.y + delPoint.y, label.frame.size.width, label.frame.size.height) ;
        
        
    }
    @end
    
    GestureRecognizer---UI手势.png

    相关文章

      网友评论

          本文标题:5.2 GestureRecognizer---UI手势

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