iOS開發(fā)之一些實(shí)用小知識(shí)點(diǎn)總結(jié)
話不多說(shuō),直接進(jìn)主題
一、防止UIButton,cell等重復(fù)點(diǎn)擊
主要是快速點(diǎn)擊button或者cell,所對(duì)應(yīng)的action或者邏輯會(huì)走多次,例如:點(diǎn)擊button或者cell調(diào)用撥打電話的方法,會(huì)彈出撥打電話框好多次;這個(gè)對(duì)用戶不太友好;問(wèn)了下哥們兒,他給了個(gè)宏,目前算是解決這個(gè)問(wèn)題;代碼如下:
// 防止多次調(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è)置一個(gè)bool變量,記錄一下,延時(shí)更改下變量的值;使用:在所需要的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) { /// 保存到本地相冊(cè) UIImageWriteToSavedPhotosAlbum(viewImage, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL); } }
保存圖片的回調(diào)處理
- (void)image:(UIImage*)image didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo{ if (error) { NSLog(@"保存失敗,請(qǐng)重試"); } else { NSLog(@"保存成功"); } }
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,本文還有許多不足,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
簡(jiǎn)述iOS屬性中的內(nèi)存管理參數(shù)
這篇文章主要介紹了簡(jiǎn)述iOS屬性中的內(nèi)存管理參數(shù) 的相關(guān)資料,需要的朋友可以參考下2018-02-02iOS之點(diǎn)擊通知欄的通知進(jìn)入程序的觸發(fā)事件
本文主要介紹了iOS中點(diǎn)擊通知欄的通知進(jìn)入程序的觸發(fā)事件的相關(guān)知識(shí),具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-02-02分享一個(gè)關(guān)于Storyboard 跳轉(zhuǎn)與傳值
近日不忙,給大家分享一個(gè)關(guān)于storyboard跳轉(zhuǎn)傳值的相關(guān)知識(shí),感興趣的朋友一起看看吧2015-12-12iOS簡(jiǎn)單到無(wú)門檻調(diào)試WebView的步驟詳解
這篇文章主要給大家介紹了關(guān)于iOS調(diào)試WebView的相關(guān)資料,文中介紹的方法可以說(shuō)是非常簡(jiǎn)單,簡(jiǎn)單到無(wú)門檻,通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07iOS App開發(fā)中Masonry布局框架的基本用法解析
這篇文章主要介紹了iOS App開發(fā)中Masonry布局框架的基本用法解析,Masonry支持iOS和OSX的Auto Layout,在GitHub上的人氣很高,需要的朋友可以參考下2016-03-03