iOS开发实战 - 完美解决UIScrollView嵌套滑动手势冲突
我们应该都有用过这个功能,你的朋友微信给你分享了一个淘宝里面的商品链接,然后当你复制这个链接打开淘宝APP的时候,就会弹出一个弹窗,像这样:
example.PNG
这个功能想必大家都挺熟悉,受这个启发我们产品也想在我们APP上添加这样一个功能,与这个不一样的是,当我们复制一段网址的时候打开我们的APP会弹出框填一些信息后上传到我们的“资源库”。大体功能就这样,所以记录一下实现的过程。
一、弹窗视图功能
.h中:两个信号一个是确定信号一个是取消信号
两个方法,一个显示一个隐藏方法
1 2 3 4 5 | @property (nonatomic, strong) RACSubject *uploadSureSignal; //确定上传信号 @property (nonatomic, strong) RACSubject *hideSucSignal; //隐藏 - ( void )show; - ( void )hide; |
.m中:主要是两个textview,还有涉及到在keywindow上,IQKeyboard的一些操作
1 2 3 | @property (nonatomic, assign) CGFloat keyboardHeight; //键盘高度 @property (nonatomic, strong) CustomUITextView *nameTV; @property (nonatomic, strong) CustomUITextView *desTV; |
因为发现IQKeyboard在这个弹出界面有问题,所以在显示这个界面的时候,将IQKeyboard禁用取之使用系统的keyboard监听方法
在(void)show方法中:
1 2 3 4 5 6 7 8 | -( void )show { //键盘通知 NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter]; [defaultCenter addObserver:self selector:@selector(keyboardWillShowOrHide:) name:UIKeyboardWillShowNotification object:nil]; [defaultCenter addObserver:self selector:@selector(keyboardWillShowOrHide:) name:UIKeyboardWillHideNotification object:nil]; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | //监听方法 - ( void )keyboardWillShowOrHide:(NSNotification *)notification { //获取通知名 NSString *notificationName = notification.name; //获取通知内容 NSDictionary *keyboardInfo = notification.userInfo; //键盘弹出时,让画面整体稍稍上移,并伴随动画 //键盘回收时反之 CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; CGFloat height = keyboardFrame.size.height; self.keyboardHeight = height; //动画结束后self.view的frame值 CGRect selfViewFrame = self.bgView.frame; //通过通知名字判断弹出还是回收 if ([notificationName isEqualToString:UIKeyboardWillShowNotification]) { selfViewFrame.origin.y = SCREEN_HEIGHT - PANELHEIGHT - height; } else { selfViewFrame.origin.y = SCREEN_HEIGHT - PANELHEIGHT; } //取出动画时长 NSTimeInterval duration = [keyboardInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; //使用动画更改self.view.frame [UIView animateWithDuration:duration animations:^{ //这里填入一些view的最终状态属性设置,即会自动产生过渡动画 self.bgView.frame = selfViewFrame; }]; } |
同时在show方法中显示keyWindow,进而改变界面的frame进行显示
1 2 3 4 5 6 7 8 9 10 | - ( void )show { UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow; [keyWindow addSubview:self]; CGRect frame = self.bgView.frame; if (frame.origin.y == SCREEN_HEIGHT) { frame.origin.y = SCREEN_HEIGHT - PANELHEIGHT; [UIView animateWithDuration: 0.4 animations:^{ self.bgView.frame = frame; }]; } |
hide方法这里要考虑到键盘弹出后将self.bgView向上提高后frame的变化。
1 2 3 4 5 6 7 8 9 10 11 12 13 | CGRect selfFrame = self.bgView.frame; if (selfFrame.origin.y == SCREEN_HEIGHT - PANELHEIGHT || selfFrame.origin.y == SCREEN_HEIGHT - PANELHEIGHT - self.keyboardHeight) { [self resignFirstResponder]; selfFrame.origin.y = SCREEN_HEIGHT; [UIView animateWithDuration: 0.4 animations:^{ self.bgView.frame = selfFrame; } completion:^(BOOL finished) { [IQKeyboardManager sharedManager].enable = YES; [[NSNotificationCenter defaultCenter] removeObserver:self]; // [self.hideSucSignal sendNext:nil]; [self removeFromSuperview]; }]; } |
delegate中的操作
这里首先要弄懂APPdelegate中的这几个代理方法的意思:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | //App已经启动 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. return YES; } //App挂起状态 - ( void )applicationWillResignActive:(UIApplication *)application { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. } //APP进入后台 - ( void )applicationDidEnterBackground:(UIApplication *)application { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } //APP将重新回到前台 - ( void )applicationWillEnterForeground:(UIApplication *)application { // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. } //APP进入活跃状态 - ( void )applicationDidBecomeActive:(UIApplication *)application { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } //系统时间发生改变时执行 - ( void )applicationWillTerminate:(UIApplication *)application { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } |
在上面的这些代理方法中,我们需要用到的是 applicationDidBecomeActive方法。在这个方法中我们去检查系统的粘贴板UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | if (pasteboard.string) { NSLog(@ "string:%@" , pasteboard.string); NSString *urlStr = pasteboard.string; //如果粘贴板中的字符串包含https或http字段,我们去检查当前的控制器 如果当前的控制器是我们弹出做操作的控制器的话 isPopVC = NO; BOOL isPopVC = NO; UIViewController * Rootvc = self.window.rootViewController; if ([Rootvc isKindOfClass:[UINavigationController class ]]) { UINavigationController * nav = (UINavigationController *)Rootvc; UIViewController * v = [nav.viewControllers lastObject]; if ([v isKindOfClass:[UploadResCofingVC class ]]) { isPopVC = YES; } } //如果popView == nil 并且isPopVC == NO 弹出popView弹窗视图 进行操作 if (!self.popView && !isPopVC) { UploadResourcesPopupView *popView = [UploadResourcesPopupView new ]; [popView show]; self.popView = popView; [self.popView.hideSucSignal subscribeNext:^(id x) { @strongify(self); self.popView = nil; }]; } } } } |
总结
以上大体就是实现这个功能的基本思路,细节方面因项目而异了,比如我们需要判断当前用户的角色,当前用户是否登录,对弹窗视图后续的一些操作。当然并不完美,欢迎批评指正。
转自:http://www.cocoachina.com/ios/20180508/23307.html

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
仿抖音注册Dialog实现
今天将仿抖音注册界面完成后接着便将注册界面实现了,这里利用的是Dialog实现的 效果图 接下来介绍如何实现底部弹出Dialog 1.首先编写动画文件 <!--tranlate_dialog_in.xml--> <?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="200" android:fromXDelta="0" android:fromYDelta="100%" android:toXDelta="0" android:toYDelta="0"> </translate> <!--tranlate_dialog_out.xml--> <?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android...
- 下一篇
Android适配8.0遇到的一些坑
刚进新公司的时候华为市场对APP提示的是不兼容android8.0,查看了下原代码发现项目基本还停留在6.0的适配上,所以就大致适配了7.0、8.0,先记录几个主要的问题点,后续更新。首先必须的把SdkVersion提升到26把(华为市场提示不兼容8.0也是这个原因),然后google了一圈。 大致google+百度了一大圈,基本锁定会导致程序异常+功能失效的几个点。 ①:通知(会收不到通知) ②:悬浮窗(7.0、8.0)(会崩溃) ③:自适应图标(图标终于有所关注) 一:通知渠道 Android O版本对通知做了规范性的控制,强制用户在发送通知的时候,对通知进行系统性的管理,新增了channel渠道功能,貌似Android P版本对与这一点也做了强调,使用户能够更好的管理通知,做到有目的性的屏蔽通知。下面上相关性的代码 NotificationChannel mChannel =new NotificationChannel("channel_01","消息推送", NotificationManager.IMPORTANCE_DEFAULT); notificationManage...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- CentOS7,8上快速安装Gitea,搭建Git服务器
- CentOS8安装MyCat,轻松搞定数据库的读写分离、垂直分库、水平分库
- SpringBoot2整合Thymeleaf,官方推荐html解决方案
- CentOS7,CentOS8安装Elasticsearch6.8.6
- CentOS关闭SELinux安全模块
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- Eclipse初始化配置,告别卡顿、闪退、编译时间过长
- SpringBoot2整合Redis,开启缓存,提高访问速度
- CentOS7设置SWAP分区,小内存服务器的救世主