码迷,mamicode.com
首页 > 编程语言 > 详细

swift 设置图片定时手势轮播,scrollview及修改pageControl按钮颜色

时间:2015-03-08 20:00:01      阅读:4393      评论:0      收藏:0      [点我收藏+]

标签:

首先: 设置使用UIScrollViewDelegate代理协议

其次需要初始化全局变量:

var picArray = [String]()   //图片数量
    let scrollview = UIScrollView()
    let pageControl = UIPageControl()
    var timer = NSTimer()

再次: 设定scrollview 与pageControl 的frame :

接着,循环图片,添加到scrollview中:

for(var i=0;i<self.picArray.count;i++){
                            //show pic scroll
                            let imageVc = UIImageView()
                            let imageX = CGFloat(i*400)
                            imageVc.frame = CGRectMake(imageX, 0, 400, 400)
                            imageVc.image = UIImage(named:picArray[i])
                            self.scrollview.showsHorizontalScrollIndicator = false
                            self.scrollview.addSubview(imageVc)
                         }

再接着设置样式:

self.scrollview.contentSize = CGSizeMake(CGFloat(400*self.picArray.count), 0)
                        self.scrollview.pagingEnabled = true
                        self.scrollview.delegate = self
                        self.mainView.addSubview(self.scrollview)
                        self.pageControl.frame = CGRectMake(270+31, 370+50, 130, 20)
                        self.pageControl.currentPageIndicatorTintColor = UIColor(rgb: 0x288dcd)
                        self.pageControl.pageIndicatorTintColor = UIColor(rgb: 0xd9d9d9)
                        self.pageControl.currentPage = 0
                        self.pageControl.numberOfPages = self.picArray.count
                        self.mainView.addSubview(self.pageControl)
                        self.addTimer()

然后,写scrollview代理函数:

    func scrollViewDidScroll(scrollView: UIScrollView) {
        let scrollviewW =  scrollView.frame.size.width
        var page = Int((scrollview.contentOffset.x + CGFloat( scrollviewW / 2)) / scrollviewW)
        self.pageControl.currentPage = page
    }
    func scrollViewWillBeginDragging(scrollView: UIScrollView) {
        self.removeTimer()
    }
    func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        self.addTimer()
    }

最后增加定时器:

 //开启定时器
    func addTimer(){
        self.timer = NSTimer.scheduledTimerWithTimeInterval(2, target: self, selector: "nextImage", userInfo: nil, repeats: true)
    }
    //关闭定时器
    func removeTimer(){
        self.timer.invalidate()
    }
    func nextImage(){
        var page = Int(self.pageControl.currentPage)
        //println(self.picArray.count)
        if (page == self.picArray.count-1){
            page = 0
        }else{
            page = page+1
        }
        self.scrollview.contentOffset = CGPointMake(CGFloat(page)*self.scrollview.frame.size.width, 0)
    }

 

 

 

参考文章: iOS开发UI篇—UIScrollView控件实现图片轮播

如何使在Swift中使用UIScrollView进行滚动和缩放

swift 设置图片定时手势轮播,scrollview及修改pageControl按钮颜色

标签:

原文地址:http://www.cnblogs.com/ericjun/p/4322095.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!