亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

iOS開發(fā)之一些實(shí)用小知識(shí)點(diǎn)總結(jié)

 更新時(shí)間:2017年10月20日 10:59:09   作者:開發(fā)仔XG  
這篇文章主要給大家介紹了關(guān)于iOS開發(fā)之實(shí)用小知識(shí)點(diǎn)的相關(guān)資料,其中包括防止UIButton,cell等重復(fù)點(diǎn)擊、獲取當(dāng)前視圖最頂層的ViewController以及代碼截圖相關(guān)的等知識(shí),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。

話不多說,直接進(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ì)用戶不太友好;問了下哥們兒,他給了個(gè)宏,目前算是解決這個(gè)問題;代碼如下:

// 防止多次調(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à)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

最新評(píng)論