源码简介:这是一款仿照暴风影音做的demo,因为项目需要,所以顺便把他完善一点,功能有侧滑,滚动导航栏,tableView ,collectionView的高度定制,希望能帮助到有需求的码友。
源码效果:
源码片段:
- - (void)viewDidLoad
- {
- self.view.backgroundColor = [UIColor whiteColor];
- UIView *statusBarView = [[UIImageView alloc] initWithFrame:CGRectMake(0.f, 0.f, self.view.frame.size.width, 0.f)];
- if (isIos7 >= 7 && __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_6_1)
- {
- statusBarView.frame = CGRectMake(statusBarView.frame.origin.x, statusBarView.frame.origin.y, statusBarView.frame.size.width, 20.f);
- statusBarView.backgroundColor = [UIColor clearColor];
- ((UIImageView *)statusBarView).backgroundColor = RGBA(33.f,125.f,194.f,1);
- [self.view addSubview:statusBarView];
- }
- //导航栏
- _navView = [[UIImageView alloc] initWithFrame:CGRectMake(0.f, StatusbarSize, self.view.frame.size.width, 50.f)];
- ((UIImageView *)_navView).backgroundColor = RGBA(33.f,125.f,194.f,1);
- [self.view insertSubview:_navView belowSubview:statusBarView];
- _navView.userInteractionEnabled = YES;
- //导航栏图标
- [self setNavbtn];
- //滚动导航栏
- _topNaviV = [[UIView alloc] initWithFrame:CGRectMake(0, _navView.frame.size.height + _navView.frame.origin.y, self.view.frame.size.width, MENU_HEIGHT)];
- _topNaviV.backgroundColor = RGBA(33.f,125.f,194.f,1);
- [self.view addSubview:_topNaviV];
- //滚动页面
- _scrollV = [[UIScrollView alloc] initWithFrame:CGRectMake(0, _topNaviV.frame.origin.y + _topNaviV.frame.size.height, self.view.frame.size.width, self.view.frame.size.height - _topNaviV.frame.origin.y - _topNaviV.frame.size.height)];
- _scrollV.tag = _scrollVTag;
- [_scrollV setPagingEnabled:YES];
- [_scrollV setShowsHorizontalScrollIndicator:NO];
- [self.view insertSubview:_scrollV belowSubview:_navView];
- _scrollV.delegate = self;
- [_scrollV.panGestureRecognizer addTarget:self action:@selector(scrollHandlePan:)];
- //选择弹出的view
- _selectTabV = [[UIView alloc] initWithFrame:CGRectMake(0, _scrollV.frame.origin.y - _scrollV.frame.size.height, _scrollV.frame.size.width, _scrollV.frame.size.height)];
- [_selectTabV setBackgroundColor:RGBA(255.f, 209.f, 56.f, 1)];
- [_selectTabV setHidden:YES];
- [self.view insertSubview:_selectTabV belowSubview:_navView];
- //创建滚动条菜单
- [self createScrollBtns];
- }
- //创建主导航菜单
- -(void)setNavbtn{
- UIButton *MenuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- [MenuBtn setFrame:CGRectMake(15, 8, 20, 20)];
- [MenuBtn setBackgroundImage:[UIImage imageNamed:@"icon_list"] forState:UIControlStateNormal];
- [MenuBtn addTarget:self action:@selector(leftAction:) forControlEvents:UIControlEventTouchUpInside];
- MenuBtn.showsTouchWhenHighlighted = YES;
- [_navView addSubview:MenuBtn];
- for (int i = 0; i < 4; i++) {
- UIButton *NavBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- [NavBtn setFrame:CGRectMake(170 + i*40, 8 , 20, 20)];
- NSString *NavBtn_backimg = [NSString stringWithFormat:@"slide_menu_%d@2x",i+1];
- [NavBtn setBackgroundImage:[UIImage imageNamed:NavBtn_backimg] forState:UIControlStateNormal];
- [NavBtn addTarget:self action:@selector(NavbtnAction:) forControlEvents:UIControlEventTouchUpInside];
- NavBtn.tag = MENU_NAVBUTTON_TAG + i;
- [_navView addSubview:NavBtn];
- }
- }
- //创建scroll 滚动菜单
- - (void)createScrollBtns
- {
- float btnW = 40;
- UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
- [btn setFrame:CGRectMake(_topNaviV.frame.size.width - btnW, 0, btnW, 30)];
- [btn setBackgroundImage:[UIImage imageNamed:@"nav_more"] forState:UIControlStateNormal];
- [_topNaviV addSubview:btn];
- [btn addTarget:self action:@selector(showSelectView:) forControlEvents:UIControlEventTouchUpInside];
- _scrollDataSource = [NSMutableArray arrayWithObjects:@"推荐", @"电影", @"电视剧", @"卡通", @"综艺", @"体育", @"娱乐", @"新闻", nil];
- _navScrollV = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - btnW, MENU_HEIGHT)];
- _navScrollV.tag = _navScrollVTag;
- [_navScrollV setShowsHorizontalScrollIndicator:NO];
- for (int i = 0; i < [_scrollDataSource count]; i++)
- {
- UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
- [btn setFrame:CGRectMake(MENU_BUTTON_WIDTH * i, -5, MENU_BUTTON_WIDTH, MENU_HEIGHT)];
- [btn setTitle:[_scrollDataSource objectAtIndex:i] forState:UIControlStateNormal];
- [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
- btn.tag = i + 1;
- [btn addTarget:self action:@selector(ScrolBtnActionbtn:) forControlEvents:UIControlEventTouchUpInside];
- btn.showsTouchWhenHighlighted = YES;
- [_navScrollV addSubview:btn];
- }
- [_navScrollV setContentSize:CGSizeMake(MENU_BUTTON_WIDTH * [_scrollDataSource count], MENU_HEIGHT)];
- [_topNaviV addSubview:_navScrollV];
- //滚动栏底部 滚动条
- _navBgV = [[UIView alloc] initWithFrame:CGRectMake(0, MENU_HEIGHT - 5, MENU_BUTTON_WIDTH, 5)];
- [_navBgV setBackgroundColor:[UIColor redColor]];
- [_navScrollV addSubview:_navBgV];
- [self addView2Page:_scrollV count:[_scrollDataSource count] frame:CGRectZero];
- }
- //初始化视图内容 显示当前选中scroll的btn要显示的内容
- - (void)addView2Page:(UIScrollView *)scrollV count:(NSUInteger)pageCount frame:(CGRect)frame
- {
- for (int i = 0; i < pageCount; i++)
- {
- UIView *view = [[UIView alloc] initWithFrame:CGRectMake(scrollV.frame.size.width * i, 0, scrollV.frame.size.width, scrollV.frame.size.height)];
- view.tag = i + 1;
- //初始化collectionViews 视频视图
- [self creatScrollSubViewsInViews:view];
- [scrollV addSubview:view];
- }
- [scrollV setContentSize:CGSizeMake(scrollV.frame.size.width * pageCount, scrollV.frame.size.height)];
- }