GLD_SegmentBinder.m
5.51 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
//
// GLD_SegmentBinder.m
// YX_BaseProject
//
// Created by yiyangkeji on 2018/4/13.
// Copyright © 2018年 com.yxvzb. All rights reserved.
//
#import "GLD_SegmentBinder.h"
#define screenWidth CGRectGetWidth([UIScreen mainScreen].bounds)
@interface GLD_SegmentBinder ()<UIScrollViewDelegate>
//@property (nonatomic, weak)UIView *selecteItemView;
@property (nonatomic, weak)id<GLD_SegmentViewDataSource> dataSourceDelegate;
@property (nonatomic, weak)id<GLD_SegmentViewDelegate> delegate;
@property (nonatomic, weak)UIButton *selectBut;
@property (nonatomic, assign)CGFloat titleHeight;
@property (nonatomic, assign)NSInteger itemCount;
@end
static const NSInteger initialTag = 101;
@implementation GLD_SegmentBinder
- (UIView *)selecteItemView{
if (!self.selectBut) return nil;
return [self itemViewAtIndexInSegmentView:(self.selectBut.tag - initialTag)];
}
- (NSInteger)selecteIndex{
if (!self.selectBut) return 0;
return self.selectBut.tag - initialTag;
}
//相当于点击标题
- (void)setSelecteIndex:(NSInteger)selecteIndex{
if (selecteIndex < 0) return;
[self didSelectedTitle:[self.titleView viewWithTag:selecteIndex + initialTag]];
}
//点击按钮
- (void)didSelectedTitle:(UIButton *)senser{
if (![senser isEqual:self.selectBut]) {
if ([self.delegate respondsToSelector:@selector(SegmentView:didScrollToItemAtIndex:)]) {
[self.delegate SegmentView:self didScrollToItemAtIndex:senser.tag - initialTag];
}
self.selectBut.selected = NO;
self.selectBut = senser;
self.selectBut.selected = YES;
[self.contentView setContentOffset:CGPointMake((senser.tag - initialTag) * screenWidth, 0) animated:YES];
}
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat offsetX = scrollView.contentOffset.x * MIN(self.itemCount, 5);
self.indicatorView.mj_x = offsetX;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
NSUInteger index = scrollView.contentOffset.x / CGRectGetWidth([UIScreen mainScreen].bounds) + initialTag;
[self didSelectedTitle:[self.titleView viewWithTag:index]];
}
- (void)setDataSourceDelegate:(id<GLD_SegmentViewDataSource>)dataSourceDelegate{
self.dataSourceDelegate = dataSourceDelegate;
//设置数据后布局
[self layoutContentView];
//
[self setSelecteIndex:self.selecteIndex];
}
- (void)layoutContentView{
//赋值
self.itemCount = [self itemCountForSegmentView];
self.titleHeight = [self headerHeight];
self.contentView.delegate = self;
self.contentView.contentSize = CGSizeMake(screenWidth * self.itemCount, 0);
for (int i = 0; i < self.itemCount; i++) {
[self addtitleButAtIndex:i];
[self addContentViewAtIndex:i];
}
}
- (void)addtitleButAtIndex:(NSInteger)index{
UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
NSString *title = [self titleForHeaderAtIndex:index];
NSAttributedString *nomalStr = [[NSAttributedString alloc]initWithString:title attributes:[self attributesAtIndex:index andIsSelect:NO]];
NSAttributedString *selecteStr = [[NSAttributedString alloc]initWithString:title attributes:[self attributesAtIndex:index andIsSelect:YES]];
[but setAttributedTitle:nomalStr forState:UIControlStateNormal];
[but setAttributedTitle:selecteStr forState:UIControlStateSelected];
CGFloat width = screenWidth / MIN(self.itemCount, 5);
but.tag = 0 + initialTag;
but.frame = CGRectMake(width * index, 0, width, self.titleHeight - 3);
[self.titleView addSubview:but];
self.indicatorView.frame = CGRectMake(0, self.titleHeight - 3, width, 2);
self.titleView.contentSize = CGSizeMake(width * self.itemCount, 0);
[self.titleView mas_updateConstraints:^(MASConstraintMaker *make) {
make.height.equalTo(@(self.titleHeight));
}];
}
- (void)addContentViewAtIndex:(NSInteger)index{
UIView *contentView = [self itemViewAtIndexInSegmentView:index];
contentView.frame = CGRectMake(index * screenWidth, self.titleHeight, screenWidth, CGRectGetHeight(self.bounds) - self.titleHeight);
[self.contentView addSubview:contentView];
}
- (CGFloat)headerHeight{
if ([self.dataSourceDelegate respondsToSelector:@selector(heightOfheaderForSegmentView:)]) {
return [self.dataSourceDelegate heightOfheaderForSegmentView:self];
}
return 0;
}
- (NSInteger)itemCountForSegmentView{
if ([self.dataSourceDelegate respondsToSelector:@selector(numbersOfItemInSegmentView:)]) {
return [self.dataSourceDelegate numbersOfItemInSegmentView:self];
}
return 0;
}
- (UIView *)itemViewAtIndexInSegmentView:(NSInteger)index{
if ([self.dataSourceDelegate respondsToSelector:@selector(segmentView:itemViewAtIndex:)]) {
return [self.dataSourceDelegate segmentView:self itemViewAtIndex:index];
}
return [UIView new];
}
- (NSString *)titleForHeaderAtIndex:(NSInteger)index{
if ([self.dataSourceDelegate respondsToSelector:@selector(segmentView:titleForItemAtIndex:)]) {
return [self.dataSourceDelegate segmentView:self titleForItemAtIndex:index];
}
return @"";
}
- (NSDictionary *)attributesAtIndex:(NSInteger)index andIsSelect:(BOOL)select{
if ([self.dataSourceDelegate respondsToSelector:@selector(segmentView:attributesForTitleAtIndex:isSelected:)]) {
return [self.dataSourceDelegate segmentView:self attributesForTitleAtIndex:index isSelected:select];
}
return @{};
}
@end