首页 > 代码库 > 手势(5)——利用GestureRecognizer

手势(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