iOS雜碎2

AVAudio 參考

1.1 AVAudioSessionCategory 幾個(gè)選項(xiàng)參考:

AVAudioSessionCategoryAmbient 只用于播放音樂時(shí),并且可以和QQ音樂同時(shí)播放,比如玩游戲的時(shí)候還想聽QQ音樂的歌,那么把游戲播放背景音就設(shè)置成這種類別。同時(shí),當(dāng)用戶鎖屏或者靜音時(shí)也會(huì)隨著靜音,這種類別基本使用所有App的背景場景。

AVAudioSessionCategorySoloAmbient 也是只用于播放,但是和"AVAudioSessionCategoryAmbient"不同的是,用了它就別想聽QQ音樂了,比如不希望QQ音樂干擾的App,類似節(jié)奏大師。同樣當(dāng)用戶鎖屏或者靜音時(shí)也會(huì)隨著靜音,鎖屏了就玩不了節(jié)奏大師了。

AVAudioSessionCategoryPlayback 如果鎖屏了還想聽聲音怎么辦?用這個(gè)類別,比如App本身就是播放器,同時(shí)當(dāng)App播放時(shí),其他類似QQ音樂就不能播放了。所以這種類別一般用于播放器類App

AVAudioSessionCategoryRecord 有了播放器,肯定要錄音機(jī),比如微信語音的錄制,就要用到這個(gè)類別,既然要安靜的錄音,肯定不希望有QQ音樂了,所以其他播放聲音會(huì)中斷。想想微信語音的場景,就知道什么時(shí)候用他了。

AVAudioSessionCategoryPlayAndRecord如果既想播放又想錄制該用什么模式呢?比如VoIP,打電話這種場景,PlayAndRecord就是專門為這樣的場景設(shè)計(jì)的 。

AVAudioSessionCategoryMultiRoute想象一個(gè)DJ用的App,手機(jī)連著HDMI到揚(yáng)聲器播放當(dāng)前的音樂,然后耳機(jī)里面播放下一曲,這種常人不理解的場景,這個(gè)類別可以支持多個(gè)設(shè)備輸入輸出。

AVAudioSessionCategoryAudioProcessing主要用于音頻格式處理,一般可以配合AudioUnit進(jìn)行使用
更多詳見

通過設(shè)置activeYES激活Session,設(shè)置為NO解除Session的激活狀態(tài)。BOOL返回值表示是否成功,如果失敗的話可以通過NSErrorerror.localizedDescription查看出錯(cuò)原因。

- (BOOL)setActive:(BOOL)active  error:(NSError * _Nullable *)outError;

注意: 因?yàn)?code>AVAudioSession會(huì)影響其他App的表現(xiàn),當(dāng)自己App的Session被激活,其他App的就會(huì)被解除激活,如何要讓自己的Session解除激活后恢復(fù)其他App Session的激活狀態(tài)呢?

- (BOOL)setActive:(BOOL)active withOptions:(AVAudioSessionSetActiveOptions)options error:(NSError * _Nullable *)outError;

這里的options傳入 AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation 即可。 當(dāng)然,也可以通過otherAudioPlaying變量來提前判斷當(dāng)前是否有其他App在播放音頻。

2.1 AVAudioSessionMode

AVAudioSessionModeDefault 每種類別默認(rèn)的就是這個(gè)模式,所有要想還原的話,就設(shè)置成這個(gè)模式。

AVAudioSessionModeVoiceChat主要用于VoIP場景,此時(shí)系統(tǒng)會(huì)選擇最佳的輸入設(shè)備,比如插上耳機(jī)就使用耳機(jī)上的麥克風(fēng)進(jìn)行采集。此時(shí)有個(gè)副作用,他會(huì)設(shè)置類別的選項(xiàng)為AVAudioSessionCategoryOptionAllowBluetooth從而支持藍(lán)牙耳機(jī)。

AVAudioSessionModeVideoChat 主要用于視頻通話,比如QQ視頻、FaceTime。時(shí)系統(tǒng)也會(huì)選擇最佳的輸入設(shè)備,比如插上耳機(jī)就使用耳機(jī)上的麥克風(fēng)進(jìn)行采集并且會(huì)設(shè)置類別的選項(xiàng)為

