//
//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!)
//設(shè)置隱藏橫向條
scrollView!.showsVerticalScrollIndicator=false
//設(shè)置自動分頁
scrollView!.pagingEnabled=true
//圖片的寬
letimagW :CGFloat=scrollView!.frame.size.width
//圖片高
letimageH :CGFloat=scrollView!.frame.size.height
//圖片的Y
letimageY :CGFloat=0
//設(shè)置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("我的數(shù)據(jù)是多少:%@",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的數(shù)據(jù):%@",x)
page=Int((x + scrollviewW) / scrollviewW)
//print("page的數(shù)據(jù):%@",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)
}
//設(shè)置定時器
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
}
*/
}