最新消息:文章中包含代码时,请遵守代码高亮规范!

iOS滑动固定菜单的简单实现【转载】

iOS 李, 永奎 116浏览 0评论

转载自http://blog.it985.com/20179.html

用在UIScrollView的滑动固定菜单,主要是在scrollViewDidScroll代理方法里判断scrollView.contentOffset.y实现的。
初始化视图的代码:

UIView *navBgView = [UIView viewWithFrame:CGRectMake(0, 0, kScreenWidth, 64) backgroundColor:kDefaultOrangeColor];
[self.view addSubview:navBgView];//自定义导航栏
mainScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, kHeaderHeight, kScreenWidth, kScreenHeight – kHeaderHeight – 49)];
mainScrollView.delegate = self;
[self.view addSubview:mainScrollView];

picScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenWidth * 326 / 750.0) imageNamesGroup:nil];//轮播图
picScrollView.bannerImageViewContentMode = UIViewContentModeScaleAspectFill;
picScrollView.backgroundColor = kBrownColor;
[mainScrollView addSubview:picScrollView];

UIView *lightGrayColorView1 = [UIView viewWithFrame:CGRectMake(0, picScrollView.maxY, kScreenWidth, 10) backgroundColor:RGB(237, 237, 237)];
[mainScrollView addSubview:lightGrayColorView1];

whiteColorView1 = [UIView viewWithFrame:CGRectMake(0, lightGrayColorView1.maxY, kScreenWidth, 40) backgroundColor:kWhiteColor];//菜单视图
[mainScrollView addSubview:whiteColorView1];

NSUInteger arrayCount = buttonTitleArray.count;
for (int i = 0; i picScrollView.maxY + 10 + 20) {
//滑动到一定程度就把菜单从滚动视图上移除并添加到self.view上
if (![self.view.subviews containsObject:whiteColorView1]) {//通过判断是否有这个子视图防止重复移除
whiteColorView1.minY = kHeaderHeight;//关键:调整菜单视图的y
[whiteColorView1 removeFromSuperview];

[self.view addSubview:whiteColorView1];
}
}else{
if (![mainScrollView.subviews containsObject:whiteColorView1]) {
whiteColorView1.minY = picScrollView.maxY + 10;//关键:调整菜单视图的y
[whiteColorView1 removeFromSuperview];

[mainScrollView addSubview:whiteColorView1];
}
}
}
}

转载时请注明出处及相应链接,本文永久地址:http://blog.it985.com/22929.html


pay_weixin
pay_weixin
微信打赏
pay_weixin
支付宝打赏
感谢您对作者Future的打赏,我们会更加努力!    如果您想成为作者,请点我

您必须 登录 才能发表评论!