Swift uses ScrollView plus pictures to make banner

Source: Internet
Author: User

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

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.