仿iOS系统相册浏览样式

一、效果

仿iOS系统相册浏览样式_第1张图片如图所示,可以左右滑动,上下联动效果

 

二、代码

1.TopCollectionView

class TopCollectionView: UICollectionView ,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UIScrollViewDelegate{

    private var myContext = 0

    //这个是控制何时发送通知的

    var isManul:Bool = true

    var currentIndex:NSInteger = 0{

        didSet{

            if self.isManul==true {

                NotificationPost(CURRENTINDEX_TOP, currentIndex as AnyObject, userinfo: nil)

            }

        }

    }

    var date:NSArray = [1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9]

 

    convenience init() {

        let flowLayOut = UICollectionViewFlowLayout.init()

        flowLayOut.scrollDirection = .horizontal

        flowLayOut.minimumLineSpacing = 0

        self.init(frame: CGRect.zero, collectionViewLayout: flowLayOut)

    }

 

   private override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {

        super.init(frame: frame, collectionViewLayout: layout)

        self.delegate = self

        self.dataSource = self

        self.backgroundColor = ColorClear

        self.layer.cornerRadius = AdWidth(10)

        self.clipsToBounds = true

        self.isPagingEnabled = true

        self.register(PictureCollectionCell.self, forCellWithReuseIdentifier: "PictureCollectionCell")

        NotificationRegister(CURRENTINDEX_BOTTOM, observer: self, selector: #selector(itemScroll(noti:)), object: nil)

 

    }

    

    required init?(coder aDecoder: NSCoder) {

        fatalError("init(coder:) has not been implemented")

    }

 

 

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return date.count

    }

    

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell:PictureCollectionCell = collectionView.dequeueReusableCell(withReuseIdentifier: "PictureCollectionCell", for: indexPath) as! PictureCollectionCell

        cell.setdateWithModel(currentIndex: indexPath.row)

        return cell

        

    }

    

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

        return CGSize.init(width: self.bounds.size.width, height: self.bounds.size.height)

    }

    

    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {

        self.isManul = true

        let middleIndexPath = self.indexPathForItem(at: CGPoint.init(x: scrollView.contentOffset.x, y: self.frame.size.height/2))

        if middleIndexPath!.row != nil {

            self.currentIndex  = middleIndexPath!.row

        }

    }

    

    @objc func itemScroll(noti:NSNotification){

        self.isManul = false

        self.currentIndex = noti.object as! NSInteger

        print("topViewCurrentInderx:\(currentIndex)")

        self.selectItem(at: IndexPath.init(row: noti.object as! NSInteger, section: 0), animated: true, scrollPosition: .centeredHorizontally)

    }

    

}

 

 

2.BottomCollectionView

class BottomCollectionView: UIView ,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UIScrollViewDelegate{

    var collectionView:UICollectionView!

    private var myContext = 0

    var currentIndex:NSInteger = 0{

        didSet{

            if self.isManul==true {

                NotificationPost(CURRENTINDEX_BOTTOM, currentIndex as AnyObject, userinfo: nil)

            }

        }

    }

    let itemWidth = AdWidth(113)

    let itemHeight = AdHeight(85)

    var date:NSArray = [1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9]

    //这个是控制何时发送通知的

    var isManul:Bool = true

    

    

    convenience init() {

        self.init(frame: CGRect.zero)

        let flowLayOut = UICollectionViewFlowLayout.init()

        flowLayOut.scrollDirection = .horizontal

        flowLayOut.minimumLineSpacing = 0

        collectionView = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: flowLayOut)

        self.addSubview(collectionView)

        collectionView.delegate = self

        collectionView.dataSource = self

        collectionView.clipsToBounds = true

        collectionView.backgroundColor = ColorClear

        collectionView.isPagingEnabled = true

        collectionView.register(PictureCollectionCell.self, forCellWithReuseIdentifier: "PictureCollectionCell")

        collectionView.contentInset = UIEdgeInsets.init(top: 0, left: itemWidth*4.5, bottom: 0, right: itemWidth*4.5)

        collectionView.contentOffset = CGPoint.init(x: -1, y: 0)

        collectionView.mas_makeConstraints { (make) in

            make?.edges.mas_equalTo()(UIEdgeInsets.init(top: 0, left: AdWidth(64), bottom: 0, right: AdWidth(64)))

        }

        NotificationRegister(CURRENTINDEX_TOP, observer: self, selector: #selector(itemScroll(noti:)), object: nil)

        

    }

    

    @objc func itemScroll(noti:NSNotification){

        self.isManul = false

        self.currentIndex = noti.object as! NSInteger

        print("bottomViewCurrentInderx:\(currentIndex)")

        collectionView.selectItem(at: IndexPath.init(row: noti.object as! NSInteger, section: 0), animated: true, scrollPosition: .centeredHorizontally)

    }

    private override init(frame: CGRect) {

        super.init(frame: frame)

        self.backgroundColor = Color(138, 171, 208)

        self.addPaddingView()

    }

    

