这篇文章主要介绍iOS开发中实用小知识点有哪些,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
一、防止UIButton,cell等重复点击
主要是快速点击button或者cell,所对应的action或者逻辑会走多次,例如:点击button或者cell调用拨打电话的方法,会弹出拨打电话框好多次;这个对用户不太友好;问了下哥们儿,他给了个宏,目前算是解决这个问题;代码如下:
// 防止多次调用 #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前调用即可:
kPreventRepeatClickTime(0.5);
二、获取当前视图最顶层的ViewController
获取当前视图最顶层的ViewController
+ (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:
/// 截屏 - (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); } }
保存图片的回调处理
- (void)image:(UIImage*)image didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo{ if (error) { NSLog(@"保存失败,请重试"); } else { NSLog(@"保存成功"); } }
以上是“iOS开发中实用小知识点有哪些”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。