实现滑动菜单栏demo
来源:互联网 发布:北京搜索引擎优化seo 编辑:程序博客网 时间:2024/05/22 10:49
开发过程中,很多界面都会涉及到滑动菜单的需求, 最近忙里偷闲,写了一个小demo ,仅供参考!话不多说,上代码, ~~~
LXQScrollerView.h
- (instancetype)initWithFrame:(CGRect)frame titleArray:(NSArray *)array;
LXQScrollerView.m
#define kWidth [UIScreen mainScreen].bounds.size.width#define kHeight [UIScreen mainScreen].bounds.size.height@interface LXQScrollerView ()<UIScrollViewDelegate>@property (nonatomic, strong) UIScrollView *bigScrollerView;@property (nonatomic, strong) UIView *line;@property (nonatomic, strong) UIView *topView;@property (nonatomic, assign) NSInteger selectIndex;@property (nonatomic, strong) NSArray *titleArray;@property (nonatomic, assign) NSInteger count;@end@implementation LXQScrollerView- (instancetype)initWithFrame:(CGRect)frame titleArray:(NSArray *)array{ self = [super initWithFrame:frame]; if (self) { self.selectIndex = 0; self.titleArray = array; self.count = array.count; [self prepareTopUI]; [self prepareScrollerViewUI]; } return self;}- (void)prepareTopUI{ self.line = [[UIView alloc] initWithFrame:CGRectMake(0, 49, kWidth / self.count, 1)]; self.line.backgroundColor = [UIColor redColor]; self.topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 50)]; self.topView.backgroundColor = [UIColor whiteColor]; for (NSInteger i = 0; i < self.count; i++) { UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(kWidth / self.count * i, 0, kWidth / self.count, 49); [button setTitle:_titleArray[i] forState:UIControlStateNormal]; [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; button.tag = i; [self.topView addSubview:button]; } [self.topView addSubview:self.line]; [self addSubview:self.topView]; [self changeButtonTextColor];}- (void)prepareScrollerViewUI{ self.bigScrollerView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 50, kWidth, self.frame.size.height - 50)]; self.bigScrollerView.contentSize = CGSizeMake(kWidth * self.count, self.frame.size.height - 50); self.bigScrollerView.scrollEnabled = YES; self.bigScrollerView.pagingEnabled = YES; self.bigScrollerView.delegate = self; [self addSubview:self.bigScrollerView]; NSArray *array = @[[UIColor redColor], [UIColor yellowColor], [UIColor grayColor]]; for (NSInteger i = 0; i < self.count; i++) { UIView *view = [[UIView alloc] initWithFrame:CGRectMake(kWidth * i, 0, kWidth, kHeight - 64 - 50)]; view.backgroundColor = array[i]; [self.bigScrollerView addSubview:view]; }}- (void)buttonAction:(UIButton *)button{ self.selectIndex = button.tag; [self changeButtonTextColor]; [self changeLinePlaceWithIndex:button.tag]; [self changeScrollerViewPlace];}//改变滚动视图位置- (void)changeScrollerViewPlace{ [UIView animateWithDuration:0.5 animations:^{ CGPoint offset = self.bigScrollerView.contentOffset; offset.x = kWidth * self.selectIndex; self.bigScrollerView.contentOffset = offset; }];}//改变选中字体颜色- (void)changeButtonTextColor{ for (UIView *view in [self.topView subviews]) { if ([view isKindOfClass:[UIButton class]]) { UIButton *button = (UIButton *)view; if (button.tag == self.selectIndex) { [button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal]; }else{ [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; } } }}//改变line位置- (void)changeLinePlaceWithIndex:(NSInteger)index{ [UIView animateWithDuration:0.5 animations:^{ CGRect frame = self.line.frame; frame.origin.x = kWidth / self.count * index; self.line.frame = frame; }];}#pragma mark - UIScrollViewDelegate- (void)scrollViewDidScroll:(UIScrollView *)scrollView{ CGFloat x = scrollView.contentOffset.x; self.selectIndex = x / kWidth; [self changeButtonTextColor]; [self changeLinePlaceWithIndex:self.selectIndex];}
以上是自定义的view, 下面简单列举一下其使用方法:
用法很简答, 只需要引入 #import “LXQScrollerView.h”即可
self.scrollerView = [[LXQScrollerView alloc] initWithFrame:CGRectMake(0, 64, kWidth, kHeight - 64) titleArray:@[@"手机", @"电脑", @"微波炉"]]; [self.view addSubview:self.scrollerView];
这样就能展示相应的滑动菜单了!
阅读全文
0 0
- 实现滑动菜单栏demo
- JS实现侧部菜单栏的滑动
- 【Android】viewpager+fragment+tablayout实现滑动菜单栏
- HTML5菜单栏切换实现底部滑块滑动效果实现
- 滑动菜单栏(三)SlidingMenu动画效果的实现
- 滑动菜单栏(三)SlidingMenu动画效果的实现
- 滑动菜单栏(三)SlidingMenu动画效果的实现
- Android:滑动菜单栏(三)SlidingMenu动画效果的实现
- Android使用TabLayout+ViewPager实现顶部滑动菜单栏
- 菜单栏滑动效果
- Android 左边滑动菜单栏
- iOS左侧滑动菜单栏
- android底部菜单栏demo
- android底部菜单栏demo
- android底部菜单栏demo .
- wpf实现iphone滑动效果demo
- ViewPager(横向滑动菜单) 实现Demo
- 实现图片滑动、滚动、菜单栏(导航)滑动、文字滚动等效果的网页插件superslide2
- ART运行时Java堆创建过程分析
- centos7 安装部署 jdk1.8.0_131
- MySQL的日期函数
- MySql存储过程与函数
- 启动服务出现错误193: 0xc1时的处理方法
- 实现滑动菜单栏demo
- Oracle_存储过程基本概念
- Hdu 2091 空心三角形
- Nginx负载均衡session会话保持方法
- linux系统中 ulimit限制
- 使用Yocto移植Qt5到Beaglebone Black板
- Java网络编程
- java获取视频播第一帧
- error C2143: syntax error : missing ';' before '<'报错解决办法