iOS仿网易简单头部滚动效果
本文实例为大家分享了iOS仿网易滚动效果片展示的具体代码,供大家参考,具体内容如下
仿网易的主要思想为:
1.设置好按钮与线的宽度,
2.将所需要的标题传入并生成按钮
3.在点击的时候,通过计算偏移量,将自身进行偏移
4.偏移量的设置需要注意不能小于0并且不成大于contengsize-frame的宽度
具体代码如下,可直接使用,需要注意的是需要先设置宽度,再传标题数组才可自动调整,否则会固定为默认的60
另外,BtnArr与linelabel设置为readonly比较合理,不过这里还需再进行研究,不要强制使用这两个属性即可
头文件如下:
// //TitleScrollView.h //@author陈晋添 // //Createdbyjkcon16/7/14. //Copyright©2016年cjt.Allrightsreserved. // #import@interfaceTitleScrollView:UIScrollView typedefvoid(^sectionTitleViewBlock)(NSIntegernum); @property(nonatomic,strong)NSMutableArray*BtnArr;//形成的按钮数组 @property(nonatomic,strong)UILabel*linelabel;//底部line @property(nonatomic,strong)sectionTitleViewBlockclickBolck;//block回调 @property(nonatomic,assign)NSIntegerLineWidth;//设置线的长度 @property(nonatomic,assign)NSIntegerButtonWidth;//按钮的宽度 /** *通过标题数组进行设置头部滚动条 * *@paramarray需要加入的标题 */ -(void)AddArrView:(NSArray*)array; /** *可直接用代码设置索引位置 * *@paramindex索引位置 */ -(void)setByIndex:(NSInteger)index; @end
.m文件如下
//
//TitleScrollView.m
//@author陈晋添
//
//Createdbyjkcon16/7/14.
//Copyright©2016年cjt.Allrightsreserved.
//
#import"TitleScrollView.h"
#defineTitleBtnTag300//button的tag值
@implementationTitleScrollView
-(instancetype)initWithFrame:(CGRect)frame
{
if(self=[superinitWithFrame:frame]){
//初始化自身
[selfsetBackgroundColor:[UIColorwhiteColor]];
self.showsHorizontalScrollIndicator=false;
_ButtonWidth=_LineWidth=60;
self.linelabel=[[UILabelalloc]initWithFrame:CGRectMake(0,self.frame.size.height-1.5,_LineWidth,1.5)];
[self.linelabelsetBackgroundColor:TintColor];
[selfaddSubview:self.linelabel];
}
returnself;
}
-(void)AddArrView:(NSArray*)array
{
self.BtnArr=[NSMutableArrayarray];
for(inti=0;iself.contentSize.width-CGRectGetWidth(self.frame)?self.contentSize.width-CGRectGetWidth(self.frame):index;
//动画效果偏移
[selfsetContentOffset:CGPointMake(index,0)animated:YES];
[UIViewanimateWithDuration:0.3animations:^{
self.linelabel.frame=CGRectMake((button.tag-TitleBtnTag)*_ButtonWidth,self.frame.size.height-1,_LineWidth,1);
}];
self.clickBolck(button.tag);
}
//通过外部代码直接设置索引
-(void)setByIndex:(NSInteger)nowindex
{
UIButton*button=self.BtnArr[nowindex];
[selfclick:button];
}
@end
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。