想了很久想好好总结一下然后写,但是我还是有很多东西不懂或者是无法理解,所以暂时无法形成一个比较系统的认识。下面是我在学习当中遇到的一写我认为我理解了的函数。
1. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
//这个函数是返回一个整数,把一个视图分成几个section
2. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
//这个函数是用来建立一个cell的实例,然后返回。cell是UICollectionView的最小单位了。一个UICollectionView可以由一个或多个section,一个section又可以由一个或多个cell组成
3. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
这个函数就是用来返回cell的大小了。
return CGSize.init(width: view.frame.width, height:100)
//这就是一个cell的宽度是View框架的大小,高度时100
4. addSubview()
这个函数用来给view添加子类View。
5. addConstraintWithFormat(format:String, views:UIView...)
这个函数又个VisualFormatLanguage的语言,来定位元素。下面举几个例子
addConstraintWithFormat(format: "H:|-12-[v0(68)]", views: profileImageView)
//这一行是在水平方向设置profileImageView这个元素:从父View左侧距离12像素,宽68。H表示是水平面horizontally。
|竖杠表示这个元素是从父View靠左
addConstraintWithFormat(format: "V:[v0(68)]", views: profileImageView)
//这一行是在竖直方向设置profileImageView这个元素:V表示Vertical,高68。在这个format参数没有竖杠|,那是因为有下面这行代码来定位
addConstraint(NSLayoutConstraint(item: profileImageView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0
))
//这一行表示profileimageView在竖直方向上它处于它的父VieW也就是self的中间。
//这个format参数不一定要一定是就一个一个定义也可以多个定义,然后views里的第一个参数就是【v0】,第二个就是【v1】类推
//如果几个参数之间紧挨着就直接[v0][v1]这样写就可以了。如果不是挨着那就要写“[v0]-数字-[v1]”这样写
6. isHighlighted
//这是个UICollectionViewCell的变量。里面有个didSet函数可以设置一些如果高亮了有什么变化这些内容。
override var isHighlighted: Bool{
didSet {
backgroundColor = isHighlighted ? UIColor(displayP3Red: 0, green: 134/255, blue: 249/255, alpha: 1) : UIColor.white
nameLabel.textColor = isHighlighted ? UIColor.white : UIColor.black
messegeLabel.textColor = isHighlighted ? UIColor.white : UIColor.black
timeLabel.textColor = isHighlighted ? UIColor.white : UIColor.black
}
}
//如上面的代码;如果高亮就会改变这个cell的背景,和文字颜色等
7. override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
//这个参数是如果被选择的时候需要做什么
8. UIColor(White:,alpha:)
//这个函数第一个参数是指灰度值,第二个参数是不透明度,两个参数的范围都是0--1,0以下算0,1以上算1
//灰度值是把白和黑之间的一个度,0时白,1是黑。
//不透明度,顾名思义。0就是完全透明,1就是完全不透明