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

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

这里用第二种办法:

UIGestureRecognizer有很多继承自它的UIxxxGestureRecognizer:

UILongPressGestureRecognizer

UIPanGestureRecognizer

UIPinchGestureRecognizer

UIRotationGestureRecognizer

UISwipeGestureRecognizer

UITapGestureRecognizer

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

        //
// 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 = ; UITapGestureRecognizer *singleOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onviewtabed:)];
singleOne.numberOfTouchesRequired = ;
singleOne.numberOfTapsRequired = ;
singleOne.delegate = self;
[self.view addGestureRecognizer:singleOne]; UILongPressGestureRecognizer *longpress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(onlongtabed:)];
longpress.minimumPressDuration = 1.0;///至少按1秒
longpress.numberOfTouchesRequired = ;//只有一个触点
[self.view addGestureRecognizer:longpress];
[longpress release]; UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:@"button" forState:UIControlStateNormal];
button.frame = CGRectMake(, , , );
[button addTarget:self action:@selector(onbuttonclick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button]; UITapGestureRecognizer *singleTwo = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onviewtabed:)];
singleTwo.numberOfTouchesRequired = ;
singleTwo.numberOfTapsRequired = ;
singleTwo.delegate = self; self.imageview = [[UIImageView alloc] init];
imageview.frame = CGRectMake(, , , );
imageview.image = [UIImage imageNamed:@"test.png"];
imageview.tag = ;
[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

使用UIGestureRecognizer监听屏幕事件的更多相关文章

  1. [Swift通天遁地]三、手势与图表-(1)监听屏幕上触摸事件的各种状态

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  2. 全局监听SCREEN_ON和SCREEN_OFF的替代方法--监听屏幕解锁事件

    在做一个程序的时候,需要时刻保持某一服务是启动的,因此想到了通过监听屏幕SCREEN_ON和SCREEN_OFF这两个action.奇怪的是,这两个action只能通过代码的形式注册,才能被监听到,使 ...

  3. IOS监听屏幕状态

    一.定义两个宏   //锁屏通知 #define NotificationOff CFSTR("com.apple.springboard.lockcomplete")   //解 ...

  4. JS 获取和监听屏幕方向变化(portrait / landscape)

    移动设备的屏幕有两个方向: landscape(横屏)和portrait(竖屏),在某些情况下需要获取设备的屏幕方向和监听屏幕方向的变化,因此可以使用Javascript提供的 MediaQueryL ...

  5. Android监听屏幕解锁和判断屏幕状态

    开发后台服务的时候经常需要对屏幕状态进行判断,如果是想要监听屏幕解锁事件,可以在配置里面注册action为 android.intent.action.USER_PRESENT的广播,则可以监听解锁事 ...

  6. pc端监听屏幕实现导航固定定位

    要点:占位符 js,监听屏幕滚动事件,当滚动条距离浏览器顶部的距离 大于 要固定定位开始以下元素的距离,则给要固定元素添加fixed样式. 初始化方法时,要给占位符添加样式 function sort ...

  7. [Swift通天遁地]三、手势与图表-(2)监听手势事件自由拖动图像视图

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  8. [JS]笔记12之事件机制--事件冒泡和捕获--事件监听--阻止事件传播

    -->事件冒泡和捕获-->事件监听-->阻止事件传播 一.事件冒泡和捕获 1.概念:当给子元素和父元素定义了相同的事件,比如都定义了onclick事件,点击子元素时,父元素的oncl ...

  9. JS 中的事件绑定、事件监听、事件委托

    事件绑定 要想让 JavaScript 对用户的操作作出响应,首先要对 DOM 元素绑定事件处理函数.所谓事件处理函数,就是处理用户操作的函数,不同的操作对应不同的名称. 在JavaScript中,有 ...

随机推荐

  1. UVa 821 Page Hopping【Floyd】

    题意:给出一个n个点的有向图,任意两个点之间都相互到达,求任意两点间最短距离的平均值 因为n很小,所以可以用floyd 建立出图,然后用floyd,统计d[][]不为0且不为INF的边的和及条数,就可 ...

  2. BZOJ 1103 大都市

    dfs序+BIT. #include<iostream> #include<cstdio> #include<cstring> #include<algori ...

  3. 制作自己的Cydia发布源

    http://patrickmuff.ch/blog/2013/02/15/create-your-own-cydia-repository-on-ubuntu/ http://www.saurik. ...

  4. HDU 5273 Dylans loves numbers(水题)

    题意:给出一个0≤N≤1018,求其二进制中有几处是具有1的,假设相连的1只算1处,比如1101011就是3处. 思路:一个个数,当遇到第一个1时就将flag置为1:当遇到0就将flag置为0.当遇到 ...

  5. BPMN这点事-BPMN扩展元素

    什么是BPMN扩展元素?我们为什么要从BPMN元素中界定出一个扩展元素的子集?BPMN扩展元素是我们平时使用频率不高的BPMN元素,这些元素更多的面向开发人员而不是业务人员,它们强调流程执行的细节,例 ...

  6. Spark RDD操作(1)

    https://www.zybuluo.com/jewes/note/35032 RDD是什么? RDD是Spark中的抽象数据结构类型,任何数据在Spark中都被表示为RDD.从编程的角度来看,RD ...

  7. HDU 5038 Grade

    解题思路:这题最关键的是要读懂题意,If not all the value are the same but the frequencies of them are the same, there ...

  8. 配置Linux 11G R2 RAC NTP服务

    安装Oracle 11g RAC时,我们需要配置ntp服务.在使用虚拟机的情况下对于时钟同步方式的配置有很多种方式,可以使用vmware自带的时钟同步功能,也可以直接将本地的一个节点用作时间服务器.本 ...

  9. AJAX异步调用

    前台: function readygo(v) {            $.ajax({                type: "post",                ...

  10. 部署K2 Blackpearl流程时出错(与基础事务管理器的通信失败或Communication with the underlying transaction manager has failed.

    转:http://www.cnblogs.com/dannyli/archive/2011/12/01/2270222.html 亲,在部署K2流程是,是否遇到这个错误(以下是中.英文错误信息) 中文 ...