一、xib随意设置frame
自定义View的Xib,初始为一个界面,且不可改变的大小,所以我们修改下,如下图:
二、安全区域问题
在iPhone X这样的手机中有安全区域,所以我们必须根据不同的情形,选择不同的选项,比如我们横屏的时候,要求自定义的导航栏必须满足充满整个区域,而不是只在安全区域中,这样会导致两侧留白
,并不美观,这样我们就进行如下设置:
选择SuperView就避免了Safe Area(安全区域)的问题。
三、闭包的使用
Swift在使用闭包传值的时候可以理解为OC的block,使用起来十分方便,下边纠结个项目中的传值来举例。
场景:APP中表的cell中放置三个collectionView页面,然后点击collectionCell,并告知点击的是哪个collectionView的哪个cell。
1、设置block块
viewTag:collectionView的tag。
selectIndex:点击的cell的row.
var itemClickBlock: ((_ viewTag:Int ,_ selectIndex:Int) -> Void)?
2、点击cell之后调用代理,传参
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
itemClickBlock!(collectionView.tag,indexPath.row)
}
3、在VC中调用传过来的参数,并对数据进行处理
cell?.itemClickBlock = {tag, index in
print("点击的cell的索引为:\(tag)~~~~~\(index)")
//仿照App Store微缩动画
UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 0, options: .curveLinear, animations: {
cell?.backV.transform = CGAffineTransform(scaleX: 0.99, y: 0.99)
}) { (res) in
cell?.backV.transform = .identity
}
}
四、TableView中嵌套collectionView
常见的场景就是我们App Store,
1、首先创建一个表
2、创建不同的cell,如只包含一个图片的cell,包含collectionView的cell,下面就着重说一下包含collectionView的cell。
a、在重写的初始化方法中,添加视图
class twoTableViewCell: UITableViewCell {
var backV: UIView!
var collectionV: UICollectionView!
var page: Int = 0
var collectionAry: [UICollectionView] = []
//设置block块
var itemClickBlock: ((_ viewTag:Int ,_ selectIndex:Int) -> Void)?
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
backV = UIView(frame: CGRect(x: 20, y: 10, width: SCREENWIDTH - 40, height: 500))
backV.backgroundColor = UIColor.init(red: 243/255.0, green: 243/255.0, blue: 243/255.0, alpha: 1)
backV.layer.masksToBounds = true
backV.layer.cornerRadius = 15
contentView.addSubview(backV)
let alabel = UILabel(frame: CGRect(x: 20, y: 20, width: 100, height: 20))
alabel.text = "超级实用"
alabel.textColor = .gray
backV.addSubview(alabel)
let ablabel = UILabel(frame: CGRect(x: 20, y: alabel.frame.maxY + 15, width: 300, height: 20))
ablabel.text = "地球人都在用,用了都说好"
ablabel.font = UIFont.boldSystemFont(ofSize: 20)
backV.addSubview(ablabel)
let layout = UICollectionViewFlowLayout()
layout.itemSize = CGSize(width: 125, height: 115)
layout.minimumLineSpacing = 10
layout.scrollDirection = .horizontal
let cheight = 115
//这里创建了三个collectionV,并赋予不同的tag
for index in 0...2 {
collectionV = UICollectionView(frame: CGRect(x: 0, y: Int(ablabel.frame.maxY) + 20 + index * (cheight + 10), width: Int(backV.frame.size.width ), height: cheight), collectionViewLayout: layout)
collectionV.delegate = self
collectionV.dataSource = self
collectionV.tag = index
collectionV.backgroundColor = .clear
collectionV.showsHorizontalScrollIndicator = false
//只能自动 拒绝手动
collectionV.isScrollEnabled = false;
backV.addSubview(collectionV)
collectionAry.append(collectionV)
collectionV.register(UICollectionViewCell.self, forCellWithReuseIdentifier: index.description)
}
//设置定时器,让他自己走contentoffset
let timer = Timer.scheduledTimer(timeInterval: 0.2, target: self, selector: #selector(startAutoScroll), userInfo: nil, repeats: true)
RunLoop.main.add(timer, forMode: RunLoopMode.UITrackingRunLoopMode)
}
}
自动移动的cell:
为了让下方的collectionViewCell一直向左移动,我们设置了定时器,不断的触发事件,然后让collectionView不断的偏移,并且禁止用户滑动。
@objc func startAutoScroll() {
page += 1
//这里的移动速度是不一样的
for index in 0...2 {
let collectionView = collectionAry[index]
collectionView.setContentOffset(CGPoint(x: (1 + index) * page, y: 0), animated: true)
}
}
这里有两个个问题:
1、cell移动完成之后就会变成空白,所以需要我们多添加一些数据,并且移动慢一些,这样保证在足够长的时间内不会出现空白。(PS:一般人也不会那么无聊的一直看这些数据)
2、在滑动界面的时候会出现定时器停止的问题,这是主线程卡顿了,需要切换一下定时器的model。
b.通过代理设置数据以及监控点击事件
extension twoTableViewCell: UICollectionViewDelegate, UICollectionViewDataSource{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 100
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
switch collectionView.tag {
case 0:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: collectionView.tag.description, for: indexPath)
cell.backgroundColor = UIColor(red: CGFloat(arc4random()%(UInt32)(256 + indexPath.item))/255.0, green: CGFloat(arc4random()%256)/255.0, blue: CGFloat(arc4random()%256)/255.0, alpha: 1.0)
cell.layer.cornerRadius = 10
return cell
case 1:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: collectionView.tag.description, for: indexPath)
cell.backgroundColor = UIColor(red: CGFloat(arc4random()%256)/255.0, green: CGFloat(arc4random()%(UInt32)(256 + indexPath.item))/255.0, blue: CGFloat(arc4random()%256)/255.0, alpha: 1.0)
cell.layer.cornerRadius = 10
return cell
case 2:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: collectionView.tag.description, for: indexPath)
cell.backgroundColor = UIColor(red: CGFloat(arc4random()%256)/255.0, green: CGFloat(arc4random()%256)/255.0, blue: CGFloat(arc4random()%(UInt32)(256 + indexPath.item))/255.0, alpha: 1.0)
cell.layer.cornerRadius = 10
return cell
default:
print("")
}
return UICollectionViewCell()
}
//发送点击cell的block事件
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
itemClickBlock!(collectionView.tag,indexPath.row)
}
如下图: