iOS 响应点击(单击或双击)scrollview空白处事件 使用NSNotification
来源:互联网 发布:网页游戏源码下载 编辑:程序博客网 时间:2024/04/30 04:50
我们时常需要在软件中点击空白处然后显示或者隐藏导航栏,工具栏,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]; } }}
- iOS 响应点击(单击或双击)scrollview空白处事件 使用NSNotification
- 解决 点击ListView空白处事件不响应问题
- 解决 点击ListView空白处事件不响应问题
- 关于AdapterView点击空白处事件
- iOS touch事件单击双击区分响应
- VC中CTreeContrl实现响应单击或双击事件
- android webview js交互, 响应webview中的图片点击事件 ,点击(双击)使图片变大,单击退出
- 按钮单击双击响应事件
- Swing之table中响应鼠标双击(单击)事件
- iOS点击空白处或点击背景收起键盘
- iOS点击空白处或点击背景收起键盘
- ios 单击和双击手势
- 给GridView双击,单击行事件
- 屏蔽按键快速双击,只响应单击
- MFC 鼠标双击响应单击事件
- Qt 单击和双击事件响应实现
- MFC picture control响应单击、双击、右键
- ios中控件同时响应单击和双击事件以及多击事件
- 对n个不同正数进行排列,一共有多少种排列方式
- IOS电影播放器—MPMoviePlayerController
- HTML5 data-* attributes are great and you know it
- 例解 autoconf 和 automake 生成 Makefile 文件
- hashCode() 和 equals() 理解
- iOS 响应点击(单击或双击)scrollview空白处事件 使用NSNotification
- poj2352树状数组入门
- android 信息(mms)的故事(三) -- 发短信
- 13.4 抽象类CSolid 及派生 (空间立体图形)
- android人脸识别技术浅析
- 动态性能视图
- PPT Design收录
- android 获取uri的正确文件路径的办法
- 黑马程序员--集合总结(二)