一 前言
1.之前一直在用OC编程,最近才开始接触使用swift就发现使用OC越来越不习惯,感觉已经爱上了swift。
2.这个自定义轮播图只是对之前OC版本进行了翻译,欢迎指正。
3.我决定一步步适应swift编程,首先从翻译原来的代码开始。
好啦,马上附上文件记录
let SW = UIScreen.mainScreen().bounds.size.width let SH = UIScreen.mainScreen().bounds.size.height
import Foundation
import UIKit class ZLScrollView: UIView ,UIScrollViewDelegate { var scrollview :UIScrollView!
var pageControl :UIPageControl!
var pageBgView :UIImageView!
var pageCount :NSInteger! var imageArray :NSArray! var leftImage :UIImageView!
var centerImage :UIImageView!
var rightImage :UIImageView! var timer :NSTimer!
var isAutoMove :Bool! var urlsArray :NSArray! var selfHeight :CGFloat! override init(frame: CGRect) { super.init(frame: frame) self.frame=frame selfHeight=frame.size.height pageCount= createScrollView()
} func initWithImage(array images : NSArray, array urls : NSArray ) { urlsArray=urls if images.count== {
return
}
imageArray=NSArray.init(array: images) if imageArray.count== {
pageBgView.hidden=true
pageControl.hidden=true let image = imageArray[].allValues[] print(image.image) centerImage.image = getImage(imageView: (imageArray.lastObject?.allValues[])! as! UIImageView)
scrollview.scrollEnabled=false
}else if imageArray.count>= {
pageBgView.hidden=false
pageControl.hidden=false
leftImage.image = getImage(imageView: (imageArray.lastObject?.allValues[])! as! UIImageView)
centerImage.image = getImage(imageView: (imageArray[].allValues[]) as! UIImageView)
rightImage.image = getImage(imageView: (imageArray[].allValues[]) as! UIImageView)
scrollview.scrollEnabled=true
pageBgView.bounds=CGRectMake(, , CGFloat(imageArray.count)*/375.0*SW, /375.0*SW)
setTimer()
}
pageControl.numberOfPages = imageArray.count
} func getImage(imageView imageView:UIImageView) -> UIImage { if imageView.image==nil {
return UIImage(named: "tpjz")!
}else{
return imageView.image!
}
} func pauseTimer() { if (timer != nil) {
if timer!.valid {
timer!.invalidate()
}
}
} func getLoadImage(urlArray :NSArray, headScroll :ZLScrollView) {
let imageArr = NSMutableArray() for i in ...urlArray.count- { let cachedImage = SDImageCache.sharedImageCache().imageFromDiskCacheForKey(urlArray[i] as! String) let imageView = UIImageView(frame: CGRectMake(, , SW, self.frame.height)) if (cachedImage != nil) {
imageView.image=cachedImage
}
else{
imageView.sd_setImageWithURL(NSURL(string :String(urlArray[i])))
} let imageDic = [ String(urlArray[i]) : imageView ] imageArr.addObject(imageDic) initWithImage(array: imageArr, array: urlArray)
} } func resumeTimer() {
if (timer != nil) {
if timer!.valid {
timer!.invalidate()
}
}
} func setTimer() {
if timer==nil {
timer = NSTimer.scheduledTimerWithTimeInterval(, target: self, selector: #selector(ZLScrollView.scrorllViewWithTimerAction), userInfo: nil, repeats: true)
}
} func scrorllViewWithTimerAction() {
scrollview.contentOffset = CGPointMake(*SW, )
isAutoMove = true
self.performSelector(#selector(ZLScrollView.scrollViewDidEndDecelerating), withObject: scrollview, afterDelay: )
} func createScrollView() { scrollview=UIScrollView()
scrollview.frame=self.bounds
scrollview.showsVerticalScrollIndicator = false
scrollview.showsHorizontalScrollIndicator = false
scrollview.delegate = self
scrollview.pagingEnabled = true
self.addSubview(scrollview)
scrollview.contentSize = CGSizeMake(SW*, selfHeight)
scrollview.setContentOffset(CGPointMake(SW, ), animated: false) pageBgView=UIImageView()
pageControl=UIPageControl()
pageControl.frame=CGRectMake(SW/, self.frame.height-/375.0*SW, SW/, /375.0*SW)
pageBgView.center=CGPointMake(SW/, self.frame.height-9.5/375.0*SW)
pageBgView.bounds=CGRectMake(, , /375.0*SW, /375.0*SW)
pageBgView.image=UIImage(named: "icon_bg_tu")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
self.addSubview(pageBgView)
pageControl.pageIndicatorTintColor=UIColor.whiteColor()
pageControl.currentPageIndicatorTintColor=UIColor.orangeColor()
pageControl.backgroundColor=UIColor.clearColor()
pageControl.alpha=0.5
self.addSubview(pageControl) leftImage=UIImageView(frame:CGRectMake(, , SW, selfHeight))
centerImage=UIImageView(frame:CGRectMake(SW, , SW, selfHeight))
centerImage.backgroundColor=UIColor.redColor()
rightImage=UIImageView(frame:CGRectMake(SW*, , SW, selfHeight))
scrollview.addSubview(leftImage)
scrollview.addSubview(centerImage)
scrollview.addSubview(rightImage) let tapGesture = UITapGestureRecognizer(target: self, action: #selector(ZLScrollView.imageViewTapGestureAction))
scrollview.addGestureRecognizer(tapGesture)
} typealias SelectImageBlcok = Int -> Void
var selectImageBlcok : SelectImageBlcok! func imageViewTapGestureAction() {
let dic = imageArray[pageCount] for i in ...urlsArray.count- {
let str = urlsArray[i] if String(str)==String(dic.allKeys[]) { self.selectImageBlcok(i)
} }
} func scrollViewDidEndDecelerating(scrollView: UIScrollView) { if scrollview.contentOffset.x > SW {
pageCount=(pageCount+)%imageArray.count
}else if scrollview.contentOffset.x < SW {
pageCount=(pageCount-+imageArray.count)%imageArray.count
}
pageControl.currentPage = pageCount leftImage.image=getImage(imageView: (imageArray[(pageCount-+imageArray.count)%imageArray.count]).allValues[] as! UIImageView)
centerImage.image=getImage(imageView: (imageArray[(pageCount+imageArray.count)%imageArray.count]).allValues[] as! UIImageView)
rightImage.image=getImage(imageView: (imageArray[(pageCount++imageArray.count)%imageArray.count]).allValues[] as! UIImageView)
if isAutoMove==nil {
timer.fireDate.dateByAddingTimeInterval()
}
isAutoMove=false
} required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
} }