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文章

iOS开发WKWebView白屏问题

2020-1-8 16:22:09

iOS文章

iOS开发多线程以及线程的创建方法

2020-1-8 16:37:51

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索