其他的因为csdn系统错误被吃掉了,以后再补录
6.8 Header 和 feeter
用这个delegate设置header和feeter
override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath)
代码:
override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
var identifier = "header"
if kind == UICollectionElementKindSectionFooter {
identifier = "footer"
}
let view = collectionView.dequeueReusableCellWithReuseIdentifier(identifier, forIndexPath: indexPath)
if kind == UICollectionElementKindSectionHeader {
if let header = view as? HeaderCollectionReusableView {
header.myLabel.text = "Section Header"
}
} else if kind == UICollectionElementKindSectionFooter {
if let footer = view as? FooterCollectionReusableView {
let title = "Section Footer"
footer.myButton.setTitle(title, forState: UIControlState.Normal)
}
}
}
6.9 添加custom interaction
iOS已经添加了一些手势给collection view,如果要添加新的手势,首先要保证新的手势不会影响已经存在的手势。在添加新手势前,要用requireGestureRecognizerToFail判断一下。
let pinch = UIPinchGestureRecognizer(target: self, action: "handlePinches:")
for recognizer in collectionView.gestureRecognizers as! [UIGestureRecognizer]{
if recognizer is UIPinchGestureRecognizer {
recognizer.requireGestureRecognizerToFail(pinch)
}
}
collectionView?.addGestureRecognizer(pinch)
如果想要改变collection cell的大小,需要改变collectionViewLayout,然后再invalidate它使他刷新:
func handlePinches(pinch: UIPinchGestureRecognizer) {
let defaultLayoutItemSize = CGSize(width: 80, height: 120)
let layout = collectionView?.collectionViewLayout as! UICollectionViewFlowLayout
layout.itemSize = CGSize(width: defaultLayoutItemSize.width * pinch.scale, height: defaultLayoutItemSize.height * pinch.scale)
layout.invalidateLayout()
}