6月24日
- 改變狀態(tài)欄文字顏色脯倒,即時(shí)間盔憨、電池等的顏色:
首先郁岩, 在 plist 文件中添加 View controller-based status bar appearance 问慎,并將其設(shè)置為 NO 。
然后笼恰, 在需要修改的頁面的 `super.viewDidLoad()` 下面添加 UIApplication.sharedApplication().statusBarStyle = .LightContent 即可歇终。
?? 注意评凝,這個(gè)操作會(huì)改變前后頁的文字顏色。如果不同的頁面不同展示宜肉,那么就改變 `style` 的值谬返。
6月27日
- 當(dāng)我們使用定位服務(wù)的時(shí)候日杈,發(fā)現(xiàn)
CLLocationManagerDelegate
方法不執(zhí)行翰蠢,是因?yàn)槲覀儧]有授權(quán)定位。解決辦法(注意廷支,這兩個(gè)字段在plist文件中中是找不到的恋拍,需要手動(dòng)輸入):
首先在 info.plist里加入對(duì)應(yīng)的缺省字段 ,值設(shè)置為YES(前臺(tái)定位寫上邊字段僵娃,前后臺(tái)定位寫下邊字段)
NSLocationWhenInUseDescription // 使用時(shí)獲取定位
NSLocationWhenInUseUsageDescription // 允許在前臺(tái)獲取GPS的描述
NSLocationAlwaysUsageDescription // 允許在前默怨、后臺(tái)獲取GPS的描述
說明一下上面三個(gè)字段使用時(shí)的區(qū)別:NSLocationWhenInUseDescription
是最常用的一個(gè)設(shè)置,此時(shí)將字段 NSLocationAlwaysUsageDescription
設(shè)置為 String
類型骤素,并加入描述文字,首次使用APP時(shí)痕檬,在使用定位的彈框提示中,下面小字顯示自己的描述文字谆棺。NSLocationWhenInUseUsageDescription
字段設(shè)置為 Bool
值時(shí),使用程序期間將程序后臺(tái)運(yùn)行罕袋,會(huì)再手機(jī)最上方提示自己的APP正在使用定位服務(wù)改淑。NSLocationAlwaysUsageDescription
表示APP使用使用定位服務(wù)浴讯。這個(gè)在上傳到AppStore審核時(shí)榆纽,必須要說明程序在哪里使用了實(shí)時(shí)定位服務(wù),并且要說明實(shí)時(shí)定位會(huì)損耗電池壽命衣屏。
7月6日
- 獲取本地視頻的指定時(shí)間截圖
首先,獲取安裝路徑下 Documents
文件夾路徑一睁,并且遍歷所有文件,獲取完整路徑:
/**
獲取全部視頻了路徑
- returns: 路徑數(shù)組
*/
func getAllVideoPaths() -> [String] {
var pathArray = [String]()
// Documents 文件夾
let pathFolder = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
// 文件夾路徑
let pathString = pathFolder[0] as String
// 拼接獲取每一個(gè)文件的完整路徑
if let lists = try? NSFileManager.defaultManager().contentsOfDirectoryAtPath(pathString) {
for item in lists {
pathArray.append(pathString + "/" + item)
}
}
return pathArray
}
然后根據(jù)路徑獲取封面:
// 通過文件路徑獲取截圖:
func getVideoImage(videoUrl: NSURL) -> UIImage? {
// 獲取截圖
let videoAsset = AVURLAsset(URL: videoUrl)
let cmTime = CMTime(seconds: 1, preferredTimescale: 10)
let imageGenerator = AVAssetImageGenerator(asset: videoAsset)
if let cgImage = try? imageGenerator.copyCGImageAtTime(cmTime, actualTime: nil) {
let image = UIImage(CGImage: cgImage)
return image
} else {
print("獲取縮略圖失敗")
}
return nil
}
刪除指定路徑下的文件方法:
NSFileManager.defaultManager().removeItemAtURL(<#T##URL: NSURL##NSURL#>)
NSFileManager.defaultManager().removeItemAtPath(<#T##path: String##String#>)
7月11日
- 獲取設(shè)備外網(wǎng)IP的方法:
使用樂視的視頻上傳功能用到了這個(gè)知識(shí)點(diǎn):
/**
獲取設(shè)備外網(wǎng)IP地址
- returns: 地址
*/
func getDeviceIP() -> NSDictionary? {
let ipUrl = NSURL.init(string: "http://pv.sohu.com/cityjson?ie=utf-8")
if var needIP = try? String.init(contentsOfURL: ipUrl!, encoding: NSUTF8StringEncoding) {
// 判斷字符串是否是所需數(shù)據(jù)
if needIP.hasPrefix("var returnCitySN = ") {
// 對(duì)字符串進(jìn)行處理佃却,然后進(jìn)行Json解析者吁,刪除字符串多與字符
for _ in 0 ..< 19 {
needIP.removeAtIndex(needIP.startIndex)
}
// 截取字符串
let nowIP = needIP.substringToIndex(needIP.startIndex.advancedBy(needIP.characters.count - 1))
// 將字符串換成二進(jìn)制進(jìn)行Json解析
let data = nowIP.dataUsingEncoding(NSUTF8StringEncoding)
if let needData = try? NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) {
return needData as? NSDictionary
} else {
print("解析外網(wǎng)地址失敗")
return nil
}
}
} else {
print("獲取外網(wǎng)地址失敗")
}
return nil
}
7月12日
- 計(jì)算指定路徑下文件的大小。
計(jì)算沙盒內(nèi)部文件大小時(shí)用到這個(gè)方法:
// 文件大小饲帅,self.videoUrl 是文件路徑
do {
let file = try NSFileManager.defaultManager().attributesOfItemAtPath(self.videoUrl!)
let size = file["NSFileSize"] as! CGFloat
let fileSize: CGFloat = size / CGFloat(1024) / CGFloat(1024)
// 保留兩位小數(shù)
print("文件大懈吹省: \(String(format: "%0.2f",fileSize)) M")
} catch let error as NSError {
print("error = \(error)")
}
7月19日
- 將標(biāo)準(zhǔn)時(shí)間轉(zhuǎn)換為時(shí)間串。
即 2016-07-18 12:26:16 +0000 -> 20160718122616 , 在用樂視SDK上傳本地視頻的時(shí)候用到了這個(gè)知識(shí)點(diǎn)灶泵。需要注意的是標(biāo)準(zhǔn)時(shí)間會(huì)比目前時(shí)間提前8小時(shí)染坯,這里也做了簡(jiǎn)單的轉(zhuǎn)化:
/**
將日期轉(zhuǎn)換成年月日時(shí)分秒的樣式,用于存儲(chǔ)本地視頻
- parameter originDate: 需要轉(zhuǎn)換的原始時(shí)間
- returns: 成品
*/
func transformDateToTimeString(originDate: NSDate) -> String {
// NSDate() 會(huì)比現(xiàn)在的時(shí)間提前8小時(shí)丘逸,轉(zhuǎn)換為當(dāng)前時(shí)間
let zone = NSTimeZone.systemTimeZone()
let interval = zone.secondsFromGMTForDate(originDate)
let localDate = originDate.dateByAddingTimeInterval(Double(interval))
// 將日期轉(zhuǎn)化為字符串 "2016-07-18 12:26:16 +0000\n"
var dateArray = String(localDate).componentsSeparatedByString(" ")
let first = dateArray.first?.componentsSeparatedByString("-")
let second = dateArray[1].componentsSeparatedByString(":")
let firArr: NSMutableArray = NSMutableArray(array: first!)
let secArr: NSMutableArray = NSMutableArray(array: second)
let name = firArr.componentsJoinedByString("") + secArr.componentsJoinedByString("")
return name
}
- 將總秒數(shù)轉(zhuǎn)換為播放時(shí)間单鹿。
即 30 -> 00:00:30 。這里比較有價(jià)值的就是字符的格式化輸出這個(gè)點(diǎn)深纲。
/**
將秒數(shù)轉(zhuǎn)化為 00:00:00 的時(shí)間樣式
- parameter totalSecond: 待轉(zhuǎn)換秒數(shù)
- returns: 時(shí)間
*/
func mTransformSecondsToPlayTime(totalSecond: Int?) -> String {
if let seconds = totalSecond {
let hour = seconds / 3600
let mintues = (seconds % 3600) / 60
let second = seconds % 60
return String(format: "%02d",hour) + ":" +
String(format: "%02d", mintues) + ":" +
String(format: "%02d", second)
} else {
return "88:88:88"
}
}
8月3日
轉(zhuǎn)換十六進(jìn)制顏色值:
Objective-C
:
#define HEXCOLOR(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
Swift:
// 十六進(jìn)制顏色轉(zhuǎn)換
func HEXCOLOR(rgbValue: Int) -> UIColor {
return UIColor.init(red: CGFloat(Double(((rgbValue & 0xFF0000) >> 16)) / 255.0), green: CGFloat(Double(((rgbValue & 0xFF00) >> 8)) / 255.0), blue: CGFloat(Double((rgbValue & 0xFF)) / 255.0), alpha: 1.0)
}
8月8日
- 雷達(dá)動(dòng)畫核心代碼仲锄,最適合用在地圖中:
// 調(diào)制顏色
struct Contants {
struct ColorPalette {
static let green = UIColor(red:0.00, green:0.87, blue:0.71, alpha:1.0)
}
}
/**
核心動(dòng)畫
- parameter beginTime: 啟動(dòng)時(shí)間間隔
*/
func sonar(beginTime: CFTimeInterval) {
let circlePath1 = UIBezierPath(arcCenter: self.view.center, radius: CGFloat(3), startAngle: CGFloat(0), endAngle: CGFloat(M_PI * 2), clockwise: true)
let circlePath2 = UIBezierPath(arcCenter: self.view.center, radius: CGFloat(80), startAngle: CGFloat(0), endAngle: CGFloat(M_PI * 2), clockwise: true)
let shapeLayer = CAShapeLayer()
shapeLayer.strokeColor = Contants.ColorPalette.green.CGColor // 邊緣顏色
shapeLayer.fillColor = Contants.ColorPalette.green.CGColor
shapeLayer.path = circlePath1.CGPath
self.view.layer.addSublayer(shapeLayer)
let pathAnimation = CABasicAnimation(keyPath: "path")
pathAnimation.fromValue = circlePath1.CGPath
pathAnimation.toValue = circlePath2.CGPath
let alphaAnimation = CABasicAnimation(keyPath: "opacity")
alphaAnimation.fromValue = 0.8
alphaAnimation.toValue = 0.0
let animationGroup = CAAnimationGroup()
animationGroup.beginTime = beginTime
animationGroup.animations = [pathAnimation, alphaAnimation]
animationGroup.duration = 2.76
animationGroup.repeatCount = FLT_MAX
animationGroup.delegate = self
animationGroup.removedOnCompletion = false
animationGroup.fillMode = kCAFillModeForwards
shapeLayer.addAnimation(animationGroup, forKey: "sonar")
}
/**
啟動(dòng)動(dòng)畫
*/
func startAnimation() {
sonar(CACurrentMediaTime())
sonar(CACurrentMediaTime() + 0.92)
sonar(CACurrentMediaTime() + 1.84)
}
8月23日
8月24日
- 隱藏
UITabBar
的灰色線條:
let tabBar = UITabBar.appearance()
tabBar.shadowImage = UIImage()
tabBar.backgroundImage = UIImage()
8月29日
- Swift 格式化輸出:
參考鏈接:SwiftTips-Log輸出
/**
自定義Log輸出
- parameter message: 輸出信息
- parameter file: 輸出方法所在文件名
- parameter method: 所在方法名
- parameter line: 所在行數(shù)
*/
public func printLog<T>(message: T,
file: String = #file,
method: String = #function,
line: Int = #line) {
#if DEBUG
print("????\((file as NSString).lastPathComponent)[\(line)] \(method)????: \(message)")
#endif
}
在 Swift 中,編譯器為我們準(zhǔn)備了幾個(gè)很有意義的編譯符號(hào)湃鹊,分別是:
符號(hào) | 類型 | 描述 |
---|---|---|
#file | String | 包含這個(gè)符號(hào)的文件的路徑 |
#line | Int | 符號(hào)出現(xiàn)處的行數(shù) |
#column | Int | 符號(hào)出現(xiàn)處的列數(shù) |
#function | String | 包含這個(gè)符號(hào)的方法名字 |
這樣還不足夠儒喊,還要配置 DEBUG 這個(gè)宏,否則不能正確輸出:
2017年3月7日
1.設(shè)置微信為白名單:
info.plist
添加代碼:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>weixin</string>
</array>