ios - Paging using UIScrollView -
i'm trying display multiple images using uiscrollview
, page control, think screenshots of apps on app store. yet reason, uiscrollview
not scrolling. checked, , uiscrollview's
contentsize's width larger uiscrollview's
width. might worth noting put page control in uiscrollview
, displays on top of pictures. code follows:
import uikit class itemdetailviewcontroller: uiviewcontroller, uiscrollviewdelegate { @iboutlet weak var scrollview: uiscrollview! @iboutlet weak var pagecontrol: uipagecontrol! var itemselected: item! var pageviews: [uiimageview?] = [] var pagecount: int! override func viewdidload() { super.viewdidload() // additional setup after loading view. pagecount = itemselected.images.count pagecontrol.currentpage = 0 pagecontrol.numberofpages = pagecount _ in 0..<pagecount { pageviews.append(nil) } scrollview.frame.size = cgsizemake(view.frame.width, view.frame.height/2.0) let pagesize = scrollview.frame.size scrollview.contentsize = cgsizemake(pagesize.width * cgfloat(pagecount), pagesize.height) loadvisiblepages() scrollview.delegate = self } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } override func viewwillappear(animated: bool) { super.viewwillappear(animated) tabbarcontroller?.tabbar.hidden = true } override func viewwilldisappear(animated: bool) { super.viewdiddisappear(animated) tabbarcontroller?.tabbar.hidden = false } // mark: - helper functions func loadpage(page: int){ if page < 0 || page >= pagecount { // page outside of range, nothing return } if let pageview = pageviews[page] { // page loaded, nothing return } else { var frame = scrollview.bounds frame.origin.x = frame.size.width * cgfloat(page) frame.origin.y = 0.0 let newpageview = uiimageview(image: itemselected.images[page]) newpageview.contentmode = .scaletofill newpageview.frame = frame scrollview.addsubview(newpageview) pageviews[page] = newpageview } } func purgepage(page: int){ if page < 0 || page >= pagecount { // page outside of range, nothing return } if let pageview = pageviews[page]{ pageview.removefromsuperview() pageviews[page] = nil } } func loadvisiblepages(){ let pagewidth = scrollview.frame.size.width let page = int(floor(scrollview.contentoffset.x * 2.0 + pagewidth)/(2.0 * pagewidth)) pagecontrol.currentpage = page let firstpage = page - 1 let lastpage = page + 1 var index = 0; index < firstpage; index++ { purgepage(index) } index in firstpage ... lastpage { loadpage(index) } var index = lastpage + 1; index < itemselected.images.count; index++ { purgepage(index) } } // mark: - scroll view delegate methods func scrollviewdidscroll(scrollview: uiscrollview) { loadvisiblepages() } }
what causing issue?
better use uipageviewcontroller
u want show images. u can see tutorial how there there
Comments
Post a Comment