iOS開發(fā)之一些實用小知識點總結(jié)
話不多說,直接進主題
一、防止UIButton,cell等重復(fù)點擊
主要是快速點擊button或者cell,所對應(yīng)的action或者邏輯會走多次,例如:點擊button或者cell調(diào)用撥打電話的方法,會彈出撥打電話框好多次;這個對用戶不太友好;問了下哥們兒,他給了個宏,目前算是解決這個問題;代碼如下:
// 防止多次調(diào)用 #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; \ }); \
總的思路是設(shè)置一個bool變量,記錄一下,延時更改下變量的值;使用:在所需要的button或者cell的action前調(diào)用即可:
kPreventRepeatClickTime(0.5);
二、獲取當(dāng)前視圖最頂層的ViewController
獲取當(dāng)前視圖最頂層的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; }
三、代碼截圖相關(guān)
截取指定的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); } }
保存圖片的回調(diào)處理
- (void)image:(UIImage*)image didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo{ if (error) { NSLog(@"保存失敗,請重試"); } else { NSLog(@"保存成功"); } }
總結(jié)
以上就是這篇文章的全部內(nèi)容了,本文還有許多不足,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
分享一個關(guān)于Storyboard 跳轉(zhuǎn)與傳值
近日不忙,給大家分享一個關(guān)于storyboard跳轉(zhuǎn)傳值的相關(guān)知識,感興趣的朋友一起看看吧2015-12-12iOS App開發(fā)中Masonry布局框架的基本用法解析
這篇文章主要介紹了iOS App開發(fā)中Masonry布局框架的基本用法解析,Masonry支持iOS和OSX的Auto Layout,在GitHub上的人氣很高,需要的朋友可以參考下2016-03-03