IOS "Uikit-uipagecontrol rounding tips using delegate to position dots"

Source: Internet
Author: User
Tags uikit

In Uiscrollview, Uipagecontrol is added as the page number ID, allowing the user to know the current pages clearly. One of the points that we need to optimize is to make Pagecontrol's small dots precisely follow scrollview. Let's take a look first:

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center "height=" 381 "width=" 249 ">

We found. When the picture is less than half-dragged, the Pagecontrol dot is positioned to the previous picture, and the image is dragged more than half. Navigate to the next picture. The calculation of rounding is required here.

We are able to use the agreement delegate to do this thing.

First, we're going to join the agent for this scrollview we're currently setting up:

Scrollview.delegate = self;

It is necessary to have the current class adhere to the Uiscrollviewdelegate protocol.



In the uiscrollviewdelegate. There is such a way. It can be triggered just by scrollview scrolling.

We can achieve this in this way.

-(void) Scrollviewdidscroll: (Uiscrollview *) scrollview{    //with horizontal moving distance divided by width equal to number of pages, but no exact    double page = Scrollview.contentoffset.x/scrollview.width;        Use rounding to determine which page is closer. and set (following is a rounding trick, + 0.5 and then rounding down can be)    self.pageControl.currentPage = (int) (page + 0.5);}

Analysis Example: (page = 0 is the first picture, page = 1 is the second picture, and so on)

page = 0.95, closer to the second picture. Page + 0.5 = 1.45. (int) 1.45 = 1

page = 1.21, closer to the second picture. Page + 0.5 = 1.71. (int) 1.71 = 1

page = 1.67. Closer to the third picture. Page + 0.5 = 2.17, (int) 2.17 = 2

page = 2.11, closer to the third picture, page + 0.5 = 2.61, (int) 2.61 = 2

It's also important to note that. The force conversion to int type is rounded down, that is, all digits after the decimal point are omitted.

IOS "Uikit-uipagecontrol rounding tips using delegate to position dots"

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.