• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

swift开发之--自动轮播图(UIScrollView+UIPageControl+Timer)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

比较简单,原理就不说了,这里只做记录:

代码如下:

1,准备

var pageControl:UIPageControl?
    var myscrollView:UIScrollView?
    var myTimer:Timer?
    var mycurrentPage:NSInteger?
    
    
    var courses = [
        ["name":"first","pic":"1.jpeg"],
        ["name":"second","pic":"2.jpeg"],
        ["name":"third","pic":"3.jpeg"]
    ]

2,内容的添加

 //添加scrollview
        self.addScrollView()
        //添加图片
        self.addImages()
        //添加pagecontrol
        self.addPageControl()
        
        mycurrentPage = 1
        
        //添加定时器
        self.setupTimer()

具体方法:

func addScrollView(){
        self.myscrollView = UIScrollView()
        self.myscrollView?.frame = CGRect(x:0,y:0,width:kScreenWidth,height:kScreenHeight/2)
        self.myscrollView?.contentSize = CGSize(width:kScreenWidth*3,height:kScreenHeight/2)
        self.myscrollView?.showsHorizontalScrollIndicator = false
        self.myscrollView?.showsVerticalScrollIndicator = false
        self.myscrollView?.scrollsToTop = false
        self.myscrollView?.delegate = self
        self.myscrollView?.isPagingEnabled = true
        self.view.addSubview(self.myscrollView!)

    }
    
    func addImages(){
        let size = self.myscrollView?.bounds.size
        for (seq,course) in courses.enumerated() {
            let page = UIView(frame:CGRect(x:0,y:64,width:kScreenWidth,height:kScreenHeight/2))
            let imageView=UIImageView(image:UIImage(named:course["pic"]!))
            imageView.frame = page.frame
            page.addSubview(imageView)
            page.backgroundColor = UIColor.gray
            let lab1 = UILabel(frame:CGRect(x:15,y:kScreenHeight/2-100,width:kScreenWidth-30,height:40))
            lab1.textAlignment = .center
            lab1.backgroundColor = UIColor.red
            lab1.text = course["name"]
            page.addSubview(lab1)
            
            page.frame = CGRect(x:CGFloat(seq)*(size?.width)!,y:0,width:(size?.width)!,height:(size?.height)!)
            self.myscrollView?.addSubview(page)
            
        }
    }
    
    func addPageControl(){
        self.pageControl = UIPageControl(frame:CGRect(x:kScreenWidth/2-50/2,y:kScreenHeight/2-40,width:50,height:35))
        self.pageControl?.backgroundColor = UIColor.blue
        self.pageControl?.numberOfPages = courses.count
        self.view.addSubview(self.pageControl!)
    }
    
    func setupTimer() {
        self.myTimer = Timer.init(timeInterval: 2.0, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true)
        RunLoop.main.add(self.myTimer!, forMode: RunLoopMode.defaultRunLoopMode)
        
//        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2) { 
//            self.myTimer?.fire()
//        }
        
    }
    
    func closeTimer(){
        self.myTimer?.invalidate()
        self.myTimer = nil
    }
    
    func timerAction(){
       self.pageControl?.currentPage += mycurrentPage!
        
        if self.pageControl?.currentPage == 0 || self.pageControl?.currentPage == 2 {
            mycurrentPage = -mycurrentPage!
        }
        self.myscrollView?.contentOffset = CGPoint(x:CGFloat(kScreenWidth)*CGFloat((pageControl?.currentPage)!),y:0)
    }

3,一些代理方法的实现,手动拖动的时候,销毁定时器,节省内存

internal func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let page = Int(scrollView.contentOffset.x/scrollView.frame.size.width)
        self.pageControl?.currentPage = page
        
    }
    
    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        self.closeTimer()
    }
    
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        self.setupTimer()
    }
    
    func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {
        self.pageControl?.currentPage = Int(scrollView.contentOffset.x/kScreenWidth)
    }

效果如下:

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Swift入门学习之一常量,变量和声明发布时间:2022-07-18
下一篇:
Swift实战-豆瓣电台(一)准备发布时间:2022-07-18
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap