Relatively simple, the principle is not said, here only record:
The code is as follows:
1, prepare
var pagecontrol:uipagecontrol?var myscrollview:uiscrollview?var mytimer:timer?var mycurrentpage:nsinteger?var courses= [ ["name":" First","pic":"1.jpeg"], ["name":"Second","pic":"2.jpeg"], ["name":"Third","pic":"3.jpeg"] ]
2, content add-on
// Add ScrollView Self.addscrollview () // add picture self.addimages () // Add Pagecontrol self.addpagecontrol () 1 // Add Timer Self.setuptimer ()
Specific methods:
func Addscrollview () {Self.myscrollview=Uiscrollview () Self.myscrollview?. frame = CGRect (x:0Y:0, width:kscreenwidth,height:kscreenheight/2) Self.myscrollview?. Contentsize = Cgsize (width:kscreenwidth*3, height:kscreenheight/2) Self.myscrollview?. Showshorizontalscrollindicator =falseSelf.myscrollview?. Showsverticalscrollindicator =falseSelf.myscrollview?. Scrollstotop =falseSelf.myscrollview?.Delegate=Self self.myscrollview?. ispagingenabled =trueSelf.view.addSubview (Self.myscrollview!)} func addimages () {Let size= Self.myscrollview?. Bounds.size for(Seq,course)inchcourses.enumerated () {let page= UIView (Frame:cgrect (x:0Y: -, width:kscreenwidth,height:kscreenheight/2)) Let ImageView=uiimageview (Image:uiimage (named:course["pic"]!)) Imageview.frame=page.frame Page.addsubview (imageView) Page.backgroundcolor=Uicolor.gray let Lab1= UILabel (Frame:cgrect (x: the, y:kscreenheight/2- -, width:kscreenwidth- -, Height: +)) Lab1.textalignment=. Center Lab1.backgroundcolor=uicolor.red Lab1.text= course["name"] Page.addsubview (LAB1) page.frame= CGRect (x:cgfloat (SEQ) * (size?. width)!,y:0, Width: (size?. width)!,height: (size?. Height)!) Self.myscrollview?. Addsubview (page)}} Func Addpagecontrol () {Self.pagecontrol= Uipagecontrol (Frame:cgrect (x:kscreenwidth/2- -/2, y:kscreenheight/2- +, Width: -, Height: *)) Self.pagecontrol?. BackgroundColor =Uicolor.blue Self.pagecontrol?. Numberofpages =courses.count Self.view.addSubview (Self.pagecontrol!)} func Setuptimer () {Self.mytimer= Timer.init (timeinterval:2.0, Target:self, selector: #selector (timeraction), Userinfo:nil, repeats:true) RunLoop.main.add (Self.mytimer!, ForMode:RunLoopMode.defaultRunLoopMode)//DispatchQueue.main.asyncAfter (Deadline:DispatchTime.now () + 2) {//Self.mytimer?. Fire ()// }} func Closetimer () {Self.mytimer?. Invalidate () Self.mytimer=Nil} func timeraction () {Self.pagecontrol?. CurrentPage + = mycurrentpage!ifSelf.pagecontrol?. CurrentPage = =0|| Self.pagecontrol?. CurrentPage = =2{mycurrentpage=-mycurrentpage!} Self.myscrollview?. Contentoffset = Cgpoint (X:cgfloat (kscreenwidth) *cgfloat (Pagecontrol?. currentpage)!), Y:0) }
3, some proxy method implementation, manually drag the time, destroy the timer, save memory
Internal Func scrollviewdidenddecelerating (_ Scrollview:uiscrollview) { = Int (scrollview.contentoffset.x/ ScrollView.frame.size.width) Self.pagecontrol? currentpage = page } func scrollviewwillbegindragging (_ Scrollview:uiscrollview) { Self.closetimer ( ) } func scrollviewdidenddragging (_ Scrollview:uiscrollview, Willdecelerate decelerate:bool) { Self.setuptimer () } func scrollviewwillbegindecelerating (_ Scrollview:uiscrollview) { Self.pagecontrol?. CurrentPage = Int (scrollview.contentoffset.x/kscreenwidth) }
The effect is as follows:
Swift Development-Auto-Carousel diagram (Uiscrollview+uipagecontrol+timer)