頂部滑動(dòng)菜單FDSlideBar
作者:fergus_ding
FDSlideBar是一個(gè)頂部滑動(dòng)菜單,如常見的網(wǎng)易、騰訊新聞等樣式。該控件支持自定顏色、字體等多種樣式風(fēng)格。
源碼簡介:FDSlideBar是一個(gè)頂部滑動(dòng)菜單,如常見的網(wǎng)易、騰訊新聞等樣式。該控件支持自定顏色、字體等多種樣式風(fēng)格。菜單間切換流暢,具有較好的體驗(yàn)性。下部的內(nèi)容展示經(jīng)過掙扎,***選擇了UITableView實(shí)現(xiàn),從而很好地解決了ScrollView的內(nèi)存問題,也獲得了很好地原生滑動(dòng)效果。
測試環(huán)境:Xcode 6.2,iOS 6.0以上
源碼截圖:
源碼片段:
- FDSlideBar *sliderBar = [[FDSlideBar alloc] init];
- sliderBar.backgroundColor = [UIColor colorWithRed:0 / 255.0 green:128 / 255.0 blue:128 / 255.0 alpha:1.0];
- // Init the titles of all the item
- sliderBar.itemsTitle = @[@"要聞", @"視頻", @"上海", @"娛樂", @"體育NBA", @"財(cái)經(jīng)", @"科技", @"社會(huì)", @"軍事", @"時(shí)尚", @"汽車", @"游戲", @"圖片", @"股票"];
- // Set some style to the slideBar
- sliderBar.itemColor = [UIColor whiteColor];
- sliderBar.itemSelectedColor = [UIColor orangeColor];
- sliderBar.sliderColor = [UIColor orangeColor];
- // Add the callback with the action that any item be selected
- [sliderBar slideBarItemSelectedCallback:^(NSUInteger idx) {
- [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:idx inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
- }];
- [self.view addSubview:sliderBar];
- _slideBar = sliderBar;
- // The frame of tableView, be care the width and height property
- CGRect frame = CGRectMake(0, 0, CGRectGetMaxY(self.view.frame) - CGRectGetMaxY(self.slideBar.frame), CGRectGetWidth(self.view.frame));
- self.tableView = [[UITableView alloc] initWithFrame:frame];
- [self.view addSubview:self.tableView];
- // Register the custom cell
- UINib *nib = [UINib nibWithNibName:@"TableViewCell" bundle:nil];
- [self.tableView registerNib:nib forCellReuseIdentifier:@"ContentCell"];
- // Set the tableView center in the bottom of view. so after rotating, it shows rightly
- self.tableView.center = CGPointMake(CGRectGetWidth(self.view.frame) * 0.5, CGRectGetHeight(self.view.frame) * 0.5 + CGRectGetMaxY(self.slideBar.frame) * 0.5);
- self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
- // Rotate the tableView 90 angle anticlockwise
- self.tableView.transform = CGAffineTransformMakeRotation(-M_PI_2);
- self.tableView.showsVerticalScrollIndicator = NO;
- self.tableView.pagingEnabled = YES;
- self.tableView.dataSource = self;
- self.tableView.delegate = self;
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- TableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"ContentCell"];
- // Rotate the cell's content 90 angle clockwise to show them rightly
- cell.contentView.transform = CGAffineTransformMakeRotation(M_PI_2);
- cell.text = self.slideBar.itemsTitle[indexPath.row];
- return cell;
- }
責(zé)任編輯:倪明