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")

如果一个类实现了多个协议要按照下面的写法

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中实现代理的时候都是要单独拿出来作为一个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中函数参数列表中的下划线使用:
1.忽略方法的默认外部参数名
在使用方法(类方法或者实例方法)时,方法的第二个参数名及后续的参数名,默认既是内部参数名,又是外部参数名,如果不想提供外部参数名,可以在参数名前添加下划线来忽略外部参数名

class Counter {  
    var count: Int = 0  
    func incrementBy(amount: Int, numberOfTimes: Int) {  
        count += amount * numberOfTimes  
    }  
}

在上面的代码中,方法incrementBy()中的numberOfTimes具有默认的外部参数名:numberOfTimes,如果不想使用外部参数名可以使用下划线进行忽略,代码可以写为(不过为了提高代码的可读性,一般不进行忽略):

class Counter {  
    var count: Int = 0  
    func incrementBy(amount: Int, _ numberOfTimes: Int) {  
        count += amount * numberOfTimes  
    }  
} 

这样外部调用就直接可以 incrementBy(amount:10,20)

创建一个枚举

public enum ScalingMode {
  case resize
  case resizeAspect
  case resizeAspectFill
}

创建一个渐变色

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")

    }

你可能感兴趣的:(swift源码阅读笔记)