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

    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,326评论 19 139
  • 基础部分(The Basics) 当推断浮点数的类型时,Swift 总是会选择Double而不是Float。 结合...
    gamper阅读 1,462评论 0 7
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,229评论 6 342
  • Hello Word 在屏幕上打印“Hello, world”,可以用一行代码实现: 你不需要为了输入输出或者字符...
    restkuan阅读 3,333评论 0 6
  • 单反:富士 X—T2 ,f 2.8,1/30秒 ,ISO1000 拍摄思路:孩子在朋友的画室玩,双手沾满了颜料,蓝...
    Linda爱美丽阅读 601评论 4 22

友情链接更多精彩内容