美文网首页
(Swift4.0)App引导页封装

(Swift4.0)App引导页封装

作者: rightmost | 来源:发表于2018-08-29 11:03 被阅读0次

    1.自定义引导页控制器

    import UIKit

    let kScreenWidth = UIScreen.main.bounds.size.width

    let kScreenHeight = UIScreen.main.bounds.size.height

    letkHighlighted =UIColor.init(red:230/255.0, green:230/255.0, blue:230/255.0, alpha:1.0)

    letkBtnBackgroundColor =UIColor.clear

    classCCPageControlSwift:UIView

    ,UICollectionViewDelegate

    ,UICollectionViewDataSource,

    UICollectionViewDelegateFlowLayout{

        //分区数

        funcnumberOfSectionsInCollectionView(collectionView:UICollectionView) ->Int{

            return1;

        }

        //每个分区含有的 item 个数

        funccollectionView(_collectionView:UICollectionView, numberOfItemsInSection section:Int) ->Int{

            return self.imageListCount!;

        }

        //每个分区的内边距

        funccollectionView(_collectionView:UICollectionView, layout collectionViewLayout:UICollectionViewLayout, insetForSectionAt section:Int) ->UIEdgeInsets{

            returnUIEdgeInsetsMake(0,0,0,0);

        }

        //最小 item 间距

        funccollectionView(_collectionView:UICollectionView, layout collectionViewLayout:UICollectionViewLayout, minimumInteritemSpacingForSectionAt section:Int) ->CGFloat{

            return0;

        }

        //最小行间距

        funccollectionView(_collectionView:UICollectionView, layout collectionViewLayout:UICollectionViewLayout, minimumLineSpacingForSectionAt section:Int) ->CGFloat{

            return0;

        }

        //item 的尺寸

        funccollectionView(_collectionView:UICollectionView, layout collectionViewLayout:UICollectionViewLayout, sizeForItemAt indexPath:IndexPath) ->CGSize{

            returnCGSize.init(width: collectionView.frame.size.width, height: collectionView.frame.size.height)

        }

        funccollectionView(_collectionView:UICollectionView, cellForItemAt indexPath:IndexPath) ->UICollectionViewCell{

            letcell = collectionView.dequeueReusableCell(withReuseIdentifier:pageControlCellId, for: indexPath)as!PageControlCollectionViewCell;

            cell.imageV?.image=UIImage(named:self.imageList![indexPath.row]as!String)

            returncell

        }

        /*

        // Only override draw() if you perform custom drawing.

        // An empty implementation adversely affects performance during animation.

        override func draw(_ rect: CGRect) {

            // Drawing code

        }

        */

        varcollectionView :UICollectionView?

        varpageControl :UIPageControl?

        varcancelBtn :UIButton?

        letpageControlCellId ="cellid"

        varimageListCount :Int?

        varimageList :Array?

        overrideinit(frame:CGRect){

            super.init(frame: frame)

        }

        typealiaspageNumBlock = (_selectIndex :Int) ->Void

        varpageBlock:pageNumBlock?

        typealiastouchBtnBlock = () ->Void

        vartouchBlock:touchBtnBlock?

        classfuncSetupPageControlView(imagesList :Array , block :pageNumBlock? , touchBlock :touchBtnBlock?){

            letpageControlView =CCPageControlSwift.init(frame:CGRect.init(x:0, y:0, width:kScreenWidth, height:kScreenHeight))

            pageControlView.backgroundColor= .red

            UIApplication.shared.keyWindow?.addSubview(pageControlView)

            pageControlView.setValues(imageList: imagesList)

            pageControlView.setupUI()

            pageControlView.pageBlock= block

            pageControlView.touchBlock= touchBlock

        }

        funcsetValues(imageList :Array) {

            self.imageListCount= imageList.count

            self.imageList= imageList

        }

        funcsetupUI(){

            let flowLayout = UICollectionViewFlowLayout()

            flowLayout.scrollDirection= .horizontal

            collectionView=UICollectionView.init(frame:CGRect.init(x:0, y:0, width:self.frame.size.width, height:self.frame.size.height),collectionViewLayout:flowLayout)

            collectionView!.backgroundColor = .white

            collectionView!.isPagingEnabled = true

            collectionView!.bounces = false

            collectionView!.delegate = self

            collectionView!.dataSource = self

            collectionView!.showsVerticalScrollIndicator = false

            collectionView!.showsHorizontalScrollIndicator = false

            collectionView!.register(PageControlCollectionViewCell.self, forCellWithReuseIdentifier: pageControlCellId)

            self.addSubview(collectionView!);

            pageControl=UIPageControl(frame:CGRect.init(x:0, y:0, width:self.frame.size.width, height:30))

            pageControl!.center = CGPoint.init(x: self.frame.size.width/2, y: self.frame.size.height - (pageControl?.frame.size.height)!)

            pageControl!.backgroundColor = .clear

            pageControl!.numberOfPages = self.imageListCount!

            pageControl!.currentPage=0

            //设置单页时隐藏

            pageControl!.hidesForSinglePage = true

            //设置显示颜色

            pageControl!.currentPageIndicatorTintColor = .green

            //设置页背景指示颜色

            pageControl!.pageIndicatorTintColor = .lightGray

            self.addSubview(pageControl!)

            cancelBtn=UIButton(frame:CGRect.init(x:0, y:0, width:self.frame.size.width*0.5, height:self.frame.size.height*0.08))

            cancelBtn?.center = CGPoint.init(x: self.frame.size.width/2, y: (self.pageControl?.frame.origin.y)! - (self.cancelBtn?.frame.size.height)!/2-10)

            cancelBtn?.backgroundColor = kBtnBackgroundColor

            cancelBtn?.layer.masksToBounds = true

            cancelBtn?.layer.cornerRadius = (cancelBtn?.frame.size.height)!/2

            cancelBtn?.layer.borderWidth = 1.0;

            cancelBtn?.layer.borderColor = UIColor.white.cgColor;

            cancelBtn?.setTitle("立即体验", for: .normal)

            cancelBtn?.addTarget(self, action:#selector(buttonBackGroundHighlighted(_:)), for: .touchDown)

            cancelBtn?.addTarget(self, action:#selector(removeViewBtn(_:)), for: .touchUpInside)

            self.addSubview(cancelBtn!)

            ifself.imageListCount==1{

                cancelBtn?.isHidden=false

            }

            else

            {

                cancelBtn?.isHidden=true

            }

        }

        @objcfuncbuttonBackGroundHighlighted(_btn :UIButton) {

            btn.backgroundColor = kHighlighted

        }

        @objcfuncremoveViewBtn(_btn :UIButton) {

            btn.backgroundColor = kBtnBackgroundColor

            touchBlock?()

            self.removeFromSuperview()

        }

        funcscrollViewDidEndDecelerating(_scrollView:UIScrollView) {

            //通过scrollView内容的偏移计算当前显示的是第几页

            letpage =Int(scrollView.contentOffset.x/ scrollView.frame.size.width)

            //设置pageController的当前页

            pageControl?.currentPage= page

            ifpage ==self.imageListCount!-1{

    //            print("最后一页了")

            }

            pageBlock?(page)

        }

        funcscrollViewDidScroll(_scrollView:UIScrollView) {

            if(Int)(scrollView.contentOffset.x/self.frame.size.width) == (self.imageList?.count)!-1{

                cancelBtn?.isHidden=false

            }

            else

            {

                cancelBtn?.isHidden=true

            }

        }

        requiredinit?(coder aDecoder:NSCoder) {

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

        }

    }

    2.自定义引导页控制器cell

    import UIKit

    classPageControlCollectionViewCell:UICollectionViewCell{

        varimageV :UIImageView?

        overrideinit(frame:CGRect) {

            super.init(frame: frame);

             imageV=UIImageView(frame:CGRect.init(x:0, y:0, width:self.frame.size.width, height:self.frame.size.height))

            self.addSubview(imageV!);

        }

        requiredinit?(coder aDecoder:NSCoder) {

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

        }

    }

    3.在使用的地方调用即可

    import UIKit

    classViewController:UIViewController{

     varsaveImageList = [String]()

          varimageList :[String] = ["image1","image2","image3"]

        overridefuncviewDidLoad() {

            super.viewDidLoad()

            CCPageControlSwift.SetupPageControlView(imagesList:imageList, block: { (pageNum)in

                print("第\(pageNum+1)页")

            }) {

                print("点击了Button")

            }

            // Do any additional setup after loading the view, typically from a nib.

        }

        overridefuncdidReceiveMemoryWarning() {

            super.didReceiveMemoryWarning()

            // Dispose of any resources that can be recreated.

        }

    }

    相关文章

      网友评论

          本文标题:(Swift4.0)App引导页封装

          本文链接:https://www.haomeiwen.com/subject/fxehwftx.html