以下內容為工作筆記:
1 盡量使用double,不使用float,否則帶來不可預料的截位問題。
2 Xcode8 關閉系統的Log :Edit Scheme-> Run -> Arguments, 在Environment Variables中添加 OS_ACTIVITY_MODE = disable
3
SecondViewController *svc = [[SecondViewController alloc] init];
svc.view.backgroundColor = [UIColor grayColor];
[self.navigationController pushViewController:svc animated:YES];
這樣的代碼可能會導致viewDidLoad中的代碼先執行,如果此時viewDidLoad中包含一些對navigationBar的操作代碼,它們會不起作用,因為此時沒有navigationBar。
4 iOS10中訪問相冊的一個問題:
需要在info.plist中加上
Privacy - Camera Usage Description
Privacy - Photo Library Usage Description來描述獲取用戶同意時的描述文字,否則會崩潰-
5 底層的time函數使用,
- 1 Time->String 創建一個C類型字符串(buffer[100])存儲轉換結果,使用localtime() 將timeInterval轉換為本地時間,最后使用strftime_l/strftime轉換為字符串。
- 2 String->Time 使用mktime()將字符串按格式轉換成timeInterval,最后轉換為NSDate
6 打印UIView的層級關系
使用LLDB: po [self.view recursiveDescription]7 如果實在需要在navigationBar上加一個subview做背景,可能Xcode7/8 會導致不同的結果,需要注意插入的位置。
8 關于用宏來判斷系統版本的問題:
kCFCoreFoundationVersionNumber
使用上面這個方法會來更加高效優雅。
- 9 中文URL的解析:
使用
- (nullable NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)enc
方法先進性編碼,也可以用下面方法替代:
- (nullable NSString *)stringByAddingPercentEncodingWithAllowedCharacters:(NSCharacterSet *)allowedCharacters
- 10 刪除Cookie
NSHTTPCookieStorage * storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie * cookie in [storage cookiesForURL:[NSURL URLWithString:[YTKNetworkConfig sharedInstance].baseUrl]]) {
[storage deleteCookie:cookie];
}
- 11 畫1像素的分割線
項目有個需求是不管機型,自定義的cell分割線必須都是1pxl,對于使用簡單的使用添加一個UIView來充當分割線的方法來說,可以使用約束的方法調整height為
(1.f/[UIScreen mainScreen].scale)
使用Masonry有效,對于xib,可以用代碼設置@property (weak, nonatomic) IBOutlet NSLayoutConstraint *someConstraint。
如果使用的是UIBezierPath等來“繪制”1像素的線的時候,就沒那么簡單了。
原因在于代碼中我們定位的點,比如(1,0)。 1其實代表的是像素與像素之間的中間點,而不是一個起始點。
所以按照上圖的渲染方法結果就是會繪制出一個2pxl的線。
如果我們想要渲染出剛好1pxl的線,在一倍屏幕上,就需要把定位的點移動0.5個point(=0.5pxl),這樣渲染的起始位置剛好與一個像素的開始位置重合。
同理,二倍屏幕上移動0.25個點,相當于移動0.5個pxl。
總結就是移動(0.5f/[UIScreen mainScreen].scale)的距離。
當然上述方法是作用于坐標點是奇數的情況下,偶數則不需要改動了。
- 12 斷言
[NSParameterAssert / NSCParameterAssert] 針對判斷參數是否存在使用。
關于以上是四個斷言的區別 這篇文章講的比較清楚:斷言(NSAssert)的使用
還需要注意的就是使用NSAssert/NSParameterAssert 容易造成引用循環。
- 13 Xcode 8 以后在xib上添加ImageView顯示不出的問題解決:
把user下的Library/Developer/Xcode/DerivedData/ 這里 rm -rf * 全部刪除
14 PCH文件的使用:
iOS 開發 Pch 文件的正確使用15 高德地圖SDK支持斷點續傳
16 關于tableView的單選需求,可以在復寫cell的setSelected:animated:方法對cell顯示進行更改,調用tableview的selectRowAtIndexPath:方法僅僅觸發cell的setSelected:animated:方法,不會調用tableView的任何代理方法,也不會發送通知。
17 設置陰影:
loginBtn.layer.shadowOffset = CGSizeMake(1, 1); //控制陰影方向和大小
loginBtn.layer.shadowOpacity = 0.8; //透明圖
loginBtn.layer.shadowColor = [UIColor blackColor].CGColor; //陰影顏色
- 18 設置attributedString的段落格式
可以設置NSMutableParagraphStyle對象的屬性進行修改,注意lineSpacing和
paragraphSpacing的差別,并與maximumLineHeight配合使用。
示例:
NSMutableParagraphStyle *linebreak = [[NSMutableParagraphStyle alloc] init];
linebreak.lineBreakMode = NSLineBreakByTruncatingTail;
linebreak.paragraphSpacing = 5.f;
linebreak.maximumLineHeight = 10.f;
NSMutableAttributedString *attributedDesc = [[NSMutableAttributedString alloc] initWithString:desc
attributes:@{NSParagraphStyleAttributeName : linebreak,
NSForegroundColorAttributeName : HJ_COLOR_66,
NSFontAttributeName: SH_FONT_MEDIUM_SIZE(10)}];
_txvDesc.attributedText = attributedDesc;
- 19 手勢沖突的一些參考:
(Gesture Recognizers與觸摸事件分發)[http://blog.csdn.net/chun799/article/details/8194893]
20 PCH文件的配置:iOS 開發 Pch 文件的正確使用】
21 UIScrollView的一個不常用方法:
- (void)flashScrollIndicators;
// displays the scroll indicators for a short time. This should be done whenever you bring the scroll view to front.
根據官方建議,當UIScrollview顯示的時候,應該調用一次,作為一種提示用(接近原生體驗)。
- 22 系統自帶滑動返回在自定義返回按鈕之后失效
此時需要添加下面的代碼解決:
self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
*23 兩個關于設計的網站
Mapbox 里面有比較多的地圖設計案例
dribbble 設計網站
*24 圓角設置可以使用CAShapeLayer進行設置,提高幀率。
*25 在- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated中加入了兩行代碼
[self.map removeFromSuperview];
[self.view addSubview:mapView];
可以有效解決內存峰值過高的問題。