Online reference OBJC write with the ScrollView picture carousel effect, according to draw a, first on:
Attached code:
1@IBOutlet weak var pc:uipagecontrol!2@IBOutlet weak var sv:uiscrollview!3 4var timer:nstimer!5 6 Overridefunc viewdidload () {7 super.viewdidload ()8 9 forIinch 1...6{//Loading the imagesTenLet image = UIImage (named:"pic\ (i). jpg")! OneLet x = CGFloat (i-1) * Self.view.frame.width//This step gets the width of the scrollview when I use the IPHONE6 physical machine test is 320, the right side will appear the second picture part, and finally the width of the root view Avar ImageView = Uiimageview (Frame:cgrectmake (x,0, Self.view.frame.width, sv.bounds.height)) -Imageview.image =Image -sv.pagingenabled =true theSv.showshorizontalscrollindicator =false -sv.scrollenabled =true - Sv.addsubview (ImageView) -Sv.Delegate= Self + } - +Sv.contentsize = Cgsizemake ((self.view.frame.width *6), Sv.frame.height) APc.numberofpages =6 atPc.currentpageindicatortintcolor =Uicolor.redcolor () -Pc.pageindicatortintcolor =Uicolor.whitecolor () - AddTimer () - - } - in func Scrollviewdidscroll (scrollview:uiscrollview) { -Let width =Self.view.frame.width toLet OffsetX =Scrollview.contentoffset.x +Let index = (OffsetX + width/2) /width -Pc.currentpage =Int (Index) the } * $ func scrollviewwillbegindragging (scrollview:uiscrollview) {Panax Notoginseng Removetimer () - } the + func scrollviewdidenddragging (Scrollview:uiscrollview, willdecelerate decelerate:bool) { A AddTimer () the } + - func AddTimer () { $Timer = Nstimer.scheduledtimerwithtimeinterval (5, Target:self, selector:"NextImage", Userinfo:nil, repeats:true) $ } - - func Removetimer () { the timer.invalidate () - }Wuyi the - func nextimage () { Wuvar pageIndex =Pc.currentpage - ifPageIndex = =5 { AboutPageIndex =0 $}Else { -pageindex++ - } - Avar OffsetX = cgfloat (pageIndex) *Self.view.frame.width +Sv.setcontentoffset (Cgpointmake (OffsetX,0), Animated:true) the}
Swift uses ScrollView plus pictures to make banner