iOS仿网易简单头部滚动效果

  

本文实例为大家分享了iOS仿网易滚动效果片展示的具体代码,供大家参考,具体内容如下

  

仿网易的主要思想为:
  

  

1。设置好按钮与线的宽度,
  2. 将所需要的标题传入并生成按钮
  3.在点击的时候,通过计算偏移量,将自身进行偏移
  4. 偏移量的设置需要注意不能小于0并且不成大于contengsize-frame的宽度

  

具体代码如下,可直接使用,需要注意的是需要先设置宽度,再传标题数组才可自动调整,否则会固定为默认的60

  

另外,BtnArr与linelabel设置为只读的比较合理,不过这里还需再进行研究,不要强制使用这两个属性即可

  

头文件如下:

     ////TitleScrollView.h//@author陈晋添////由jkc>////TitleScrollView.m//@author陈晋添////由jkc 16/7/14。//版权软件是2016年公司。保留所有权利。//#进口“TitleScrollView.h”      #定义TitleBtnTag 300//按钮的标签值   @ implementation TitleScrollView      - (instancetype) initWithFrame:(CGRect中)框架   {   如果(自我=(超级initWithFrame:帧)){//初始化自身   (自我setBackgroundColor:(用户界面颜色whiteColor]];   自我。showsHorizontalScrollIndicator=false;   _ButtonWidth=_LineWidth=60;      自我。linelabel=[[UILabel alloc] initWithFrame: CGRectMake (_LineWidth 0, self.frame.size.height - 1.5, 1.5)];   (自我。linelabel setBackgroundColor TintColor):;   (自我addSubview: self.linelabel);   }   回归自我;   }      - (void) AddArrView: (NSArray *)数组   {   自我。BtnArr=[NSMutableArray数组);   for (int i=0;i self.contentSize.width-CGRectGetWidth (self.frame) & # 63; self.contentSize.width-CGRectGetWidth (self.frame):指数;//动画效果偏移   【自我setContentOffset: CGPointMake(指数(0)动画:是的);   [UIView animateWithDuration: 0.3动画:^ {   self.linelabel.frame=CGRectMake ((button.tag-TitleBtnTag) * _ButtonWidth、self.frame.size.height-1 _LineWidth, 1);   });      self.clickBolck (button.tag);   }//通过外部代码直接设置索引   - (void) setByIndex: nowindex (NSInteger)   {   UIButton *按钮=self.BtnArr [nowindex];   (自我点击:按钮);   }      @end      之前      

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

iOS仿网易简单头部滚动效果