Swift Settings picture timed gestures carousel, ScrollView and modify Pagecontrol button color

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.