定義一個struct
struct video {
let image: String
let title: String
let source: String
}
初始化這個struct
video(image: "videoScreenshot01", title: "Introduce 3DS Mario", source: "Youtube - 06:32")
如果一個類實現(xiàn)了多個協(xié)議要按照下面的寫法
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate
記住一個變量在使用之前一定要先聲明
懶加載的寫法
lazy var applicationDocumentsDirectory: URL = {
// The directory the application uses to store the Core Data store file. This code uses a directory named "me.appkitchen.Snapchat_Menu" in the application's documents Application Support directory.
let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
return urls[urls.count-1]
}()
在一個controller中實現(xiàn)代理的時候都是要單獨拿出來作為一個extension
extension HomeViewController : UICollectionViewDataSource {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return interests.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Storyboard.CellIdentifier, for: indexPath) as! InterestCollectionViewCell
cell.interest = self.interests[indexPath.item]
return cell
}
}
聲明一個只有當前文件可以使用的變量
fileprivate struct Storyboard {
static let CellIdentifier = "InterestCell"
}
可選綁定
if let containsPlacemark = placemark {}
swift中函數(shù)參數(shù)列表中的下劃線使用:
1.忽略方法的默認外部參數(shù)名
在使用方法(類方法或者實例方法)時,方法的第二個參數(shù)名及后續(xù)的參數(shù)名,默認既是內部參數(shù)名,又是外部參數(shù)名,如果不想提供外部參數(shù)名,可以在參數(shù)名前添加下劃線來忽略外部參數(shù)名
class Counter {
var count: Int = 0
func incrementBy(amount: Int, numberOfTimes: Int) {
count += amount * numberOfTimes
}
}
在上面的代碼中,方法incrementBy()中的numberOfTimes具有默認的外部參數(shù)名:numberOfTimes,如果不想使用外部參數(shù)名可以使用下劃線進行忽略,代碼可以寫為(不過為了提高代碼的可讀性,一般不進行忽略):
class Counter {
var count: Int = 0
func incrementBy(amount: Int, _ numberOfTimes: Int) {
count += amount * numberOfTimes
}
}
這樣外部調用就直接可以 incrementBy(amount:10,20)
創(chuàng)建一個枚舉
public enum ScalingMode {
case resize
case resizeAspect
case resizeAspectFill
}
創(chuàng)建一個漸變色
let gradientLayer = CAGradientLayer()
gradientLayer.frame = self.bounds
let color1 = UIColor(white: 1.0, alpha: 0.2).cgColor as CGColor
let color2 = UIColor(white: 1.0, alpha: 0.1).cgColor as CGColor
let color3 = UIColor.clear.cgColor as CGColor
let color4 = UIColor(white: 0.0, alpha: 0.05).cgColor as CGColor
gradientLayer.colors = [color1, color2, color3, color4]
gradientLayer.locations = [0.0, 0.04, 0.95, 1.0]
layer.insertSublayer(gradientLayer, at: 0)
關鍵幀動畫
func animateMask() {
let keyFrameAnimation = CAKeyframeAnimation(keyPath: "bounds")
keyFrameAnimation.delegate = self as! CAAnimationDelegate
keyFrameAnimation.duration = 0.6
keyFrameAnimation.beginTime = CACurrentMediaTime() + 0.5
keyFrameAnimation.timingFunctions = [CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut), CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)]
let initalBounds = NSValue(cgRect: mask!.bounds)
let secondBounds = NSValue(cgRect: CGRect(x: 0, y: 0, width: 90, height: 73))
let finalBounds = NSValue(cgRect: CGRect(x: 0, y: 0, width: 1600, height: 1300))
keyFrameAnimation.values = [initalBounds, secondBounds, finalBounds]
keyFrameAnimation.keyTimes = [0, 0.3, 1]
self.mask!.add(keyFrameAnimation, forKey: "bounds")
}