Turn off implicit animation
CATransaction.begin()
CATransaction.setDisableActions(true)
self.layer.frame = self.bounds
CATransaction.commit()
Copy the code
[Bug Mc-10864] – AVPlayer is caching for a while before starting to play
player.automaticallyWaitsToMinimizeStalling = false// Delay playback is enabled by default
Copy the code
A few notes on this property:
In versions of iOS prior to iOS 10.0 and versions of OS X prior to iOS 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.
In iOS10, false is recommended for non-streaming media playback, although this parameter is not available.
Determine whether AVPlayer is playing
This value is incorrect when we use KVO to listen on player.rate to determine if player is playing. Player. rate=1 does not mean that the player is playing. Player. rate=0 does mean that the player is paused. Player. rate=0
self.timeObserve = self.player.addPeriodicTimeObserver(forInterval: CMTimeMake(1.1),
queue: DispatchQueue.main,
using: {(time) in
if self.player.timeControlStatus == AVPlayerTimeControlStatus.playing {
/ / after AVPlayerTimeControlStatus for iOS API
self.state = .playing
}
})
Copy the code
Configuration of URLSessionConfiguration during download
When downloading with Alamofire, we usually need a SessionManager configuration download parameter:
let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = 50/ / 50 s timeout
/** Maximum number of concurrent downloads ---- The default maximum number of concurrent downloads for an IP server is 4 on iOS, and 6 */ on OS X
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// Support background download for true
manager = Alamofire.SessionManager(configuration: configuration)
Copy the code
Do not store absolute sandbox addresses
When we write to the sandbox, we save the absolute path, and the next time we open the address, we don’t get the data we saved. Here’s why:
After iOS8, apple added a new feature that regenerates the sandbox (unique code path) (red box) every time you open your app, and keeps the last sandbox files (Documents, Library, TMP) in the newly generated file. The old files are deleted, that is, the files you saved are still there, but every time you open it, There’s going to be a new absolute path.
// Both represent relative paths to document
let rootPath = NSHomeDirectory() + "/Documents/"
let rootPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first
Copy the code
The viewDidAppear method of childViewController is called
If you have multiple Child ViewControllers nested within a ViewController. When the host VC (we’ll call it that for now) calls methods like viewDidAppear, the childViewController in it defaults to calling the corresponding method. We can override the VC property if we don’t want childViewController to call this method:
override var shouldAutomaticallyForwardAppearanceMethods: Bool {
return false
}
Copy the code
Image switching in and out method
Displaying images via UIImageView and layer.contents can be done using the following methods:
let transition = CATransition()
transition.duration = 0.5
transition.type = kCATransitionFade
self.view.layer.add(transition, forKey: "layer.contents")
self.view.layer.contents = image.cgImage// Applies to imageView
Copy the code
Method of moving a cell from a view to a view
//TableViewCell
override func prepareForReuse(a) {
super.prepareForReuse()// With the reuse pool cell, the displayed cell is moved to visual range
}
//TableView
func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
// Called when the cell is removed from the view
}
Copy the code
Tableview,collectionView after data reload
If we wanted to pop an alertView after Reload, or scroll to a particular line, we would say:
tableView.reloadData()
tableView.scrollToRow(at: indexPath, at: .middle, animated: true)
Copy the code
It looks fine, but scrolling doesn’t work because reloadData is returned immediately, not waiting for the TableView to refresh. The solution is to wait for the reload to complete before doing the desired operation. There are several ways to check whether the reload is complete:
//collectionView
collectionView.performBatchUpdates(nil) { (finished) in
/ / reload
}
// The tableView method is available only in iOS11
tableView.performBatchUpdates(nil) { (finished) in
/ / reload
}// replace func beginUpdates(), func endUpdates()
//tableView can be used after reload
tableView.reloadData()
DispatchQueue.main.async {
/ / reload
}
Copy the code