First: Set up using the uiscrollviewdelegate Proxy Protocol
Next, you need to initialize the global variables:
var picarray = [String] () // picture number let scrollview = uiscrollview () = Uipagecontrol () var timer = Nstimer ()
Again: Set the frame of ScrollView and Pagecontrol:
Next, loop the picture and add it to the ScrollView:
for(varI=0; i<self.picarray.count;i++){ //Show pic ScrollLet IMAGEVC =Uiimageview () let ImageX= CGFloat (i* -) Imagevc.frame= CGRectMake (ImageX,0, -, -) Imagevc.image=UIImage (Named:picarray[i]) Self.scrollview.showsHorizontalScrollIndicator=falseSelf.scrollview.addSubview (IMAGEVC)}
And then set the style:
Self.scrollview.contentSize = Cgsizemake (CGFloat ( -*self.picarray.count),0) self.scrollview.pagingEnabled=trueSelf.scrollview.Delegate=Self self.mainView.addSubview (self.scrollview) self.pageControl.frame /c7>= CGRectMake ( the+ to,370+ -, the, -) Self.pageControl.currentPageIndicatorTintColor= Uicolor (RGB:0X288DCD) Self.pageControl.pageIndicatorTintColor= Uicolor (RGB:0xd9d9d9) Self.pageControl.currentPage=0self.pageControl.numberOfPages=self.picArray.count Self.mainView.addSubview (Self.pagecontrol) Self.add Timer ()
Then, write the ScrollView agent function:
func scrollviewdidscroll (scrollview:uiscrollview) { = scrollView.frame.size.width var2))/ scrollvieww) = page } func scrollviewwillbegindragging (Scrollview:uiscrollview) { Self.removetimer () } func scrollviewdidenddragging (Scrollview:uiscrollview, Willdecelerate Decelerate:bool) { self.addtimer () }
Last Add Timer:
//Turn on Timerfunc AddTimer () {Self.timer= Nstimer.scheduledtimerwithtimeinterval (2, Target:self, selector:"NextImage", Userinfo:nil, repeats:true) } //Turn off the timerfunc Removetimer () {self.timer.invalidate ()} func NextImage () {varpage =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) }
Reference article: iOS Development UI Chapter-uiscrollview control implementation Picture Carousel
How to enable scrolling and zooming with Uiscrollview in Swift
Swift Set picture timed gestures carousel, ScrollView and modify Pagecontrol button color