ios - Swift - UIScrollView Jumps to first page instead scrolling to it -


is there way add circle scrolling uiscrollview? example if scroll reaches last page first page should next , if scrolling reaches first page last page should next.

i have been trying using scrollviewdidscroll function however, when scroll reaches last page view jumps first view instead of scrolling or add next it.

my code is:

func scrollviewdidscroll(scrollview: uiscrollview) {          if (scrollview.contentoffset.x > scrollview.frame.size.width){             scrollview.setcontentoffset(cgpointzero, animated: true)             //scrollview.scrollrecttovisible(cgrectmake(0, 0, 1, 1), animated: false)           }          if (scrollview.contentoffset.x < 0){             scrollview.setcontentoffset(cgpointmake(scrollview.frame.size.width,0.0), animated: true)         }     }  

any idea how this?

you're going have small amount of around work it's nothing arduous.

seemingly displaying first page of content right of last isn't scroll view can natively do. fix that, need extend contentsize screen right , duplicate content.

that'll move having content area covers range [0, end of last page) [0, end of last page + 1). still want scrolling constrained [0, last page), offsets in range [-1, 0] being mapped [last page, last page + 1). so:

func scrollviewdidscroll(scrollview: uiscrollview) {      let realcontentwidth = scrollview.contentsize.width - scrollview.bounds.size.width      scrollview.contentoffset.x = ((scrollview.contentoffset.x % realcontentwidth)                                                      + realcontentwidth) % realcontentwidth; }  

the logic on modulo is:

  • if start content offset being positive number inner % map correct range , outer cause + no-op; but
  • if start content offset being negative number inner % leave still negative in (-realcontentwidth, 0], + move corresponding positive range outer % being no-op.

(with quick observation objective-c'ers: % defined on floating point numbers in swift; in objective-c you'd use fmod in place % defined integral arithmetic there)


Comments

Popular posts from this blog

How to provide Authorization & Authentication using Asp.net, C#? -

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

How to use Authorization & Authentication in Asp.net, C#? -