最近项目不算紧,于是就学了学 Swift ,看了一大神写的项目https://github.com/hrscy/DanTang,很受益,感谢开源!另外自己也写了一些基础代码,分享出来,第一是希望得到同行前辈的指导,第二是希望对需要的朋友有所帮助。
先分享一些学习资料:
学习网站:
苹果官方为开发者提供的 Swift 学习资源: https://developer.apple.com/swift/resources/
官方的 API Design: https://swift.org/documentation/api-design-guidelines/
学习书籍:
TheSwiftProgrammingLanguage(Swift3): 链接:http://pan.baidu.com/s/1jIopBwi 密码:dqho
The Swift Programming Language 中文版: 链接:http://pan.baidu.com/s/1slpxtTj 密码:xay1
其它学习资料
练习的 demo 地址: https://github.com/liuzhongning/NNMintFurniture
- 主要包括以下功能:
- 图片轮播
- 导航栏渐变
- 瀑布流练习
- UIScrollView 练习
- 照相功能,更换头像
- 二维码扫描及识别
- 随机图片验证码封装
- 圆形输入框封装
- 第三方库 SnapKit 用法
- …………
练习 demo 的简单介绍,前方高能预警,大量图片请注意手机流量!
一、图片轮播
- 核心代码:
轮播图的封装
// MARK: - 懒加载轮播视图
private lazy var shufflingFigureView : NNShufflingFigureView = {
let frame = CGRect(x: 0, y: 0, width: NNScreenWidth, height: 180)
let imageView = ["shuffling1", "shuffling2", "shuffling3", "shuffling4"]
let shufflingFigureView = NNShufflingFigureView(frame: frame, images: imageView as NSArray, autoPlay: true, delay: 3, isFromNet: false)
shufflingFigureView.delegate = self
return shufflingFigureView
}()
通过代理处理图片的点击事件
// MARK: - 轮播代理方法,处理轮播图的点击事件
extension NNItemTableViewController: NNShufflingFigureViewDelegate {
func addShufflingFigureView(addShufflingFigureView: NNShufflingFigureView, iconClick index: NSInteger) {
print(index)
}
}
二、导航栏渐变
- 核心代码
页面滚动时调用
// MARK: - UIScrollViewDelegate 滚动页面时调用
extension NNItemTableViewController {
override func scrollViewDidScroll(_ scrollView: UIScrollView) {
if type == tableViewType.haveHeader {
return
}
currentPostion = scrollView.contentOffset.y
if currentPostion > 0 {
if currentPostion - lastPosition >= 0 {
if topBool {
topBool = false
bottomBool = true
stopPosition = currentPostion + 64
}
lastPosition = currentPostion
navigationController?.navigationBar.alpha = 1 - currentPostion / 500
} else {
if bottomBool {
bottomBool = false
topBool = true
stopPosition = currentPostion + 64
}
lastPosition = currentPostion
navigationController?.navigationBar.alpha = (stopPosition - currentPostion) / 200
}
}
}
}
三、瀑布流练习
- 核心代码
基础设置
// 布局
let layout = NNItemCollectionViewFlowLayout()
// 创建collectionView
let collectionView = UICollectionView.init(frame: view.bounds, collectionViewLayout: layout)
view.addSubview(collectionView)
collectionView.dataSource = self
collectionView.delegate = self
collectionView.backgroundColor = UIColor.white
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: NNItemCollectionViewControllerID)
自定义 UICollectionViewFlowLayout
// MARK: - 更新布局
override func prepare() {
super.prepare()
// 清除所有的布局属性
attrsArray.removeAll()
columnHeightsAry.removeAll()
for _ in 0 ..< columnCountDefault {
columnHeightsAry.append(edgeInsetsDefault.top)
}
let sections : Int = (collectionView?.numberOfSections)!
for num in 0 ..< sections {
let count : Int = (collectionView?.numberOfItems(inSection: num))!
for i in 0 ..< count {
let indexpath : NSIndexPath = NSIndexPath.init(item: i, section: num)