AVAudioSessionCategoryOptionAllowBluetoothAVAudioSessionCategoryOptionDefaultToSpeaker
AVAudioSessionModeGameChat 適用于游戲App的采集和播放,比如“GKVoiceChat”對(duì)象,一般不需要手動(dòng)設(shè)置

坐標(biāo)轉(zhuǎn)換

/// 將像素point由point所在視圖轉(zhuǎn)換到目標(biāo)視圖view中,返回在目標(biāo)視圖view中的像素值
- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view;
 
/// 將像素point從view中轉(zhuǎn)換到當(dāng)前視圖中,返回在當(dāng)前視圖中的像素值
- (CGPoint)convertPoint:(CGPoint)point fromView:(UIView *)view;
 
/// 將rect由rect所在視圖轉(zhuǎn)換到目標(biāo)視圖view中,返回在目標(biāo)視圖view中的rect
- (CGRect)convertRect:(CGRect)rect toView:(UIView *)view;
 
/// 將rect從view中轉(zhuǎn)換到當(dāng)前視圖中,返回在當(dāng)前視圖中的rect
- (CGRect)convertRect:(CGRect)rect fromView:(UIView *)view;

plist文件轉(zhuǎn)json文件

plutil -convert json name.plist -o name.json

Label文字后面添加圖片

NSTextAttachment *attch = [[NSTextAttachment alloc] init];
attch.image = [UIImage imageNamed:@"imagename"];
attch.bounds = CGRectMake(10, -1, 7, 12);
NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:attch];
            
NSString *text = targetPlan.title;
NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:text];
[attri addAttribute:NSForegroundColorAttributeName value:RGBA(74,74,91,1) range:NSMakeRange(0, attri.length)];
[attri addAttribute:NSFontAttributeName value:PingFangRegularFont(15) range:NSMakeRange(0, attri.length)];
 [attri insertAttributedString:string atIndex:text.length];
self.titleLabel.attributedText = attri;

OC注入JS失敗

錯(cuò)誤描述:

Error Domain=WKErrorDomain Code=4 "A JavaScript exception occurred" UserInfo={WKJavaScriptExceptionLineNumber=1, WKJavaScriptExceptionMessage=ReferenceError: Can't find variable: web, WKJavaScriptExceptionColumnNumber=4, WKJavaScriptExceptionSourceURL=about:blank, NSLocalizedDescription=A JavaScript exception occurred}

錯(cuò)誤原因: 數(shù)組轉(zhuǎn)json字符串時(shí)options選成NSJSONWritingPrettyPrinted 而不是kNilOptions導(dǎo)致,下面是正確的注入方法:

// 數(shù)組轉(zhuǎn)化成JSON字符串
NSData *data = [NSJSONSerialization dataWithJSONObject:array options:kNilOptions error:nil];
NSString *paraStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSString *method = [NSString stringWithFormat:@"%@('%@')", @"web.getSmallWeightDate", paraStr];
[webView evaluateJavaScript:method completionHandler:^(id _Nullable response, NSError * _Nullable error) {
}];

修改textView或者textField 光標(biāo)的大小

繼承自UITextView 重寫父類方法


@implementation CustomTextView

- (CGRect)caretRectForPosition:(UITextPosition *)position {
    
    CGRect originalRect = [super caretRectForPosition:position];
    originalRect.size.height = self.font.lineHeight - 8;
    originalRect.size.width = 2;
    return originalRect;
}

@end

NSPredicate用法之一: 數(shù)組元素為對(duì)象時(shí),通過對(duì)象的某個(gè)屬性快速篩選出數(shù)組中的對(duì)象

// 找出tips和年報(bào)的模型
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"type == 1001 OR type == 1002"];
NSArray *temp = [self.dataArray filteredArrayUsingPredicate:predicate];
if (temp.count > 0) {
   _hasTips = YES;
} else {
    _hasTips = NO;
}

關(guān)于NSPredicate更多高級(jí)用法,參見:iOS NSPredicate 使用詳解
iOS中的謂詞(NSPredicate)使用

