溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

iOS開(kāi)發(fā)中實(shí)用小知識(shí)點(diǎn)有哪些

發(fā)布時(shí)間:2021-08-04 13:40:13 來(lái)源:億速云 閱讀:98 作者:小新 欄目:移動(dòng)開(kāi)發(fā)

這篇文章主要介紹iOS開(kāi)發(fā)中實(shí)用小知識(shí)點(diǎn)有哪些,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

一、防止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(@"保存成功");
 }
}

以上是“iOS開(kāi)發(fā)中實(shí)用小知識(shí)點(diǎn)有哪些”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

ios
AI