SWFIT3中banner定制2

在完成上述工作后,就可以进入流程,首先我们确定初始化定时器后应该在什么时候启动定时器,很明显要有接口传入的数据后才开始启动,所以需要一个有接收并判断的接口,这里我们用接收广告的数量来实现

    //MARK:设置总页数后,启动动画  
    func setTotalPagesCount(totalPageCout: (()->Int)) {
        self.totalPages = totalPageCout()
        print("totalPages = \(self.totalPages)")
        
        self.pageControl?.numberOfPages = self.totalPages!
        self.currentPageIndex = 0
        if self.totalPages == 1 {
            scrollView?.contentSize = CGSize(width: MAIN_WIDTH, height: self.bounds.size.height)
            configureContentViews()
            self.pageControl?.isHidden = true//如果只有一页不需要显示pageControl
            
        }else{
            self.pageControl?.isHidden = false
        }
        if self.totalPages! > 0 && self.totalPages! != 1 {
            configureContentViews()
            self.animationTimer?.resumeTimerAfterInterval(self.animationInterval!)
        }
        
        
    }

定时器启动

    func animationTimerDidFire(timer:Timer){
        let index = Int((self.scrollView?.contentOffset.x)!/MAIN_WIDTH)//防止视图偏移
        self.scrollView?.setContentOffset(CGPoint(x: MAIN_WIDTH * CGFloat(index)+MAIN_WIDTH, y: 0),animated: true)
        
    }

获取数据

    //获取数据
    func setScrollViewDataSource () {
        let previousIndex = validateNextPageIndexWithPageIndex(index: self.currentPageIndex! - 1)
        let rearIndex = validateNextPageIndexWithPageIndex(index: self.currentPageIndex! + 1)
 
        self.contentViews.removeAll()
        
        if self.contentViewAtIndex != nil {
            self.contentViews.append(self.contentViewAtIndex!(previousIndex))//获取数据的接口
            self.contentViews.append(self.contentViewAtIndex!(currentPageIndex!))
            self.contentViews.append(self.contentViewAtIndex!(rearIndex))
            
        }
        
    }

点击响应

    //点击事件 
    func contentViewTapped(sender: UIGestureRecognizer){
        if self.tapActionBlock != nil {
            self.tapActionBlock!(self.currentPageIndex!)
        }
        
    }

此外一些常规的操作依赖于scollView的delegate来完成

    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        self.animationTimer?.pauseTimer()
    }
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        self.animationTimer?.resumeTimerAfterInterval(self.animationInterval!)
    }
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if scrollView.contentOffset.x >= (MAIN_WIDTH * CGFloat(2)) {
            self.currentPageIndex = validateNextPageIndexWithPageIndex(index:  self.currentPageIndex!+1)
            configureContentViews()
        }else if scrollView.contentOffset.x <= 0 {
            self.currentPageIndex = validateNextPageIndexWithPageIndex(index:  self.currentPageIndex!-1)
            configureContentViews()
        }
        
        self.pageControl?.currentPage = self.currentPageIndex!
    }
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        scrollView.setContentOffset(CGPoint(x: MAIN_WIDTH, y: 0), animated: true)
    }

ok,这就完成了一个banner的简单封装,如果有不足谢谢大家指导

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,908评论 18 139
  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,209评论 30 471
  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 11,141评论 6 13
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,227评论 25 708
  • 我写下 星辰飘洒的规律 写下 种子和时针的距离 写下 当下的痛楚 和恒久的幸福 甚至写下 被爱标注的 权力和义务 ...
    乱石斋主阅读 432评论 5 2