UIScrollView contentOffset error?

I have set the isPagingEnabled

element's scrollView property to true

for example:

let scrollView = UIScrollView(frame: view.bounds)
scrollView.contentSize = CGSize(width: view.bounds.width * 2, height: 0)
scrollView.delegate = self
scrollView.isPagingEnabled = true
view.addSubview(scrollView)

      

and the delegate method:

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    print(scrollView.contentOffset.x)
}

      

When scrolling the view to the border, the console prints as shown:

enter image description here

and

enter image description here

The values ​​in the red box shouldn't be printed, right?

But, if the setting isPagingEnabled

is false

, console printing is fine. Is this a bug UIScrollView

?

+3


source to share


4 answers


Your content offset is not displayed correctly because you are using the content size in the viewdidload using the content size in this method than printing your content with the set



 override func viewDidLayoutSubviews()
 {
  scrollView.contentSize = CGSize(width: view.bounds.width * 2, height: 0)
 }

      

+1


source


As far as I know, it works correctly when you set the contentSize on the scrollview , which will respond to their delegate the first time around itself.

func scrollViewDidScroll (_ scrollView: UIScrollView)

Whenever the user tries to scroll or drag, the above delegate will be called recursively. Here your contentSize width is twice the width of your view borders (so there are two pages, contentOffset.x will start at 0 and the current scroll position, if it's on the second page, this will give you a view width * n ) and you set isPagingEnabled to true strong>.



Check out https://developer.apple.com/reference/uikit/uiscrollview/1619404-contentoffset

The default is CGPoint Zero .

In fact contentOffset will tell you what the current scroll position is.

+1


source


Whenever it UIScrollView

scrolls, this method scrollViewDidScroll(_ scrollView: UIScrollView)

calls. Any direction if you scroll down will get these types of output.

However, if you want to use subcategorization, then instead of this method you can use:

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {

        let pageNumber = round(scrollView.contentOffset.x / scrollView.frame.size.width)
        print("pageNumber = \(pageNumber)")

    }

      

With this method, you will get the exact page no UIScrollView

.

0


source


Please use a frame instead of borders.

The bounds of a UIView is a rectangle expressed as a location (x, y) and size (width, height) relative to its own coordinate system (0,0). The frame of a UIView is a rectangle, expressed as a location (x, y) and size (width, height) relative to the overlap it is contained in.

 let scrollView = UIScrollView(frame: view.frame.size)
 scrollView.contentSize = CGSize(width: view.frame.size.width * 2, 
 height: 0)
 scrollView.delegate = self
 scrollView.isPagingEnabled = true
 view.addSubview(scrollView)

      

0


source







All Articles