用ScrollView设置导航栏滚动时图,代码如下:
#define HScreen [[UIScreen mainScreen] bounds].size.height
#define WScreen [[UIScreen mainScreen] bounds].size.width
@interface WHINewsViewController ()<UIScrollViewDelegate>
@property (nonatomic,strong)UIScrollView *scollerTop;
@property (nonatomic,strong)UIScrollView *scollerView;
@end
@implementation WHINewsViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *title= @[@"兑换中",@"兑换中",@"即将开始",@"即将开始",@"即将开始"];
NSArray *title2= @[@"9:00",@"11:00",@"15:00",@"17:00",@"20:00"];
self.scollerTop.bounces = NO;
self.scollerTop.showsHorizontalScrollIndicator =NO;
self.scollerTop.showsVerticalScrollIndicator =NO;
// self.scollerTop.frame = CGRectMake(0, 0, WScreen, 48);
self.scollerTop = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WScreen, 48)];
self.scollerTop.backgroundColor = [UIColor clearColor];
[self.view addSubview:self.scollerTop];
_scollerView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 48, WScreen, HScreen-48)];
_scollerView.showsHorizontalScrollIndicator = NO;
_scollerView.showsVerticalScrollIndicator = NO;
_scollerView.bounces = NO;
_scollerView.pagingEnabled = YES;
_scollerView.delegate = self;
// _scollerView.pagingEnabled = YES;
_scollerView.userInteractionEnabled = YES;
// _scollerView.showsHorizontalScrollIndicator = NO;
_scollerView.contentSize = CGSizeMake(5*WScreen, 0);
[self.view addSubview:_scollerView];
self.scollerTop.tag = 1;
for (int i= 0; i<title.count; i++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(i*WScreen/title.count, 0, WScreen/title.count, 46);
btn.titleLabel.font = [UIFont systemFontOfSize:11];
btn.tag = i+100;
[btn addTarget:self action:@selector(actionBtn:) forControlEvents:UIControlEventTouchUpInside];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self.scollerTop addSubview:btn];
UILabel *ScrollTopLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(i*WScreen/title.count, 23, WScreen/title.count, 20)];
ScrollTopLabel1.textAlignment = NSTextAlignmentCenter;
[ScrollTopLabel1 setText:title[i]];
ScrollTopLabel1.tag = i+300;
[ScrollTopLabel1 setTextColor:[UIColor blackColor]];
ScrollTopLabel1.font = [UIFont systemFontOfSize:11];
[self.scollerTop addSubview:ScrollTopLabel1];
UILabel *ScrollTopLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(i*WScreen/title.count, 6, WScreen/title.count, 20)];
ScrollTopLabel2.textAlignment = NSTextAlignmentCenter;
[ScrollTopLabel2 setText:title2[i]];
ScrollTopLabel2.tag = i+400;
[ScrollTopLabel2 setTextColor:[UIColor blackColor]];
ScrollTopLabel2.font = [UIFont systemFontOfSize:16];
[self.scollerTop addSubview:ScrollTopLabel2];
}
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 46, WScreen/title.count, 2)];
view.backgroundColor = [UIColor redColor];
view.tag =200;
[self.scollerTop addSubview:view];
for (int i=0; i<title.count; i++) {
UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(i*WScreen, 0, WScreen,_scollerView.frame.size.height)];
if (i==0){
// [view1 addSubview:vc1.view];
view1.backgroundColor = [UIColor redColor];
}
else if(i==1){
// [view1 addSubview:vc2.view];
view1.backgroundColor = [UIColor yellowColor];
}else if (i==2){
// [view1 addSubview:vc3.view];
view1.backgroundColor = [UIColor grayColor];
}else if (i==3){
// [view1 addSubview:vc4.view];
view1.backgroundColor = [UIColor greenColor];
}else if (i==4){
// [view1 addSubview:vc5.view];
view1.backgroundColor = [UIColor blueColor];
}
[_scollerView addSubview:view1];
}
}
#pragma mark action 点击
-(void)actionBtn:(UIButton *)btn{
[UIView animateWithDuration:0.2 animations:^{
UIScrollView *view1 = (UIScrollView *)[self.view viewWithTag:200] ;
view1.frame = CGRectMake(WScreen/5*(btn.tag-100), 46, WScreen/5, 2);
for (int i=0; i<5; i++) {
UILabel *ScrollTopLabel1 = (UILabel *)[self.scollerTop viewWithTag:(i+300)];
ScrollTopLabel1.textColor = [UIColor blackColor];
}
for (int i=0; i<5; i++) {
UILabel *ScrollTopLabel1 = (UILabel *)[self.scollerTop viewWithTag:(i+400)];
ScrollTopLabel1.textColor = [UIColor blackColor];
}
UILabel *ScrollTopLabel1 = (UILabel *)[self.scollerTop viewWithTag:(btn.tag+200)];
ScrollTopLabel1.textColor = [UIColor redColor];
UILabel *ScrollTopLabel2 = (UILabel *)[self.scollerTop viewWithTag:(btn.tag+300)];
ScrollTopLabel2.textColor = [UIColor redColor];
}];
_scollerView.contentOffset = CGPointMake(WScreen *(btn.tag-100), 0);
NSLog(@"%lf",_scollerView.contentOffset.x/WScreen);
}
#pragma mark scrollV 的滑动事件
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
if (_scollerView ==scrollView) {
int xx = _scollerView.contentOffset.x/WScreen;
NSLog(@"++++++++>%lf",_scollerView.contentOffset.x/WScreen);
[UIView animateWithDuration:0.2 animations:^{
UIView *view1 = (UIView *)[self.scollerTop viewWithTag:200];
view1.frame = CGRectMake(WScreen/5*xx, 46, WScreen/5, 2);
for (int i=0; i<5; i++) {
UILabel *ScrollTopLabel1 = (UILabel *)[self.scollerTop viewWithTag:(i+300)];
ScrollTopLabel1.textColor = [UIColor blackColor];
}
for (int i=0; i<5; i++) {
UILabel *ScrollTopLabel1 = (UILabel *)[self.scollerTop viewWithTag:(i+400)];
ScrollTopLabel1.textColor = [UIColor blackColor];
}
UILabel *ScrollTopLabel1 = (UILabel *)[self.scollerTop viewWithTag:(xx+300)];
ScrollTopLabel1.textColor = [UIColor redColor];
UILabel *ScrollTopLabel2 = (UILabel *)[self.scollerTop viewWithTag:(xx+400)];
ScrollTopLabel2.textColor = [UIColor redColor];
}];
}
}
@end
效果图如下: