swift源碼閱讀筆記

定義一個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")

    }
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,534評論 19 139
  • 基礎部分(The Basics) 當推斷浮點數(shù)的類型時,Swift 總是會選擇Double而不是Float。 結合...
    gamper閱讀 1,487評論 0 7
  • Spring Boot 參考指南 介紹 轉載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,261評論 6 342
  • Hello Word 在屏幕上打印“Hello, world”,可以用一行代碼實現(xiàn): 你不需要為了輸入輸出或者字符...
    restkuan閱讀 3,362評論 0 6
  • 單反:富士 X—T2 ,f 2.8,1/30秒 ,ISO1000 拍攝思路:孩子在朋友的畫室玩,雙手沾滿了顏料,藍...
    Linda愛美麗閱讀 608評論 4 22

友情鏈接更多精彩內容