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"