我是靠谱客的博主 危机热狗,这篇文章主要介绍iOS开发之一些实用小知识点总结,现在分享给大家,希望可以做个参考。

话不多说,直接进主题

一、防止UIButton,cell等重复点击

主要是快速点击button或者cell,所对应的action或者逻辑会走多次,例如:点击button或者cell调用拨打电话的方法,会弹出拨打电话框好多次;这个对用户不太友好;问了下哥们儿,他给了个宏,目前算是解决这个问题;代码如下:

复制代码
1
2
3
4
5
6
7
8
// 防止多次调用 #define kPreventRepeatClickTime(_seconds_) static BOOL shouldPrevent; if (shouldPrevent) return; shouldPrevent = YES; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)((_seconds_) * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ shouldPrevent = NO; });

总的思路是设置一个bool变量,记录一下,延时更改下变量的值;使用:在所需要的button或者cell的action前调用即可:

复制代码
1
kPreventRepeatClickTime(0.5);

二、获取当前视图最顶层的ViewController

获取当前视图最顶层的ViewController

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
+ (UIViewController *)currentViewController { UIWindow * window = [[UIApplication sharedApplication] keyWindow]; if (window.windowLevel != UIWindowLevelNormal){ NSArray *windows = [[UIApplication sharedApplication] windows]; for(UIWindow * tmpWin in windows){ if (tmpWin.windowLevel == UIWindowLevelNormal){ window = tmpWin; break; } } } UIViewController *currentVC = window.rootViewController; while (currentVC.presentedViewController) { currentVC = currentVC.presentedViewController; } if ([currentVC isKindOfClass:[UITabBarController class]]) { currentVC = [(UITabBarController *)currentVC selectedViewController]; } if ([currentVC isKindOfClass:[UINavigationController class]]) { currentVC = [(UINavigationController *)currentVC topViewController]; } return currentVC; }

三、代码截图相关

截取指定的View:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/// 截屏 - (void)actionForScreenShotWith:(UIView *)aimView savePhoto:(BOOL)savePhoto { if (!aimView) return; UIGraphicsBeginImageContextWithOptions(aimView.bounds.size, NO, 0.0f); [aimView.layer renderInContext: UIGraphicsGetCurrentContext()]; UIImage* viewImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); if (savePhoto) { /// 保存到本地相册 UIImageWriteToSavedPhotosAlbum(viewImage, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL); } }

保存图片的回调处理

复制代码
1
2
3
4
5
6
7
- (void)image:(UIImage*)image didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo{ if (error) { NSLog(@"保存失败,请重试"); } else { NSLog(@"保存成功"); } }

总结

以上就是这篇文章的全部内容了,本文还有许多不足,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对靠谱客的支持。

最后

以上就是危机热狗最近收集整理的关于iOS开发之一些实用小知识点总结的全部内容,更多相关iOS开发之一些实用小知识点总结内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(120)

评论列表共有 0 条评论

立即
投稿
返回
顶部