iOS開發月報#1|201807

關閉隱式動畫

CATransaction.begin()
CATransaction.setDisableActions(true)
self.layer.frame = self.bounds
CATransaction.commit()
複製代碼

AVPlayer出現一直緩存,緩存一段時間之後纔開始播放的問題

player.automaticallyWaitsToMinimizeStalling = false//延遲播放,默認開
複製代碼

關於這個屬性的一些說明:

In versions of iOS prior to iOS 10.0 and versions of OS X prior to 10.12, this property is unavailable, and the behavior of the AVPlayer corresponds to the type of content being played. For streaming content, including HTTP Live Streaming, the AVPlayer acts as if automaticallyWaitsToMinimizeStalling is YES. For file-based content, including file-based content accessed via progressive http download, the AVPlayer acts as if automaticallyWaitsToMinimizeStalling is NO.

大致是說在iOS10之前的客戶端,雖然這個參數不可用,但是非流媒體類型的播放這個配置默認爲false,所以在iOS10下建議這個屬性值爲false。

AVPlayer是否正在播放的判斷

當我們使用KVO監聽player.rate來判斷player的是否正在播放時,會發現這個值是不準的。其實準確的說是player.rate=1不代表正在播放,player.rate=0是可以代表正在暫停的。所以player.rate=0代表暫停,正在播放的狀態可以這樣判斷:

self.timeObserve = self.player.addPeriodicTimeObserver(forInterval: CMTimeMake(1, 1),
queue: DispatchQueue.main,
using: {(time) in
if self.player.timeControlStatus == AVPlayerTimeControlStatus.playing {
//AVPlayerTimeControlStatus爲iOS之後的API
self.state = .playing
}
})
複製代碼

下載時URLSessionConfiguration的配置

使用Alamofire下載時,我們通常需要一個SessionManager配置下載參數:

let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = 50//50s超時
/** 最大同時下載數 ---- iOS對於同一個IP服務器的併發最大默認爲4,OS X爲6 */
configuration.httpMaximumConnectionsPerHost = 4
/** A Boolean value that indicates whether TCP connections should be kept open when the app moves to the background. */
configuration.shouldUseExtendedBackgroundIdleMode = true//爲true支持後臺下載
manager = Alamofire.SessionManager(configuration: configuration)
複製代碼

不要存儲沙盒絕對地址

當我們向沙盒寫入數據時,將該絕對路徑保存下來,下次再打開該地址並不會獲取到我們存入的數據。原因如下:

iOS8之後,蘋果添加的新特性,將每次打開app內的沙盒[唯一編碼路徑](紅框部分)重新生成,並保持上一次的沙盒文件(Documents、Library、tmp)移到新生成的文件內,舊文件刪除,就是說,你保存的文件都在,只不過每次打開後,都會有一個新的絕對路徑。

所以存儲路徑應該存相對路徑:

//這兩個都代表document的相對路徑
let rootPath = NSHomeDirectory() + "/Documents/"
let rootPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first
複製代碼

childViewController的viewDidAppear方法調用

如果一個ViewController中嵌套了多個childViewController。當宿主VC(我們暫且這麼稱呼它)調用viewDidAppear等方法時,其中的childViewController都會默認調用對應方法。如果我們不想childViewController調用該方法可以重寫該VC的屬性:

override var shouldAutomaticallyForwardAppearanceMethods: Bool {
return false
}
複製代碼

圖片切換漸入漸出的方法

通過UIImageView展示圖片和layer.contents展示圖片都可以使用以下方法:

let transition = CATransition()
transition.duration = 0.5
transition.type = kCATransitionFade
self.view.layer.add(transition, forKey: "layer.contents")
self.view.layer.contents = image.cgImage//適用於imageView
複製代碼

cell移出視圖,移入視圖的方法

//TableViewCell
override func prepareForReuse() {
super.prepareForReuse()//使用重用池的cell,顯示過的cell移至可視範圍
}
//TableView
func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
//cell移出視圖時調用
}
複製代碼

tableview,collectionView數據reload之後的操作

我們如果要想實現在reload之後彈出alertView,或者滾動到特定一行,可能會直接寫:

tableView.reloadData()
tableView.scrollToRow(at: indexPath, at: .middle, animated: true)
複製代碼

看似沒問題,但是滾動沒起作用,因爲reloadData是立即返回的,不會等tableview刷新完成。 解決辦法就是需要等reload完成之後再做我們需要的操作,reload是否完成有幾種方式監聽:

//collectionView
collectionView.performBatchUpdates(nil) { (finished) in
    //reload完成
}
//tableView方法只有iOS11可用
tableView.performBatchUpdates(nil) { (finished) in
    //reload完成
}//替代func beginUpdates(),func endUpdates()
//tableView等reload完成還可以使用
tableView.reloadData()
DispatchQueue.main.async {
    //reload完成
}
複製代碼
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章