栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > iOS

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

iOS 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

仿网易的主要思想为:

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

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

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

头文件如下:

//
// TitleScrollView.h
// @author 陈晋添
//
// Created by jkc on 16/7/14.
// Copyright © 2016年 cjt. All rights reserved.
//

#import 

@interface TitleScrollView : UIScrollView

typedef void(^sectionTitleViewBlock)(NSInteger num);

@property (nonatomic, strong) NSMutableArray *BtnArr;   //形成的按钮数组
@property (nonatomic, strong) UILabel *linelabel;    //底部line
@property (nonatomic, strong) sectionTitleViewBlock clickBolck; //block回调

@property (nonatomic, assign) NSInteger LineWidth;    //设置线的长度
@property (nonatomic, assign) NSInteger ButtonWidth;   //按钮的宽度


-(void)AddArrView:(NSArray*)array;


-(void)setByIndex:(NSInteger)index;
@end

.m文件如下

//
// TitleScrollView.m
// @author 陈晋添
//
// Created by jkc on 16/7/14.
// Copyright © 2016年 cjt. All rights reserved.
//

#import "TitleScrollView.h"

#define TitleBtnTag 300   //button的tag值
@implementation TitleScrollView

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

  //初始化自身
  [self setBackgroundColor:[UIColor whiteColor]];
  self.showsHorizontalScrollIndicator = false;
  _ButtonWidth = _LineWidth = 60;

  self.linelabel = [[UILabel alloc] initWithframe:CGRectMake(0, self.frame.size.height-1.5, _LineWidth, 1.5)];
  [self.linelabel setBackgroundColor:TintColor];
  [self addSubview:self.linelabel];
 }
 return self;
}

-(void)AddArrView:(NSArray*)array
{
 self.BtnArr = [NSMutableArray array];
 for (int i=0; iself.contentSize.width-CGRectGetWidth(self.frame)?self.contentSize.width-CGRectGetWidth(self.frame):index;

 //动画效果偏移
 [self setContentOffset:CGPointMake(index, 0) animated:YES];
 [UIView animateWithDuration:0.3 animations:^{
  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];
 [self click:button];
}

@end

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/166223.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号