SoFunction
Updated on 2025-04-12

iOS imitation NetEase simple head scrolling effect

This article shares the specific code for iOS imitation NetEase scrolling effect movie display for your reference. The specific content is as follows

The main idea of ​​imitating NetEase is:

1. Set the width of the buttons and lines.
2. Pass the required title and generate the button
3. When clicking, offset itself by calculating the offset
4. When setting the offset, please note that it cannot be less than 0 and cannot be greater than the width of the contengsize-frame.

The specific code is as follows, which can be used directly. It should be noted that you need to set the width first, and then pass the title array before you can adjust it automatically, otherwise it will be fixed to the default 60.

In addition, it is more reasonable to set BtnArr and linelabel to readonly, but it needs to be studied here, so don't force the use of these two properties.

The header file is as follows:

//
// 
// @author Chen Jintian//
// Created by jkc on 16/7/14.
// Copyright © 2016 cjt. All rights reserved.//

#import <UIKit/>

@interface TitleScrollView : UIScrollView

typedef void(^sectionTitleViewBlock)(NSInteger num);

@property (nonatomic, strong) NSMutableArray *BtnArr;   //The button array formed@property (nonatomic, strong) UILabel *linelabel;    //Bottom line@property (nonatomic, strong) sectionTitleViewBlock clickBolck; //block callback
@property (nonatomic, assign) NSInteger LineWidth;    //Set the length of the line@property (nonatomic, assign) NSInteger ButtonWidth;   //The width of the button
/**
  * Set the header scrollbar through the title array
  *
  * @param array Title that needs to be added
  */
-(void)AddArrView:(NSArray*)array;

/**
  * You can directly set the index position with code
  *
  * @param index index position
  */
-(void)setByIndex:(NSInteger)index;
@end

The .m file is as follows

//
// 
// @author Chen Jintian//
// Created by jkc on 16/7/14.
// Copyright © 2016 cjt. All rights reserved.//

#import ""

#define TitleBtnTag 300 //button's tag value@implementation TitleScrollView

-(instancetype)initWithFrame:(CGRect)frame
{
 if (self = [super initWithFrame:frame]) {

  //Initialize yourself  [self setBackgroundColor:[UIColor whiteColor]];
   = false;
  _ButtonWidth = _LineWidth = 60;

   = [[UILabel alloc] initWithFrame:CGRectMake(0, -1.5, _LineWidth, 1.5)];
  [ setBackgroundColor:TintColor];
  [self addSubview:];
 }
 return self;
}

-(void)AddArrView:(NSArray*)array
{
  = [NSMutableArray array];
 for (int i=0; i<; i++) {
  //Initialize all btns  UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(i*_ButtonWidth, 0, _ButtonWidth,34)];
  [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
   = [UIFont systemFontOfSize:12];
   = NSTextAlignmentCenter;
  [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  [btn setTitle:array[i] forState:UIControlStateNormal];

   = TitleBtnTag+i;

  [self addSubview:btn];
  [ addObject:btn];
 }
 //Set the internal size according to the number of buttons [self setContentSize:CGSizeMake(*_ButtonWidth, CGRectGetHeight())];
}

-(void)click:(UIButton*)button
{
 //Reset all btn styles for (UIButton *btn in ) {
   = [UIFont systemFontOfSize:12];
   = NSTextAlignmentCenter;
  [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
 }

 //Specially set the button style of click [button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];

 // Calculate to obtain the offset, CGFloat index = (-TitleBtnTag)*_ButtonWidth-(-_ButtonWidth)/2;
 index = index<0?0:index;
 index = index>-CGRectGetWidth()?-CGRectGetWidth():index;

 //Animation effect offset [self setContentOffset:CGPointMake(index, 0) animated:YES];
 [UIView animateWithDuration:0.3 animations:^{
   = CGRectMake((-TitleBtnTag)*_ButtonWidth, -1, _LineWidth, 1);
 }];

 ();
}

//Set the index directly through external code-(void)setByIndex:(NSInteger)nowindex
{
 UIButton *button = [nowindex];
 [self click:button];
}

@end

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.