forked from coding/Coding-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSHotTopicPagesVC.m
More file actions
183 lines (137 loc) · 5.79 KB
/
CSHotTopicPagesVC.m
File metadata and controls
183 lines (137 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//
// CSHotTopicPagesVC.m
// Coding_iOS
//
// Created by Lambda on 15/8/5.
// Copyright (c) 2015年 Coding. All rights reserved.
//
#import "CSHotTopicPagesVC.h"
#import "CSHotTopicView.h"
#import "CSMyTopicVC.h"
#import "SMPageControl.h"
@interface CSHotTopicPagesVC ()<UIScrollViewDelegate>
@property (nonatomic,strong) UIScrollView *scrollView;
@property (nonatomic,strong) SMPageControl *pageControl;
@property (nonatomic,strong) UIView *navigationBarView;
@property (nonatomic,strong)NSArray *navTitles;
@end
@implementation CSHotTopicPagesVC
- (void)viewDidLoad {
[super viewDidLoad];
[self setupUI];
}
- (void)onGoBack {
[self.navigationController popViewControllerAnimated:YES];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController.navigationBar addSubview:self.navigationBarView];
}
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[self.navigationBarView removeFromSuperview];
}
#pragma mark -
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
[self updateNavItems:scrollView.contentOffset.x];
float mid = [UIScreen mainScreen].bounds.size.width/2 - 45.0;
float width = [UIScreen mainScreen].bounds.size.width;
CGFloat xOffset = scrollView.contentOffset.x;
int i = 0;
for(UILabel *v in _navTitles){
CGFloat alpha = 0.0;
if(v.frame.origin.x < mid)
alpha = 1 - (xOffset - i*width) / width;
else if(v.frame.origin.x >mid)
alpha=(xOffset - i*width) / width + 1;
else if(v.frame.origin.x == mid-5)
alpha = 1.0;
i++;
v.alpha = alpha;
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
[self sendNewIndex:scrollView];
}
-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
[self sendNewIndex:scrollView];
}
-(void)sendNewIndex:(UIScrollView *)scrollView{
CGFloat xOffset = scrollView.contentOffset.x;
NSInteger index = ((int) roundf(xOffset) % (2 * (int)kScreen_Width)) / kScreen_Width;
if (self.pageControl.currentPage != index)
{
self.pageControl.currentPage = index;
}
}
#pragma mark -
-(void)updateNavItems:(CGFloat) xOffset{
__block int i = 0;
[_navTitles enumerateObjectsUsingBlock:^(UIView* v, NSUInteger idx, BOOL *stop) {
CGFloat distance = (kScreen_Width/2) - 60;
CGSize vSize = CGSizeMake(100, 16);
CGFloat originX = ((kScreen_Width/2 - vSize.width/2) + i*distance) - xOffset/(kScreen_Width/distance);
v.frame = (CGRect){originX, 8, vSize.width, vSize.height};
i++;
}];
}
- (void)setupUI {
self.navigationItem.title = @"";
CGRect frame = CGRectMake(0, 0, kScreen_Width, self.view.bounds.size.height);
self.scrollView = [[UIScrollView alloc] initWithFrame:frame];
self.scrollView.backgroundColor = [UIColor clearColor];
self.scrollView.pagingEnabled = YES;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.showsVerticalScrollIndicator = NO;
self.scrollView.delegate = self;
self.scrollView.bounces = NO;
[self.scrollView setContentInset:UIEdgeInsetsMake(0, 0, 0, 0)];
[self.view addSubview:self.scrollView];
[self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
}];
self.scrollView .clipsToBounds = YES;
self.scrollView.contentSize = CGSizeMake(kScreen_Width * 2, 0);
_navigationBarView = [[UIView alloc] initWithFrame:(CGRect){0, 0, kScreen_Width, 44}];
_navigationBarView.backgroundColor = [UIColor clearColor];
// Make the page control
self.pageControl = ({
SMPageControl *pgC = [[SMPageControl alloc] init];
pgC.userInteractionEnabled = NO;
pgC.backgroundColor = [UIColor clearColor];
pgC.pageIndicatorImage = [UIImage imageNamed:@"nav_page_unselected"];
pgC.currentPageIndicatorImage = [UIImage imageNamed:@"nav_page_selected"];
pgC.frame = CGRectMake(0, 32.0, kScreen_Width, 7.0);
pgC.numberOfPages = 2;
pgC.currentPage = 0;
pgC;
});
_navigationBarView.userInteractionEnabled = NO;
[_navigationBarView addSubview:self.pageControl];
// Adds all views
CSHotTopicView *v1 = [[CSHotTopicView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, self.view.height - 50)];
v1.backgroundColor = [UIColor whiteColor];
CSMyTopicView *v2 = [[CSMyTopicView alloc]initWithFrame:CGRectMake(kScreen_Width+1, 0, kScreen_Width, self.view.height - 50)];
v2.backgroundColor = [UIColor whiteColor];
v1.parentVC = self;
v2.parentVC = self;
[self.scrollView addSubview:v1];
[self.scrollView addSubview:v2];
CGFloat titleWidth = 100;
CGFloat distance = (kScreen_Width/2) - 60;
UILabel *la1 = [[UILabel alloc] initWithFrame:CGRectMake((kScreen_Width/2 - titleWidth/2) + 0*distance, 8, titleWidth, 16)];
la1.text = @"热门话题";
la1.textAlignment = NSTextAlignmentCenter;
la1.font = [UIFont systemFontOfSize:16];
la1.textColor = [UIColor whiteColor];
UILabel *la2 = [[UILabel alloc] initWithFrame:CGRectMake((kScreen_Width/2 - titleWidth/2) + 1*distance, 8, titleWidth, 16)];
la2.text = @"我的话题";
la2.textAlignment = NSTextAlignmentCenter;
la2.font = [UIFont systemFontOfSize:16];
la2.textColor = [UIColor whiteColor];
la2.alpha = 0;
[_navigationBarView addSubview:la1];
[_navigationBarView addSubview:la2];
_navTitles = @[la1,la2];
}
@end