iOS slides to the first page algorithm based on the offset of the ScrollView slide (different requirements are calculated)

Source: Internet
Author: User

The first type:

CGFloat pageWidth = self.scrollView.frame.size.width;

int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;

Let me give you an example.
The width of a page is 320
Using the results of Method 1
Scenario 1:
Contentoffset is less than 320+160, that is, the left half of the second page, (SELF.SCROLLVIEW.CONTENTOFFSET.X-PAGEWIDTH/2)/ The result of the pagewidth is that the 0.xxxx,floor result is 0 and the result is 1, which returns to the first page
Scenario 2:
Contentoffset greater than 320+160, that is, the right half of the second page, (SELF.SCROLLVIEW.CONTENTOFFSET.X-PAGEWIDTH/2)/ The result of the pagewidth is that the result of the 1.xxxx,floor is 1,page and the result is 2, which will go to the second page


The second Kind
self.scrollView.contentOffset.x/pageWidth
Using the results of Method 2
Self.scrollview.contentoffset.x/pagewidth calculation results are 1.xxx
While page is of type int, the system is automatically forced to transform according to rounding

iOS slides to the first page algorithm based on the offset of the ScrollView slide (different requirements are calculated)

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.