Tab implementation principle, Tab implementation principle

Source: Internet
Author: User

Tab implementation principle, Tab implementation principle

1: implement the class rolling effect of the header

The main code is as follows:

        UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, screen_width, 40)];        scrollView.pagingEnabled = NO;        scrollView.alwaysBounceHorizontal = YES;        scrollView.showsHorizontalScrollIndicator = NO;        scrollView.showsVerticalScrollIndicator = NO;        scrollView.backgroundColor = RGB(246, 246, 246);        [self.view addSubview:scrollView];                float btnWidth = 60;                for (int i = 0; i < self.cateNameArray.count; i++) {            UIButton *nameBtn = [UIButton buttonWithType:UIButtonTypeCustom];            nameBtn.frame = CGRectMake(btnWidth*i, 0, btnWidth, 40);            nameBtn.tag = 10+i;            nameBtn.font = [UIFont systemFontOfSize:13];            [nameBtn setTitle:self.cateNameArray[i] forState:UIControlStateNormal];            [nameBtn setTitleColor:navigationBarColor forState:UIControlStateSelected];            [nameBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];            [nameBtn addTarget:self action:@selector(OnTapNameBtn:) forControlEvents:UIControlEventTouchUpInside];            [scrollView addSubview:nameBtn];            if (i == 0) {//                nameBtn.selected = YES;                _lineView = [[UIView alloc] initWithFrame:CGRectMake(nameBtn.center.x-20, 38, 40, 2)];                _lineView.backgroundColor = navigationBarColor;                [scrollView addSubview:_lineView];            }        }        scrollView.contentSize = CGSizeMake(self.cateNameArray.count*btnWidth, 0);
-(Void) OnTapNameBtn :( UIButton *) sender {NSInteger index = sender. tag-10; if (index = _ currentIndex) {return;} _ currentIndex = index; _ cateid = _ cateIDArray [index]; _ page = 1; [UIView animateWithDuration: 0.5 animations: ^ {_ lineView. center = CGPointMake (sender. center. x, 39) ;}]; // refresh data [self.tableView.gif Header beginRefreshing];}

_ LineView is a view, because the list below is refreshed during scrolling;

 

2: Use a third-party plug-in XTSegmentControl and use iCarousel to scroll the tab.

The effect is as follows:

The main code is as follows:

#import <UIKit/UIKit.h>#import "oldChildVewController.h"#import "ChildViewController.h"#import "newChildVewController.h"#import "XTSegmentControl.h"#import "iCarousel.h"#import "Masonry.h"@interface ViewController : UIViewController<iCarouselDataSource, iCarouselDelegate>@end
# Import "ViewController. h "# define kScreen_Height [UIScreen mainScreen]. bounds. size. height # define kScreen_Width [UIScreen mainScreen]. bounds. size. width # define controls 44.0 @ interface ViewController () @ property (strong, nonatomic) NSMutableDictionary * dictionary; @ property (strong, nonatomic) XTSegmentControl * mySegmentControl; @ property (strong, nonatomic) NSArray * titlesArray; @ property (strong, nonatomic) iCarousel * myCarousel; @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; self. view. backgroundColor = [UIColor whiteColor]; CGRect frame = self. view. bounds; self. myCarousel = ({iCarousel * icarousel = [[iCarousel alloc] initWithFrame: frame]; icarousel. dataSource = self; icarousel. delegate = self; icarousel. decelerationRate = 1.0; icarousel. scrollSpeed = 1.0; icarousel. type = iCarouselTypeLinear; icarousel. pagingEnabled = YES; icarousel. clipsToBounds = YES; icarousel. bounceDistance = 0.2; [self. view addSubview: icarousel]; [icarousel mas_makeConstraints: ^ (MASConstraintMaker * make) {make. edges. similar to (self. view ). insets (UIEdgeInsetsMake (kMySegmentControl_Height, 0, 0) ;}]; icarousel ;}); // Add the slider _ weak typeof (_ myCarousel) weakCarousel = _ myCarousel; self. mySegmentControl = [[XTSegmentControl alloc] initWithFrame: CGRectMake (0, 0, kScreen_Width, kMySegmentControl_Height) Items: self. titlesArray selectedBlock: ^ (NSInteger index) {[weakCarousel scrollToItemAtIndex: index animated: NO] ;}]; [self. view addSubview: self. mySegmentControl];}-(void) handle {[super didReceiveMemoryWarning];} # pragma mark-Getter/Setter-(NSArray *) titlesArray {if (nil = _ titlesArray) {_ titlesArray = @ [@ "", @ ""];} return _ titlesArray;} # pragma mark iCarousel M-(NSUInteger) numberOfItemsInCarousel :( iCarousel *) carousel {return [self. titlesArray count];}-(UIView *) carousel :( iCarousel *) carousel viewForItemAtIndex :( NSUInteger) index reusingView :( UIView *) view {UIViewController * childContrll; switch (index) {case 0: {childContrll = [[ChildViewController alloc] init]; break;} case 1: {childContrll = [[oldChildVewController alloc] init]; break;} default: childContrll = [[newChildVewController alloc] init]; break;} return childContrll. view;}-(void) incluuseldidscroll :( iCarousel *) incluusel {if (_ mySegmentControl) {float offset = incluusel. scroloffset; if (offset> 0) {[_ mySegmentControl moveIndexWithProgress: offset] ;}}- (void) returns :( iCarousel *) carousel {if (_ mySegmentControl) {_ mySegmentControl. currentIndex = usel. currentItemIndex; }}@ end

You can also download the code here: Download the source code

 

Supplement 1:We won't let it scroll. We just arrange it in the width of a screen. If there are multiple screens that can scroll, we can refer to XTSegmentControl. m. The main changes are as follows:

#define XTSegmentControlHspace (12)- (void)initItemsWithTitleArray:(NSArray *)titleArray{    _itemFrames = @[].mutableCopy;    _items = @[].mutableCopy;    float y = 0;    float height = CGRectGetHeight(self.bounds);    NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:XTSegmentControlItemFont]};        NSObject *obj = [titleArray firstObject];    if ([obj isKindOfClass:[NSString class]]) {        for (int i = 0; i < titleArray.count; i++) {            NSString *title = titleArray[i];            CGSize size = [title sizeWithAttributes:attributes];                        float x = i > 0 ? CGRectGetMaxX([_itemFrames[i-1] CGRectValue]) : 0;            float width = 2 * XTSegmentControlHspace + size.width;            CGRect rect = CGRectMake(x, y, width, height);            [_itemFrames addObject:[NSValue valueWithCGRect:rect]];        }                for (int i = 0; i < titleArray.count; i++) {            CGRect rect = [_itemFrames[i] CGRectValue];            NSString *title = titleArray[i];            XTSegmentControlItem *item = [[XTSegmentControlItem alloc] initWithFrame:rect title:title type:XTSegmentControlItemTypeTitle];            if (i == 0) {                [item setSelected:YES];            }            [_items addObject:item];            [_contentView addSubview:item];        }    }        [_contentView setContentSize:CGSizeMake(CGRectGetMaxX([[_itemFrames lastObject] CGRectValue]), CGRectGetHeight(self.bounds))];    self.currentIndex = 0;    [self selectIndex:0];}- (void)addRedLine{    if (!_lineView) {        CGRect rect = [_itemFrames[0] CGRectValue];        _lineView = [[UIView alloc] initWithFrame:CGRectMake(XTSegmentControlHspace, CGRectGetHeight(rect) - XTSegmentControlLineHeight, CGRectGetWidth(rect) - 2 * XTSegmentControlHspace, XTSegmentControlLineHeight)];        _lineView.backgroundColor = [UIColor colorWithHexString:@"0x3bbd79"];        [_contentView addSubview:_lineView];                UIView *bottomLineView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(rect)-0.5, CGRectGetWidth(self.bounds), 0.5)];        bottomLineView.backgroundColor = [UIColor colorWithHexString:@"0xc8c7cc"];        [self addSubview:bottomLineView];    }}

 

Supplement 2:When the following part is manually scratched, if the menu is not on the current screen, it will not scroll to the position of the current menu, and it will also be simply modified;

XTSegmentControl. h (put the-(void) setscroloffset :( NSInteger) index out for calling outside)

@interface XTSegmentControl : UIView@property (nonatomic) NSInteger currentIndex;- (instancetype)initWithFrame:(CGRect)frame Items:(NSArray *)titleItem delegate:(id <XTSegmentControlDelegate>)delegate;- (instancetype)initWithFrame:(CGRect)frame Items:(NSArray *)titleItem selectedBlock:(XTSegmentControlBlock)selectedHandle;- (void)selectIndex:(NSInteger)index;- (void)moveIndexWithProgress:(float)progress;- (void)endMoveIndex:(NSInteger)index;- (void)setScrollOffset:(NSInteger)index;@end

Then, when the external icarousel is combined, the call is simply modified (mainly for this line [_ mySegmentControl setscroloffset: index]):

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view{    [_mySegmentControl setScrollOffset:index];    UIViewController *childContrll;    switch (index) {        case 0:        {            childContrll=[[ChildViewController alloc]init];                        break;        }        case 1:        {            childContrll=[[oldChildVewController alloc]init];                        break;        }        default:            childContrll=[[newChildVewController alloc]init];            break;    }    return childContrll.view;}

Modified code: Download source code

 

3: several good instances

A: https://github.com/HAHAKea/HACursor

 

B: https://github.com/fergusding/FDSlideBar

Related Article

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.