Chapter 5:collection view - iOS 8 Swift Programming cookBook 读书笔记

其他的因为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()
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值