//
//GMShufflingView.swift
//GreenMoore
//
//Created by汤义on 16/4/19.
//Copyright © 2016年汤义. All rights reserved.
//
importUIKit
classGMShufflingView:UIView,UIScrollViewDelegate{
privatevarpageControl :UIPageControl?
privatevarpageWH :Int=70
privatevarscrollView :UIScrollView?
privatevarpage :Int?
privatevartimer :NSTimer?
overrideinit(frame:CGRect) {
super.init(frame: frame)
self.backgroundColor=UIColor.redColor()
self.initScrollView()
}
privatefuncinitScrollView() {
ifscrollView==nil{
scrollView=UIScrollView.init(frame:CGRectMake(0,0,SCREEN_WH,self.frame.size.height))
}
scrollView!.delegate=self
self.addSubview(scrollView!)
//设置隐藏横向条
scrollView!.showsVerticalScrollIndicator=false
//设置自动分页
scrollView!.pagingEnabled=true
//图片的宽
letimagW :CGFloat=scrollView!.frame.size.width
//图片高
letimageH :CGFloat=scrollView!.frame.size.height
//图片的Y
letimageY :CGFloat=0
//设置scrollview的滚动范围
letcontentW :CGFloat=5* imagW
scrollView!.contentSize=CGSizeMake(contentW,0)
fortotalCountin0...4{
print(totalCount)
letimageView :UIImageView=UIImageView.init()
letimageX :CGFloat=CGFloat(Float(totalCount) *Float(imagW))
//print("我的数据是多少:%@",imageX)
imageView.frame=CGRectMake(imageX, imageY, imagW, imageH)
//图片名称
letimageName :String="tu_\(totalCount +1)"
imageView.image=UIImage(named:imageName )
scrollView!.showsHorizontalScrollIndicator=false
scrollView!.backgroundColor=UIColor.yellowColor()
scrollView!.addSubview(imageView)
}
ifpageControl==nil{
pageControl=UIPageControl.init(frame:CGRectMake((SCREEN_WH/2) -CGFloat(pageWH),self.frame.size.height-30,CGFloat(pageWH*2),20))
}
pageControl?.numberOfPages=5
self.addSubview(pageControl!)
self.addTimer()
}
funcscrollViewDidScroll(scrollView:UIScrollView) {
letscrollviewW :CGFloat= scrollView.frame.size.width
letx :CGFloat= scrollView.contentOffset.x
//print("x的数据:%@",x)
page=Int((x + scrollviewW) / scrollviewW)
//print("page的数据:%@",page)
pageControl?.currentPage=page! -1
}
functimers() ->Void{
varpage :Int= (self.pageControl?.currentPage)!
ifpage ==4{
page =0
}else{
page +=1
}
letx :CGFloat=CGFloat(page) * (self.scrollView?.frame.size.width)!
UIView.animateWithDuration(1) {
self.scrollView?.contentOffset=CGPointMake(x,0)
}
}
funcscrollViewWillBeginDragging(scrollView:UIScrollView) {
self.removeTimer()
}
funcscrollViewDidEndDragging(scrollView:UIScrollView, willDecelerate decelerate:Bool) {
}
funcaddTimer() ->Void{
self.timer?.invalidate()
timer=NSTimer.scheduledTimerWithTimeInterval(2.0, target:self, selector:#selector(timers), userInfo:nil, repeats:true)
}
//设置定时器
funcremoveTimer() ->Void{
self.timer?.invalidate()
}
requiredinit?(coder aDecoder:NSCoder) {
fatalError("init(coder:) has not been implemented")
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
// Drawing code
}
*/
}