SD圖片加載漸現(xiàn)效果

[_ImageView sd_setImageWithURL:url placeholderImage:placeholderImage options:YYWebImageOptionAllowBackgroundTask progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
    dispatch_sync(dispatch_get_main_queue(), ^(){
       [weakSelf fadeLayer:weakSelf.ImageView.layer];
    });
} completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
}];

- (void)fadeLayer:(CALayer *)layer {
    CATransition *transition = [CATransition animation];
    transition.duration = 0.5;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionFade;
    [layer addAnimation:transition forKey:@"fade"];
}

使用pandoc為markdown生成大綱

pandoc 2.5
Compiled with pandoc-types 1.17.5.4, texmath 0.11.1.2, skylighting 0.7.4
Default user data directory: /Users/wanghaobing/.pandoc
Copyright (C) 2006-2018 John MacFarlane
Web:  http://pandoc.org
  • 3.cd到存放.md文件的文件夾目錄
    1. 輸入pandoc -s --toc README.md -o README.md命令,即可自動(dòng)生成目錄 (使用該命令默認(rèn)生成三級(jí)目錄)
      如果想要生成4級(jí)目錄,可以使用pandoc -s --toc --toc-depth=4 README.md -o README.md命令 (README.md可以修改為你的markdown文件名)

WKWebView頭部添加自定義View, 隨Webview一起滾動(dòng)

WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) configuration:configuration];
webView.navigationDelegate = self;
webView.multipleTouchEnabled = YES;
webView.userInteractionEnabled = YES;
webView.contentMode = UIViewContentModeScaleAspectFit;
webView.scrollView.scrollEnabled = YES;
webView.scrollView.delegate = self;
        
// 調(diào)整contentInset,將自定義視圖添加到webView.scrollView
webView.scrollView.contentInset = UIEdgeInsetsMake(ScreenWidth, 0, 0, 0);

CustomView *customView = [[CustomView alloc] initWithFrame:CGRectMake(0, -ScreenWidth, ScreenWidth, ScreenWidth)]; //注意y值.
[webView.scrollView addSubview: customView];

[self.view addSubview:webView];

獲取當(dāng)天,當(dāng)周,當(dāng)月,當(dāng)年的時(shí)間區(qū)間

double interval = 0;
NSDate *beginDate = nil;
NSDate *endDate = nil;
    
NSCalendar *calendar = [NSCalendar currentCalendar];
[calendar setFirstWeekday:2];//設(shè)定周一為周首日
BOOL exist = [calendar rangeOfUnit:NSCalendarUnitWeekOfYear startDate:&beginDate interval:&interval forDate:[NSDate date]];
// unit可分別修改為 NSCalendarUnitDay NSCalendarUnitWeekOfYear NSCalendarUnitMonth NSCalendarUnitYear
if (exist) {
   endDate = [beginDate dateByAddingTimeInterval:interval-1];
}else {
   return [NSArray array];
}
NSDateFormatter *myDateFormatter = [[NSDateFormatter alloc] init];
[myDateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *beginString = [myDateFormatter stringFromDate:beginDate];
NSString *endString = [myDateFormatter stringFromDate:endDate];
    
NSString *section = [NSString stringWithFormat:@"%@ ~ %@",beginString,endString];
NSLog(@"%@",section); // 2018-11-12 00:00:00 ~ 2018-11-18 23:59:59

當(dāng)然,將上述方法中的 rangeOfUnit 替換為:

  • NSCalendarUnitDay可以獲取當(dāng)天的時(shí)間區(qū)間 2018-11-12 00:00:00 ~ 2018-11-12 23:59:59
  • NSCalendarUnitWeekOfYear可以獲取當(dāng)周的時(shí)間區(qū)間 2018-11-12 00:00:00 ~ 2018-11-18 23:59:59
  • NSCalendarUnitMonth 可以獲取當(dāng)月的時(shí)間區(qū)間 2018-11-01 00:00:00 ~ 2018-11-30 23:59:59
  • NSCalendarUnitYear可以獲取當(dāng)年的時(shí)間區(qū)間 2018-01-01 00:00:00 ~ 2018-12-31 23:59:59

iOS 11以后系統(tǒng)相冊選取完圖片編輯頁面,取消按鈕很難被點(diǎn)擊到的問題解決辦法

#pragma mark - Navigation Controller Delegate
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if ([UIDevice currentDevice].systemVersion.floatValue < 11) {
        return;
    } if ([viewController isKindOfClass:NSClassFromString(@"PUPhotoPickerHostViewController")]) {
        [viewController.view.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            // iOS 11之后,圖片編輯界面最上層會(huì)出現(xiàn)一個(gè)寬度<42的view,會(huì)遮蓋住左下方的cancel按鈕,使cancel按鈕很難被點(diǎn)擊到,故改變該view的層級(jí)結(jié)構(gòu)
            if (obj.frame.size.width < 42) {
                [viewController.view sendSubviewToBack:obj];
                *stop = YES;
            }
        }];
    }
}

