background

Because the company wrote a page number control for a new App, but found that the code intrusion of the original App page number control was relatively large, which would affect the business code and cause large modification. So I decided to rewrite it.

The code structure

First of all, the control needs several capabilities, such as scrolling up and down to display a number, clicking after the top function, and switching the display view. So you need a class that listens for UITableView swipes up and down, and then uses runtime to swap several delegate methods for UIScrollView, ScrollViewDidEndDecelerating:, scrollViewDidEndDragging: willDecelerate: and then go back to bring up. I’m going to use UIView for this class just for ease of use. Some child views that need to rely on these methods need to implement the corresponding methods and add them to this UIView.

The class diagram structure

Customize child views

Of course I want to be able to make others more easily add a custom view, so I wrote a special CSFooterCircleSubviewProtocol, to let the child view have the ability to monitor the UITableView slide up and down and stop the slide.

@protocol CSFooterCircleSubviewProtocol <NSObject> typedef void(^CSCircleFunctionSubviewsAnimationCompletion)(void); @required /** The subview is not lighted up at @param isHighlighted */ - (void)setHighlighted:(BOOL)isHighlighted; /** The parent tableView control slides up TODO: I'm gonna use the willtableViewScrollUp callback, Because sometimes need to change the state rather than a continuous state callback @ param view CSFooterCircleFunctionView @ param index sliding time shows the cell index @ param total When slide shows the number of total cell * / - (void) tableViewScrollUp: (UIView < CSFooterCircleViewProtocol > *) the view index (NSInteger) index; /** Parent tableView control slide down TODO: You need to use the willScrollDown callback, Because sometimes need to change the state rather than a continuous state callback @ param view CSFooterCircleFunctionView @ param index sliding time shows the cell index @ param total When slide shows the number of total cell * / - (void) tableViewScrollDown: (UIView < CSFooterCircleViewProtocol > *) the view index (NSInteger) index; /** The parent tableView control stops sliding TODO: I'm gonna use the willtableViewScrollStop callback, Because sometimes is need to change the state rather than a continuous state callback @ param view CSFooterCircleFunctionView * / - (void)tableViewScrollStop:(UIView<CSFooterCircleViewProtocol> *)view; @endCopy the code

Using CSFooterCircleFunctionView

Initialize the SubViews

    CSFotterCircleScrollToTopSubview<CSFooterCircleSubviewProtocol> *scrollToTopSubViews = [[CSFotterCircleScrollToTopSubview alloc] initWithFrame:CGRectMake(0, 0, 45, 45)];
    [scrollToTopSubViews setHighlighted:NO];
    
    CSFotterCircleShowSrcollIndexTypeSubview<CSFooterCircleSubviewProtocol> *srcollIndexTypeSubview = [[CSFotterCircleShowSrcollIndexTypeSubview alloc] initWithFrame:CGRectMake(0, 0, 45, 45)];
    [srcollIndexTypeSubview setTotal:50];
    [srcollIndexTypeSubview setHighlighted:NO];
    
    NSArray *retArr = @[scrollToTopSubViews,srcollIndexTypeSubview];
    return retArr;
Copy the code

Initialize CSFooterCircleFunctionView

- (CSFooterCircleFunctionView *)footerCircleView
{
    if(! _footerCircleView) { _footerCircleView = [[CSFooterCircleFunctionView alloc] initWithSubViews:(NSArray <CSFooterCircleSubviewProtocol>*)[self setupSubViewsForFooterCircleFunctionView]]; _footerCircleView.weakTableView = self.tableView; __weak __typeof(self)weakSelf = self; _footerCircleView.actionCompletion = ^(NSString *viewName, UIView<CSFooterCircleSubviewProtocol> *subview) {if ([viewName isEqualToString:@"CSFotterCircleScrollToTopSubview"])
            {
                __strong __typeof(weakSelf)strongSelf = weakSelf;
                [strongSelf.tableView setContentOffset:CGPointMake(0, 0) animated:YES]; }}; }return _footerCircleView;
}
Copy the code

The future changes

  1. We need to add some critical point states.
  2. Optimize scrolling performance.
  3. You need to use less code.

Making the address

Github.com/KoonChaoSo/…

If you have any suggestions or improvements you can comment below and say oh thanks for reading.