• 手势(5)——利用GestureRecognizer


    #import "MJmainViewController.h"

    @interface MJmainViewController ()

    @end

    @implementation MJmainViewController

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }


    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        self.title=@"UITapGestureRecognizer";
        self.view.backgroundColor=[UIColor greenColor];
        
        //============= 轻拍==============
        UITapGestureRecognizer *onefinger=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onetap:)];
        onefinger.numberOfTapsRequired=1;
        onefinger.numberOfTouchesRequired=1;
        [self.view addGestureRecognizer:onefinger];
        
        
        UITapGestureRecognizer *doublefinger=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(twotap:)];
        
        doublefinger.numberOfTapsRequired=2;
        doublefinger.numberOfTouchesRequired=1;
        [self.view addGestureRecognizer:doublefinger];
        [onefinger  requireGestureRecognizerToFail:doublefinger];
        
        
        //==============缩放=============
        UIPinchGestureRecognizer *pinch=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(onPinch:)];
        
        [self.view addGestureRecognizer:pinch];
        
        
        //===============拖拽=============
    #if 1
        UIPanGestureRecognizer *pan =[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(onPan:)];
        pan.maximumNumberOfTouches=2;
        pan.minimumNumberOfTouches=1;
        [self.view addGestureRecognizer:pan];
    #endif
        
        //===============横扫=============
        UISwipeGestureRecognizer *swiperight =[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(onSwipe:)];
        swiperight.direction=UISwipeGestureRecognizerDirectionRight;
        [self.view addGestureRecognizer:swiperight];
        
        UISwipeGestureRecognizer *swipeleft =[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(onSwipe:)];
        swipeleft.direction=UISwipeGestureRecognizerDirectionLeft;
        [self.view addGestureRecognizer:swipeleft];
        
        UISwipeGestureRecognizer *swipeup =[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(onSwipe:)];
        swipeup.direction=UISwipeGestureRecognizerDirectionUp;
        [self.view addGestureRecognizer:swipeup];
        
        UISwipeGestureRecognizer *swipedown =[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(onSwipe:)];
        swipedown.direction=UISwipeGestureRecognizerDirectionDown;
        [self.view addGestureRecognizer:swipedown];
        
        [pan requireGestureRecognizerToFail:swiperight];
        [pan requireGestureRecognizerToFail:swipeleft];
        [pan requireGestureRecognizerToFail:swipeup];
        [pan requireGestureRecognizerToFail:swipedown];

       //===================旋转=================
        UIRotationGestureRecognizer *rotation=[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(onRatation:)];
        [self.view addGestureRecognizer:rotation];
        

        

    //  ====================边界拖入界面==================
        UIScreenEdgePanGestureRecognizer *screenleft =[[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(onscreen:)];
        screenleft.edges=UIRectEdgeLeft;
        [self.view addGestureRecognizer:screenleft];
        
        UIScreenEdgePanGestureRecognizer *screenfight =[[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(onscreen:)];
        screenfight.edges=UIRectEdgeRight;
        [self.view addGestureRecognizer:screenfight];
        
        [swiperight requireGestureRecognizerToFail:screenleft];
        

        
        //  ====================长摁===================
        UILongPressGestureRecognizer *longpress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(onLongPress:)];
        longpress.numberOfTouchesRequired=1;
        [self.view addGestureRecognizer:longpress];
        
        
        
        [onefinger requireGestureRecognizerToFail:longpress];
        

        
        
    }





    -(void)onetap:(UITapGestureRecognizer *)sender
    {
        NSLog(@"单击");
    }



    -(void)twotap:(UITapGestureRecognizer *)sender
    {
        NSLog(@"双击");
    }



    -(void)onPinch:(UIPinchGestureRecognizer *)sender
    {
        if (sender.scale>1) {
            NSLog(@"放大");
        }else{
            NSLog(@"缩放");}
    }

    -(void)onPan:(UIPanGestureRecognizer *)sender
    {
        NSLog(@"拖拽");
    }

    -(void)onSwipe:(UISwipeGestureRecognizer *)sender
    {
        
        if (sender.direction == UISwipeGestureRecognizerDirectionRight) {
            
            NSLog(@"右扫");
        }else{
            if ( sender.direction==UISwipeGestureRecognizerDirectionLeft) {
                NSLog(@"左扫");
            }else{
                if (sender.direction==UISwipeGestureRecognizerDirectionUp ) {
                    NSLog(@"向上扫");
                    
                }else{
                    NSLog(@"向下扫");
                    
                }
            }
            
        }
    }

    -(void)onRatation:(UIRotationGestureRecognizer *)sender
    {
        NSLog(@"%f",sender.rotation);
        
       
    }

    -(void)onscreen:(UIScreenEdgePanGestureRecognizer *)sender
    {
        if (sender.edges==UIRectEdgeLeft) {
            NSLog(@"左边的妹妹快出来");
        }else{
        NSLog(@"右边的妹妹快出来");
        }
    }


    -(void)onLongPress:(UILongPressGestureRecognizer *)sender
    {
        NSLog(@"长摁");
    }

    @end

  • 相关阅读:
    从MANNING抽的新书 免费分享 <算法图解>
    这些天在看的
    创建Spring框架程序初始化的的几种方式
    我在互联网公司的一天
    python文件中 代码的顺序
    分布式事务,两阶段提交协议,三阶段提交协议
    Zookeeper工作原理
    CAP定理
    SpringCloud 架构
    springCloud(一)Erueka工作原理
  • 原文地址:https://www.cnblogs.com/wangdelong/p/3848575.html
Copyright © 2020-2023  润新知