iOS 响应点击(单击或双击)scrollview空白处事件 使用NSNotification
2012-06-14 14:23
756 查看
我们时常需要在软件中点击空白处然后显示或者隐藏导航栏,工具栏,Tab时。如果 在UIView中这个很容易实现,将view的Custom Class 由UIVIew更改为UIControl,就可以发现View和Button一样拥有了事件响应,但是在UIScrollView中该方法就行不通了,这时就需要使用NSNotification在类与类之间实现通信。
首先我们需要重写UIScrollView,也即继承它一下
分别时头文件和.m文件
#import <UIKit/UIKit.h> @interface myScrollView : UIScrollView{ } @end
在scrollview双击事件发生的时候发送notification,而且标识其为BNR(随便起个名字而已);
#import "myScrollView.h" @implementation myScrollView - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ UITouch *touch = [touches anyObject]; if (2 == [touch tapCount]) {//响应双击事件 NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; NSLog(@"nc send!"); [nc postNotificationName:@"BNR" object:self];
//发送notification消息 } } @end
在viewController类中注册一下,此前已经定义并且连接了myScrollView
IBOutlet myScrollView *scrollView;
注册一个notification
- (void)viewDidLoad{ scrollView.contentSize = CGSizeMake(320, 1000); UIButton *bt = [UIButton buttonWithType:UIButtonTypeRoundedRect]; bt.frame = CGRectMake(1, 0, 100, 50); [bt setTitle:@"Normal" forState:UIControlStateNormal]; [scrollView addSubview:bt]; NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; [nc addObserver:self selector:@selector(doubleClicked) //响应scrollview被双击的函数 name:@"BNR" object:nil];
//注册notification NSLog(@"Observer!"); }然后在另一个类中调用函数doubleClicked,这样就能在另一个类中响应scrollView被双击的事件了,加入相应代码就能实现显示或者隐藏导航栏和Tab的功能。
- (void)doubleClicked{ NSLog(@"Succed to pass double click!"); bShowNavAndTab = !bShowNavAndTab; self.navigationController.navigationBarHidden = bShowNavAndTab; NSArray *views = [self.tabBarController.view subviews]; for (id v in views) { if ([v isKindOfClass:[UITabBar class]]) { [(UITabBar *) v setHidden:bShowNavAndTab]; } } }
相关文章推荐
- android webview js交互, 响应webview中的图片点击事件 ,点击(双击)使图片变大,单击退出
- iOS touch事件区分单击双击响应的方法
- iOS touch事件单击双击区分响应
- ios中控件同时响应单击和双击事件以及多击事件
- IOS 集成百度地图 自定义显示标签 点击事件不响应
- iOS开发:UILabel无法响应点击事件的问题
- ios学习笔记:UIAlertView使用和监听点击事件
- iOS UITableView单元格单击出现延迟,双击正常执行事件问题
- iOS 中 点击事件响应的方法hitTest与pointInside
- iOS 点击事件传递及响应
- ios 手势点击子视图的时候不响应父视图的点击事件
- iOS 百度地图api点击BMKAnnotationView,不响应didSelectAnnotationView事件。
- android 点击button 弹出一个listview并且响应单击事件
- Android 横屏启动activity,点击屏幕的单击、双击和长按事件
- 关于listfragment中使用listview的点击事件不响应
- 扩展GridView(六)——数据行响应鼠标的单击和双击事件
- iOS 在添加了UITapGestureRecognizer的UIView上响应按钮的点击事件
- iOS开发中两层view上的button不响应点击事件
- iOS巅峰之解决隐藏tabbar后原位置无法响应点击事件的问题
- iOS开发之解决隐藏tabbar后原位置无法响应点击事件的问题