iOS仿網(wǎng)易簡單頭部滾動(dòng)效果
本文實(shí)例為大家分享了iOS仿網(wǎng)易滾動(dòng)效果片展示的具體代碼,供大家參考,具體內(nèi)容如下
仿網(wǎng)易的主要思想為:
1. 設(shè)置好按鈕與線的寬度,
2. 將所需要的標(biāo)題傳入并生成按鈕
3. 在點(diǎn)擊的時(shí)候,通過計(jì)算偏移量,將自身進(jìn)行偏移
4. 偏移量的設(shè)置需要注意不能小于0并且不成大于contengsize-frame的寬度
具體代碼如下,可直接使用,需要注意的是需要先設(shè)置寬度,再傳標(biāo)題數(shù)組才可自動(dòng)調(diào)整,否則會(huì)固定為默認(rèn)的60
另外,BtnArr與linelabel設(shè)置為readonly比較合理,不過這里還需再進(jìn)行研究,不要強(qiáng)制使用這兩個(gè)屬性即可
頭文件如下:
// // TitleScrollView.h // @author 陳晉添 // // Created by jkc on 16/7/14. // Copyright © 2016年 cjt. All rights reserved. // #import <UIKit/UIKit.h> @interface TitleScrollView : UIScrollView typedef void(^sectionTitleViewBlock)(NSInteger num); @property (nonatomic, strong) NSMutableArray *BtnArr; //形成的按鈕數(shù)組 @property (nonatomic, strong) UILabel *linelabel; //底部line @property (nonatomic, strong) sectionTitleViewBlock clickBolck; //block回調(diào) @property (nonatomic, assign) NSInteger LineWidth; //設(shè)置線的長度 @property (nonatomic, assign) NSInteger ButtonWidth; //按鈕的寬度 /** * 通過標(biāo)題數(shù)組進(jìn)行設(shè)置頭部滾動(dòng)條 * * @param array 需要加入的標(biāo)題 */ -(void)AddArrView:(NSArray*)array; /** * 可直接用代碼設(shè)置索引位置 * * @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]; } //根據(jù)button個(gè)數(shù)設(shè)置內(nèi)部大小 [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]; } //特殊設(shè)置點(diǎn)擊的button樣式 [button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal]; //計(jì)算獲得偏移量, 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; //動(dòng)畫效果偏移 [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); } //通過外部代碼直接設(shè)置索引 -(void)setByIndex:(NSInteger)nowindex { UIButton *button = self.BtnArr[nowindex]; [self click:button]; } @end
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- IOS上iframe的滾動(dòng)條失效的解決辦法
- iOS實(shí)現(xiàn)滾動(dòng)字幕的動(dòng)畫特效
- IOS中無限滾動(dòng)Scrollview效果
- iOS利用UIScrollView實(shí)現(xiàn)無限滾動(dòng)效果
- iOS實(shí)現(xiàn)無限循環(huán)滾動(dòng)的TableView實(shí)戰(zhàn)教程
- iOS仿網(wǎng)易新聞滾動(dòng)導(dǎo)航條效果
- 使用Swift實(shí)現(xiàn)iOScollectionView廣告無限滾動(dòng)效果(DEMO)
- iOS使用UICollectionView實(shí)現(xiàn)橫向滾動(dòng)照片效果
- iOS中無限循環(huán)滾動(dòng)簡單處理實(shí)現(xiàn)原理分析
- iOS實(shí)現(xiàn)文字水平無間斷滾動(dòng)效果
相關(guān)文章
iOS開發(fā)狀態(tài)欄及設(shè)置功能全面詳解
這篇文章主要為大家介紹了iOS開發(fā)狀態(tài)欄及設(shè)置功能全面詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06iOS實(shí)現(xiàn)點(diǎn)擊圖片放大和長按保存圖片的示例
本篇文章主要介紹了iOS實(shí)現(xiàn)點(diǎn)擊圖片放大和長按保存圖片的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03