iOS monitor Scrollview/tableview the correct posture for scrolling

Source: Internet
Author: User

Main introduction
    1. Monitoring TableView vertical scrolling comfortable posture
    2. Monitoring the correct posture of scrollview/collectionview transverse scrolling
1. Monitor TableView vertical scrolling comfort Posture

Usually we listen to Scrollview/tableview's contentoffset with KVO or in Scrollviewdidscroll proxy method, For example, listen for TableView contentoffset to set the transparency of the navigation bar or to stretch the top picture.


Image
Image


a common posture is to listen to Scrollview.contentoffset.y in the Scrollviewdidscroll proxy method, you will find the navigation bar Scrollview.contentoffset.y the initial value may be equal to 64 if you manually set the tableview.co Ntentinset This value will change, this time need to calculate the initial offset, and then calculate the difference of the offset, if a few days to change the demand ... Recalculate

Is there any more comfortable posture?

Yes

First define 2 member properties, one is the critical value of the monitoring range, and the other is used to record the scrollView.contentInset.top (emphasis)

 //the critical point of the monitoring range, >0 represents how much distance to slide up, <0 is sliding down how much distance  @property (nonatomic, assign) cgfloat threshold; //record scrollView.contentInset.top  @property (nonatomic, assign" CGFloat MarginTop; //here Set the value-80, that is, sliding down 80,-80 to 0 is the focus of our monitoring range self .threshold =-80;        

Then add the following code to the KVO callback or Scrollviewdidscroll proxy method.
Need to understand is contentinset, four values represent the value of the TableView contentview up and down distance boundary, the interface has a navigation bar, the system will automatically tableview Contentview to the top of the distance is set to 64, That is contentinset.top=64, if the navigation bar is transparent, you can see a blank area. Without a navigation bar or when we call Self.automaticallyadjustsscrollviewinsets = no, the contentinset.top of TableView becomes 0. By the following algorithm, we can not deliberately calculate the initial offset, as long as the threshold is set to the line, newoffsety is the actual deviation we want to monitor. PS: Manual setup of Tableview.conteninset needs to be done in viewdidappear.

        Real-time monitoring of scrollView.contentInset.top, system optimization and manual setting of Contentinset will affect Contentinset.top.if (Self. margintop! =Self. ScrollView. contentinset. top) {Self. margintop =Self. ScrollView.contentinset.top;} //cgfloat OffsetY = [change[@ "new"] cgpointvalue].y; cgfloat OffsetY = Scrollview.contentoffset.Y; //newoffsety is the offset offset.y we want to monitor, with an initial value of 0 //when sliding down <0, when sliding upward >0; cgfloat newoffsety = OffsetY + self.MARGINTOP; if (newoffsety >= self0) {cgfloat progress = Newoffsety/self.threshold;}      

is a mule is a horse, pull out to sneak, look again.


Iamge

First page (home), the row becomes transparent

