国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS仿網易簡單頭部滾動效果

2019-10-21 18:41:04
字體:
來源:轉載
供稿:網友

本文實例為大家分享了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 <UIKit/UIKit.h>@interface TitleScrollView : UIScrollViewtypedef 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;   //按鈕的寬度/** * 通過標題數組進行設置頭部滾動條 * * @param array 需要加入的標題 */-(void)AddArrView:(NSArray*)array;/** * 可直接用代碼設置索引位置 * * @param index 索引位置 */-(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; i<array.count; i++) {  //初始化所有btn  UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(i*_ButtonWidth, 0, _ButtonWidth,34)];  [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];  btn.titleLabel.font = [UIFont systemFontOfSize:12];  btn.titleLabel.textAlignment = NSTextAlignmentCenter;  [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];  [btn setTitle:array[i] forState:UIControlStateNormal];  btn.tag = TitleBtnTag+i;  [self addSubview:btn];  [self.BtnArr addObject:btn]; } //根據button個數設置內部大小 [self setContentSize:CGSizeMake(array.count*_ButtonWidth, CGRectGetHeight(self.frame))];}-(void)click:(UIButton*)button{ //把所有的btn樣式重置 for (UIButton *btn in self.BtnArr) {  btn.titleLabel.font = [UIFont systemFontOfSize:12];  btn.titleLabel.textAlignment = NSTextAlignmentCenter;  [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; } //特殊設置點擊的button樣式 [button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal]; //計算獲得偏移量, CGFloat index = (button.tag-TitleBtnTag)*_ButtonWidth-(self.frame.size.width-_ButtonWidth)/2; index = index<0?0:index; index = index>self.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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 略阳县| 宣恩县| 新宾| 都匀市| 平遥县| 平和县| 泰安市| 平武县| 靖边县| 九龙坡区| 海伦市| 惠东县| 恭城| 北海市| 扶风县| 墨江| 天峻县| 宁河县| 崇礼县| 永和县| 洛浦县| 仪陇县| 出国| 太和县| 云林县| 湖南省| 江山市| 湟中县| 荆州市| 微山县| 岳池县| 宁明县| 盐源县| 北宁市| 宁远县| 楚雄市| 大邑县| 赣榆县| 玉环县| 丰县| 宁都县|