Swift Page Paging view

Source: Internet
Author: User

var data:nsarray!

var scrollview:uiscrollview!

var pagectrl:uipagecontrol!

Override Func Viewdidload () {

Super.viewdidload ()

Instantiate TableView

Let Tableview:uitableview = UITableView (Frame:self.view.bounds, Style:UITableViewStyle.Plain)

Tableview.delegate = Self

Tableview.datasource = Self

Tableview.registerclass (uitableviewcell.self, Forcellreuseidentifier: "Cell")

Self.view.addSubview (TableView)

System font

data = Uifont.familynames ()

Instantiate ScrollView

Let Widthm = TableView.frame.size.width

Let View:uiview = UIView (Frame:cgrect (x:0, y:0, width:0, height:200))

ScrollView = Uiscrollview (Frame:cgrect (x:0, y:0, Width:widthm, height:200))

Scrollview.contentsize = Cgsize (Width:widthm * 5, height:200)

Scrollview.showshorizontalscrollindicator = False

Scrollview.showsverticalscrollindicator = False

Scrollview.pagingenabled = True

Scrollview.delegate = Self

For index in 1...5 {

Let imgname = "image\ (index). jpg"

Let Imgview:uiimageview = Uiimageview (Image:uiimage (named:imgname))

Imgview.frame = CGRect (X:widthm * cgfloat (index-1), y:0, Width:widthm, height:200)

Scrollview.addsubview (Imgview)

}

View.autoresizessubviews = False

View.addsubview (ScrollView)

Instantiating a paging controller

PageCtrl = Uipagecontrol (Frame:cgrect (x:0, y:200-20, Width:widthm, height:20))

Pagectrl.addtarget (Self, Action:selector ("Pagectrlaction:"), forControlEvents:UIControlEvents.ValueChanged)

Pagectrl.numberofpages = 5

Pagectrl.currentpage = 0

View.addsubview (PageCtrl)

Tableview.tableheaderview = view

}

Mark:uitableviewdatasource

Func TableView (Tableview:uitableview, Numberofrowsinsection section:int), Int {

Return Data.count

}

Func TableView (Tableview:uitableview, Cellforrowatindexpath indexpath:nsindexpath), UITableViewCell {

Let Tableviewcell = Tableview.dequeuereusablecellwithidentifier ("cell", Forindexpath:indexpath) as UITableViewCell

Tableviewcell.textlabel?. Text = Self.data[indexpath.row] as? String

Return Tableviewcell

}

Mark:uiscrollviewdelegate

Func scrollviewdidenddecelerating (Scrollview:uiscrollview) {

if ScrollView = = Self.scrollview {

Pagectrl.currentpage = Int (scrollview.contentoffset.x/scrollview.frame.size.width)

}

}

Mark:pagectrl Action

Func pagectrlaction (Pagectrl:uipagecontrol) {

Scrollview.contentoffset.x = ScrollView.frame.width * CGFloat (Pagectrl.currentpage)

Scrollview.setcontentoffset (Cgpoint (X:scrollview.frame.width * cgfloat (pagectrl.currentpage), y:0), animated:true)

}

Swift Page Paging view

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.