- (void) Scrollviewdidscroll: (Uiscrollview *) scrollview{if (Self. margintop! = ScrollView. contentinset. top) {Self. margintop = ScrollView. contentinset. Top; }CGFloat OffsetY = ScrollView. contentoffset. Y;CGFloat newoffsety = OffsetY +Self. margintop;Critical value 150, drag up time-varying transparencyif (newoffsety >=0 && newoffsety <= 150) {[ self.navigationcontroller1-newoffsety/ 150]; }else if (newoffsety > 150) {[ Span class= "Hljs-keyword" >self.navigationcontroller0]; }else{[self.navigationbar jp_setnavigationbarbackgroundalpha:1"; }} 

The second interface imitates the effect of daily life X life in his article imitates the daily-Main page addendum (Part 2) has been implemented in another way.


From Life x Life


Just say my thoughts.

    1. The Y coordinate of the tableview is 20, then the group head height is 44, which is exactly the height of the navigation bar. Because the group head hovers at the top of the tableview, the Y coordinate is 20.
    2. Add a view with a height of 20 above the Navigationbar and place it under the status bar in the same color as the head of the group. Navigationbar The view appears to be connected to the group header after transparent.
    3. In the Viewdidappear method, the frame of the first group header is computed, and the Y coordinate is taken. (Preferably in the Viewdidappear method, or the addition of tableheaderview may produce deviations)
    4. Then the Scrollviewdidscroll method realizes the transparency of the navigation bar and toggles the Titleview
- (void) Viewdidappear: (BOOL) animated{[Super viewdidappear:animated]; Headerframe = [Self. TableView Rectforheaderinsection:1];} - (void) Scrollviewdidscroll: (Uiscrollview *) scrollview{if (Self. margintop! = ScrollView. contentinset. top) {Self. margintop = ScrollView. contentinset. Top; }CGFloat OffsetY = ScrollView. contentoffset. Y;CGFloat newoffsety = OffsetY +Self. margintop;if (newoffsety >=0 && newoffsety <=150) {[Self. Navigationcontroller. Navigationbar jp_setstatusbarbackgroundviewalpha:newoffsety/150]; }Elseif (Newoffsety >150) {[Self. Navigationcontroller. Navigationbar Jp_setstatusbarbackgroundviewalpha:1]; }if (newoffsety >= headerframe. Origin. Y) {self.refrshview.hidden = Span class= "Hljs-literal" >yes; [self.navigationcontrolleruicolor Purplecolor] ColorWithAlphaComponent:0]; }else if (Newoffsety < Headerframe .origin.y) {[self.refrshview resetnavigationitemtitle:@ "Home"]; self.refrshview.hidden = Span class= "Hljs-literal" >NO; }} 
2. Monitoring the correct posture of scrollview/collectionview transverse scrolling

First of all, the following ScrollView page to move the above navigation bar, how will you achieve?


Iamge

The first thought is definitely the end of the agent method of deceleration: scrollviewdidenddecelerating, but the sliding too fast is not called Scrollviewdidenddecelerating proxy method, if done with 3 interface + ScrollView realize a circular scrolling display of pictures, then basically you will encounter such a problem. How to accurately monitor the page? my solution to the idea is as follows

    1. The code that needs to be processed after slowing down is integrated into a method, and it needs to pass in ScrollView's contentoffset to determine the current page/index. Hereafter referred to as "method a"
    2. Focus on three agent methods throughout the process, drag and start to slow down-------start dragging
    3. Normal sliding speed, call scrollviewwillbegindecelerating before calling Scrollviewdidenddecelerating, then call method a
    4. When sliding quickly, call Scrollviewwillbegindecelerating first, then call scrollviewwillbegindragging, not call scrollviewwillbegindragging. We can add a flag to determine whether to slow down. No deceleration, then call method A.
Start self.didenddecelerating = No at the beginning of deceleration, and the end deceleration will be set to Yes if the slide is fast or No. - (void) Scrollviewwillbegindecelerating: (Uiscrollview *) scrollview{Self. didenddecelerating =NO;} - (void) Scrollviewdidenddecelerating: (Uiscrollview *) scrollview{Self. didenddecelerating = YES; //Call method A, pass Scrollview.contentoffset} //Drag again to determine if there is no way to end the deceleration because the slide is too fast. //If not, round up the manually determined location. This will solve the problem of sliding too fast-(void) scrollviewwillbegindragging: (uiscrollview *) scrollview{ if (!  Self. didenddecelerating) { //calculates the page/index of the current period cgfloat index = ScrollView. Contentoffset. x/ self. screenwidth; //Rounding out the contentoffset of the scrollview that should have slowed down. That is: ROUNDF (index) *self.screenwidth]}}             



Wen/stream Maple Wolf (Jane book author)
Original link: HTTP://WWW.JIANSHU.COM/P/2172CCA95CDC
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".

iOS monitor Scrollview/tableview the correct posture for scrolling

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.