創(chuàng)建一個(gè)可任意伸縮的圖片

- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight
  • 這是 UIImage 的一個(gè)實(shí)例方法,它的功能是創(chuàng)建一個(gè)內(nèi)容可拉伸,而邊角不拉伸的圖片,需要兩個(gè)參數(shù),第一個(gè)是左邊不拉伸區(qū)域的寬度,第二個(gè)參數(shù)是上面不拉伸的高度。根據(jù)設(shè)置的寬度和高度,將接下來的一個(gè)像素進(jìn)行左右擴(kuò)展和上下拉伸。

  • 注意:可拉伸的范圍都是距離leftCapWidth后的1豎排像素,和距離topCapHeight后的1橫排像素

如何忽略UserInterfaceState.xcuserstate文件

首先說說它的作用:UserInterfaceState.xcuserstate是Xcode中保存的用戶操作的GUI狀態(tài),如窗口位置,打開的標(biāo)簽頁,在項(xiàng)目檢查等展開的節(jié)點(diǎn)、 簡單地調(diào)整大小的Xcode窗口將這個(gè)文件來改變和修改您的源代碼控制系統(tǒng)進(jìn)行標(biāo)記。每個(gè)開發(fā)者此文件都會(huì)是modify狀態(tài),為了避免多個(gè)開發(fā)者的沖突那么我們最好ignore它為好!
第一步:cd 到你的工程主目錄
第二步:git rm --cache */UserInterfaceState.xcuserstate
第三步:git commit -m "Never see you again, UserInterfaceState"
然后去管理工具中選中列出的untrack文件,igonre它,最后再push一下。
使用git時(shí)如何忽略UserInterfaceState.xcuserstate文件

Article list

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 硬派健身 摘要 自序 與更好的自己,在未來重逢。 2016-10-11 13:34:10 是誰說運(yùn)動(dòng)一定要持續(xù)40...
    夜上海灘閱讀 10,392評(píng)論 0 50
  • 國家電網(wǎng)公司企業(yè)標(biāo)準(zhǔn)(Q/GDW)- 面向?qū)ο蟮挠秒娦畔?shù)據(jù)交換協(xié)議 - 報(bào)批稿:20170802 前言: 排版 ...
    庭說閱讀 12,446評(píng)論 6 13
  • 最近,相信大家都從各大新聞上可以看到我們中國的傳統(tǒng)節(jié)氣“立秋”已到了。正所謂秋高氣爽,在這個(gè)時(shí)候正當(dāng)是游山玩水的好...
    李想家閱讀 554評(píng)論 0 0
  • 張藝謀導(dǎo)演的《影》最近出了新的預(yù)告片,攝影出身的張導(dǎo)在色彩和畫面上從來都是獨(dú)樹一幟,這次黑白分明的水墨風(fēng)也是讓人眼...
    王小民的吐槽閱讀 583評(píng)論 1 2
  • “群里咋沒有動(dòng)靜呢”? 我手機(jī)里有三個(gè)大群:親戚群,同學(xué)群,戰(zhàn)友群。最近幾篇文章“咣當(dāng)”砸進(jìn)去沒了波浪,發(fā)個(gè)笑話還...
    倪郭閱讀 961評(píng)論 15 33

友情鏈接更多精彩內(nèi)容