使用UIGestureRecognizer监听屏幕事件

 

转载自  http://blog.csdn.net/samguoyi/article/details/7911499

如果只是想获取屏幕点击事件有一个最简单的办法,就是写一个透明的uibutton覆盖在需要获取点击事件的view上面。

这里用第二种办法:

UIGestureRecognizer有很多继承自它的UIxxxGestureRecognizer:

 UILongPressGestureRecognizer

 UIPanGestureRecognizer

 UIPinchGestureRecognizer

 UIRotationGestureRecognizer

 UISwipeGestureRecognizer

 UITapGestureRecognizer

可以响应很多的点击事件,这里的demo介绍点击事件。

    //  
    //  MainViewController.h  
    //  testapple  
    //  
    //  Created by kiri on 12-5-8.  
    //  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.  
    //  
      
    #import <UIKit/UIKit.h>  
    #import "JSONKit.h"  
    #import "AppDelegate.h"  
    #import "GCDAsyncSocket.h"  
    #import <QuartzCore/QuartzCore.h>  
      
      
    @interface MainViewController : UIViewController<UIGestureRecognizerDelegate>  
    {  
        UIImage *aaaimg;  
        UIImage *bbbimg;  
        int index;  
    }  
      
    @property (nonatomic,retain) UIImageView *imageview;  
      
    @end  
 
        //  
        //  MainViewController.m  
        //  testapple  
        //  
        //  Created by kiri on 12-5-8.  
        //  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.  
        //  
          
        #import "MainViewController.h"  
          
        @implementation MainViewController  
        @synthesize imageview;  
          
          
        #pragma mark - View lifecycle  
          
          
        // Implement loadView to create a view hierarchy programmatically, without using a nib.  
        - (void)loadView  
        {  
            [super loadView];  
            self.view.tag = 11;  
              
            UITapGestureRecognizer *singleOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onviewtabed:)];  
            singleOne.numberOfTouchesRequired = 1;  
            singleOne.numberOfTapsRequired = 1;  
            singleOne.delegate = self;  
            [self.view addGestureRecognizer:singleOne];  
              
              
            UILongPressGestureRecognizer *longpress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(onlongtabed:)];  
            longpress.minimumPressDuration = 1.0;///至少按1秒  
            longpress.numberOfTouchesRequired = 1;//只有一个触点  
            [self.view addGestureRecognizer:longpress];  
            [longpress release];  
              
            UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
            [button setTitle:@"button" forState:UIControlStateNormal];  
            button.frame = CGRectMake(0, 0, 300, 50);  
            [button addTarget:self action:@selector(onbuttonclick) forControlEvents:UIControlEventTouchUpInside];  
            [self.view addSubview:button];      
              
            UITapGestureRecognizer *singleTwo = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onviewtabed:)];  
            singleTwo.numberOfTouchesRequired = 1;  
            singleTwo.numberOfTapsRequired = 1;  
            singleTwo.delegate = self;  
              
            self.imageview = [[UIImageView alloc] init];  
            imageview.frame = CGRectMake(0, 70, 320, 240);  
            imageview.image = [UIImage imageNamed:@"test.png"];  
            imageview.tag = 22;  
            [imageview addGestureRecognizer:singleTwo];  
            imageview.multipleTouchEnabled = YES;//设置属性使得uiimageview可以响应屏幕事件  
            imageview.userInteractionEnabled = YES;  
            [self.view addSubview:imageview];  
              
            aaaimg = [UIImage imageNamed:@"aaa.jpg"];  
            bbbimg = [UIImage imageNamed:@"bbb.jpg"];  
            imageview.image = aaaimg;  
              
              
              
              
        }  
          
        - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch  
        {  
            NSLog(@"gestureRecognizer");  
            return ([[touch.view class] isSubclassOfClass:[UIButton class]]) ? NO : YES;///当点击按钮时使得原按钮事件去响应,而不是gesture事件  
        }  
          
        -(void)onviewtabed:(UITapGestureRecognizer *)sender  
        {  
            NSLog(@"view touchs on tag = %d",sender.view.tag);  
            if(sender.state != UIGestureRecognizerStateEnded && sender.state != UIGestureRecognizerStateFailed)///注意需要判断状态  
            {  
                ///do things  
            }  
        }  
          
        -(void)onlongtabed:(UILongPressGestureRecognizer *)sender  
        {  
            NSLog(@"on long tabed");  
        }  
          
        // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.  
        - (void)viewDidLoad  
        {  
            [super viewDidLoad];  
        }  
          
          
        -(void)opinbackthread  
        {  
              
              
        }  
          
          
        #pragma mark - myfunctions  
        -(void)onbuttonclick  
        {  
            NSLog(@"onbuttonclick");  
              
        }  
          
        - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
        {  
              
            return (interfaceOrientation == UIInterfaceOrientationPortrait) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight);  
        }  
          
        -(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation  
        {  
            NSLog(@"did rotate");  
        }  
          
        -(void)dealloc  
        {  
            [super dealloc];  
        }  
          
        @end
原文地址:https://www.cnblogs.com/allanliu/p/4482431.html