iOS development-Define reuse mechanism for Scrollerview join child view

Source: Internet
Author: User

In fact, I thought of this question very early, just did not write the program implementation, yesterday someone mentioned, I was a coincidence

We do not know how many times the cell was initially printed in Guo Tableview:cellforrow, that is, the number of cells in the reuse pool. This is fixed, for example, the screen can display up to 4 cells so the number of reuse pool may be 5 6 or about 7, I think of the assumption ScrollView to implement, the sub-view should also have a number, then how to implement the reuse pool?

I thought of the array, the variable group, and I set the sliding area of the ScrollView to 10000 in Loadview. And then give ScrollView every 20 distance to add a view of 10, in the For loop to add the 10 view to the array arr, then how to do it, I think so, when the need to join the 11th time, the 1th from the array to be raised. Change the frame to the 11th one. And then change it to the last of the array, is not there, the same assumption pull up when how to display the previous? The last one of the arrays is raised. Change the frame and change it to the first position in the array. So much for saying. The following sticker code bar.

viewcontroller.m//Scollerview Reuse////Created by Monkeyheng on 8/5/15.//Copyright (c) Monkeyheng. All rights reserved.//#import "ViewController.h" @interface Viewcontroller () <UIScrollViewDelegate> @property ( Nonatomic, Strong) Uiscrollview *scrollview;//managed Reuse Array @property (nonatomic, strong) Nsmutablearray *arr;// The coordinate Y value that needs to be added to the head sub-view @property (nonatomic, assign) CGFloat upy;//need to be added to the trailing sub-view coordinates y@property (nonatomic, assign) CGFloat downy;/ /Drag Time offset @property (nonatomic, assign) cgfloat offset; @end @implementation viewcontroller-(void) Loadview {[Super Load    View];    Initialize a scrollView width to the screen width sliding area for 10000 self.scrollView.contentSize = Cgsizemake (Self.view.frame.size.width, 10000);    Self.scrollView.backgroundColor = [Uicolor Redcolor];    Self.scrollView.delegate = self; [Self.view AddSubview:self.scrollView];}    -(void) viewdidload {[Super viewdidload];    Additional setup after loading the view, typically from a nib. Add initial sub-view to ScrollView a total of 10 can be based on the actual situationRegulation/Algorithmic regulation Nsinteger i = 0; for (i = 0; i < i++) {UIView *view = [[UIView alloc] Initwithframe:cgrectmake (0, I * + (i-1) * 20, 375        , 100)];        View.backgroundcolor = [Uicolor Bluecolor];        Join the sub-view to ScrollView [Self.scrollview Addsubview:view];    Add view to managed reusable array [Self.arr Addobject:view];    }//The Y-point of the next view is i++;    Self.downy = i * + (i-1) * 20;    Self.upy =-120; Self.offset = 0;}    -(void) didreceivememorywarning {[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} Drag start//ScrollView start drag-(void) scrollviewwillbegindragging: (Uiscrollview *) scrollview{//record the offset at the start of the drag SELF.OFFSE t = Scrollview.contentoffset.y;} -(void) Scrollviewdidscroll: (Uiscrollview *) ScrollView {//Infer whether the offset exceeds the on screen if (Scrollview.contentoffset.y < 0 | | Scrollview.contentoffset.y > (10000-667)) {//Out of screen range do not do whatever operation} else if (scrollview.contentoffset.y-self  . Offset) > 0) {      The offset of the start slide is assumed to be greater than the offset of the drag finger drag the screen up/down slide if ((Self.downy-(SELF.SCROLLVIEW.CONTENTOFFSET.Y + 667)) <            150) {//in the tail y-coordinate distance from the bottom of the screen///Remove the first element of the array UIView *view = [Self.arr objectatindex:0];            Change the y-coordinate of the head to the y-coordinate of the view as the view is added to the tail self.upy = VIEW.FRAME.ORIGIN.Y;            Remove the first element of the divisor group [Self.arr removeobjectatindex:0];            Change View Frame view.frame = CGRectMake (0, self.downy-100-20, 375, 100);            Add the view to the array again [Self.arr Addobject:view];                    Change the Y value of the next tail coordinate self.downy = self.downy + 100 + 20;  }} else if ((Scrollview.contentoffset.y-self.offset) < 0) {//start sliding offset assumed to be greater than the offset of the drag the finger drag the screen down// Sliding if ((Self.scrollview.contentoffset.y-self.upy) < 270) {//at the head y coordinate from the top 270 of the screen-the end of the header view that will be added            Distance from screen head 150//Remove the last element of the array UIView *view = [Self.arr lastobject]; WillThe tail y-coordinate changes to the y-coordinate of the view because the view is added to the head self.downy = VIEW.FRAME.ORIGIN.Y;            Remove the last element of the divisor group [Self.arr Removeobject:view];            Change View Frame view.frame = CGRectMake (0, Self.upy, 375, 100);            Add view again to the head of the array [Self.arr Insertobject:view atindex:0];        Change the Y value of the next head coordinate self.upy = self.upy-100-20; }}} #pragma mark lazy load-(Uiscrollview *) ScrollView {if (_scrollview = = nil) {_scrollview = [[Uiscrollview all]    OC] Initwithframe:cgrectmake (0, 0, Self.view.frame.size.width, self.view.frame.size.height)]; } return _scrollview;}    -(Nsmutablearray *) arr {if (_arr = = nil) {_arr = [[Nsmutablearray alloc] init]; } return _arr;} @end
In two pictures.

When you start the simulator,

When you slide into the middle,

Slide to the last one.

iOS development-Define reuse mechanism for Scrollerview join child view

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.