    func addPaddingView() {

        let previousBtn = UIButton.init(type: .custom)

        self.addSubview(previousBtn)

        previousBtn.setImage(UIImage.init(named: "course_arrow1"), for: .normal)

        previousBtn.addTarget(self, action: #selector(previousBtnClick), for: .touchUpInside)

        previousBtn.mas_makeConstraints { (make) in

            make?.left.equalTo()(self.mas_left)?.offset()(AdWidth(21))

            make?.centerY.equalTo()(self.mas_centerY)

            make?.width.mas_equalTo()(AdWidth(19))

            make?.height.mas_equalTo()(AdHeight(36))

        }

        previousBtn.setMinHitTestWidth(minHitTestWidth: AdWidth(50))

        previousBtn.setMinHitTestHeight(minHitTestHeight: AdHeight(40))

        

        let nextBtn = UIButton.init(type: .custom)

        self.addSubview(nextBtn)

        nextBtn.setImage(UIImage.init(named: "course_arrow2"), for: .normal)

        nextBtn.addTarget(self, action: #selector(nextBtnClick), for: .touchUpInside)

        nextBtn.mas_makeConstraints { (make) in

            make?.right.equalTo()(self.mas_right)?.offset()(-AdWidth(21))

            make?.centerY.equalTo()(self.mas_centerY)

            make?.width.mas_equalTo()(AdWidth(19))

            make?.height.mas_equalTo()(AdHeight(36))

        }

        nextBtn.setMinHitTestWidth(minHitTestWidth: AdWidth(50))

        nextBtn.setMinHitTestHeight(minHitTestHeight: AdHeight(40))

        

    }

    

    @objc func previousBtnClick(){

        print("上一页")

        self.isManul = true

        currentIndex = currentIndex==0 ? currentIndex : currentIndex-1

        collectionView.selectItem(at: IndexPath.init(row: currentIndex, section: 0), animated: true, scrollPosition: .centeredHorizontally)

    }

    

    @objc func nextBtnClick(){

        print("下一页")

        self.isManul = true

        currentIndex = currentIndex==date.count-1 ? currentIndex : currentIndex+1

        collectionView.selectItem(at: IndexPath.init(row: currentIndex, section: 0), animated: true, scrollPosition: .centeredHorizontally)

 

    }

    

    required init?(coder aDecoder: NSCoder) {

        fatalError("init(coder:) has not been implemented")

    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return date.count

    }

    

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell:PictureCollectionCell = collectionView.dequeueReusableCell(withReuseIdentifier: "PictureCollectionCell", for: indexPath) as! PictureCollectionCell

        cell.setdateWithModel(currentIndex: indexPath.row)

        return cell

        

    }

    

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

        return CGSize.init(width: itemWidth, height: itemHeight)

    }

    

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        collectionView.selectItem(at: IndexPath.init(row: indexPath.row, section: 0), animated: true, scrollPosition: .centeredHorizontally)

    }

    

    func scrollViewDidScroll(_ scrollView: UIScrollView) {

        let middleIndexPath = collectionView.indexPathForItem(at: CGPoint.init(x: scrollView.contentOffset.x+itemWidth*4.5, y: collectionView.frame.size.height/2))

 

        if ((middleIndexPath?.row) != nil) {

            self.currentIndex = (middleIndexPath?.row)!

            for cell in collectionView.visibleCells {

                (cell as! PictureCollectionCell).setCenterItemStyle(isCenter: collectionView.indexPath(for: cell)==middleIndexPath)

            }

        }

        

    }

  

    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {

       

    }

    func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer) {

        self.isManul = true

    }

}

 

3.PictureCollectionCell

class PictureCollectionCell: UICollectionViewCell {

    var titleLabel:UILabel!

    

    override init(frame: CGRect) {

        super.init(frame: frame)

        self.backgroundColor = RandomColor()

        self.layer.cornerRadius = AdWidth(10)

        self.clipsToBounds = true

        self.createUI()

    }

    

    required init?(coder aDecoder: NSCoder) {

        fatalError("init(coder:) has not been implemented")

    }

    

    func createUI() {

        titleLabel = UILabel.init()

        self.addSubview(titleLabel)

        titleLabel.textColor = ColorWhite

        titleLabel.mas_makeConstraints { (make) in

            make?.centerX.equalTo()(self.mas_centerX)

            make?.centerY.equalTo()(self.mas_centerY)

        }

    }

    

    func setdateWithModel(currentIndex:NSInteger) {

        titleLabel.text = currentIndex.description

       

    }

    func setCenterItemStyle(isCenter:Bool) {

        if isCenter==true {

            self.layer.borderWidth = 1

            self.layer.borderColor = ColorWhite.cgColor

        }else{

            self.layer.borderWidth = 1

            self.layer.borderColor = ColorClear.cgColor

        }

 

    }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(ios,swift)