iOS开发实用知识点 iOS开发之一些实用小知识点总结

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

iOS开发实用知识点 iOS开发之一些实用小知识点总结

开发仔XG   2021-03-29 我要评论
想了解iOS开发之一些实用小知识点总结的相关内容吗,开发仔XG在本文为您仔细讲解iOS开发实用知识点的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:ios,防止cell重复点击,ios,viewcontroller,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(@"保存成功");
 }
}

总结

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

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们