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的简单封装,如果有不足谢谢大家指导

你可能感兴趣的:(SWFIT3中banner定制2)