宽窄优行-由【嘉易行】项目成品而来
younger_times
2023-07-05 0d8f5fc8a516bfd07e425909e4a4432600572ee7
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
//
//  XHPageControl.m
//  XHPageControl
//
//  Created by xuanhe on 2019/1/2.
//  Copyright © 2019 xuanhe. All rights reserved.
//
 
#import "XHPageControl.h"
@interface XHPageControl ()<XHPageControlDelegate>
 
@end
@implementation XHPageControl
 
- (instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        [self initialize];
    }
    return self;
}
- (void)initialize{
    self.backgroundColor = [UIColor clearColor];
    _otherMultiple = 1;
    _currentMultiple = 2;
    _numberOfPages = 0;
    _currentPage = 0;
    _controlSize = 6;
    _controlSpacing = 8;
    _otherColor = [UIColor grayColor];
    _currentColor = [UIColor orangeColor];
    _type = PageControlMiddle;
}
 
- (void)setOtherColor:(UIColor *)otherColor{
    if(![self isTheSameColor:otherColor anotherColor:_otherColor]){
        _otherColor = otherColor;
        [self createPointView];
    }
}
 
- (void)setCurrentColor:(UIColor *)currentColor{
    if(![self isTheSameColor:currentColor anotherColor:_currentColor]){
        _currentColor = currentColor;
        [self createPointView];
    }
}
 
- (void)setControlSize:(NSInteger)controlSize{
    if(_controlSize != controlSize){
        _controlSize = controlSize;
        [self createPointView];
    }
}
- (void)setOtherMultiple:(NSInteger)otherMultiple {
    if (otherMultiple != _otherMultiple) {
        _otherMultiple = otherMultiple;
        [self createPointView];
    }
}
- (void)setCurrentMultiple:(NSInteger)currentMultiple {
    if (currentMultiple != _currentMultiple) {
        _currentMultiple = currentMultiple;
        [self createPointView];
    }
}
 
- (void)setControlSpacing:(NSInteger)controlSpacing {
    if(_controlSpacing != controlSpacing){
        _controlSpacing = controlSpacing;
        [self createPointView];
    }
}
 
- (void)setCurrentBkImg:(UIImage *)currentBkImg {
    if(_currentBkImg != currentBkImg){
        _currentBkImg = currentBkImg;
        [self createPointView];
    }
}
 
- (void)setOtherBkImg:(UIImage *)otherBkImg {
    if (_otherBkImg != otherBkImg) {
        _otherBkImg = otherBkImg;
        [self createPointView];
    }
}
 
- (void)setType:(XHPageControlType)type {
    if (_type != type) {
        _type = type;
        [self createPointView];
    }
}
 
- (void)setNumberOfPages:(NSInteger)page {
    if(_numberOfPages == page){
        return;
    }
    _numberOfPages = page;
    [self createPointView];
}
 
- (void)setCurrentPage:(NSInteger)currentPage {
    
    if([self.delegate respondsToSelector:@selector(xh_PageControlClick:index:)]) {
        [self.delegate xh_PageControlClick:self index:currentPage];
    }
    
    if(_currentPage == currentPage){
        return;
    }
    
    [self exchangeCurrentView:_currentPage new:currentPage];
    _currentPage = currentPage;
}
 
- (void)clearView {
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
}
 
- (void)createPointView {
    [self clearView];
    if(_numberOfPages <= 0)
        return;
    
    //居中控件
    CGFloat startX = 0;
    CGFloat startY = 0;
    CGFloat mainWidth = (_numberOfPages - 1)* _controlSize*_otherMultiple+ (_numberOfPages - 1) * _controlSpacing + _controlSize * _currentMultiple;
    if(self.frame.size.width < mainWidth){
        startX = 0;
    }else{
        if (_type == PageControlLeft) {
            startX = 10;
        }else if (_type == PageControlMiddle){
            startX = (self.frame.size.width - mainWidth)/2.0;
        }else if (_type == PageControlRight){
            startX = self.frame.size.width - mainWidth - 10;
        }
    }
    if(self.frame.size.height < _controlSize){
        startY = 0;
    }else{
        startY = (self.frame.size.height-_controlSize)/2;
    }
    //动态创建点
    for (int page = 0; page < _numberOfPages; page++) {
        if(page == _currentPage){
            UIView *currPointView = [[UIView alloc]initWithFrame:CGRectMake(startX, startY, _controlSize*_currentMultiple, _controlSize)];
            currPointView.layer.cornerRadius = _controlSize/2;
            currPointView.tag = page+1000;
            currPointView.backgroundColor = _currentColor;
            currPointView.userInteractionEnabled = YES;
            UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickAction:)];
            [currPointView addGestureRecognizer:tapGesture];
            [self addSubview:currPointView];
            startX = CGRectGetMaxX(currPointView.frame)+_controlSpacing;
            
            if(_currentBkImg){
                currPointView.backgroundColor = [UIColor clearColor];
                UIImageView *currBkImg = [[UIImageView alloc]init];
                currBkImg.tag = 2233;
                currBkImg.frame = CGRectMake(0, 0, currPointView.frame.size.width, currPointView.frame.size.height);
                currBkImg.image = _currentBkImg;
                [currPointView addSubview:currBkImg];
            }
            
        }else{
            UIView *otherPointView = [[UIView alloc]initWithFrame:CGRectMake(startX, startY, _controlSize * _otherMultiple, _controlSize)];
            otherPointView.backgroundColor = _otherColor;
            otherPointView.tag = page+1000;
            otherPointView.layer.cornerRadius = _controlSize/2.0;
            otherPointView.userInteractionEnabled = YES;
            
            if (_otherBkImg) {
                otherPointView.backgroundColor = [UIColor clearColor];
                UIImageView *otherBkImg = [[UIImageView alloc] init];
                otherBkImg.tag = page+1000+2244;
                otherBkImg.frame = CGRectMake(0, 0, otherPointView.frame.size.width, otherPointView.frame.size.height);
                otherBkImg.image = _otherBkImg;
                [otherPointView addSubview:otherBkImg];
            }
            
            UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickAction:)];
            [otherPointView addGestureRecognizer:tapGesture];
            [self addSubview:otherPointView];
            startX = CGRectGetMaxX(otherPointView.frame)+_controlSpacing;
        }
    }
}
 
//切换当前的点
- (void)exchangeCurrentView:(NSInteger)old new:(NSInteger)new {
    UIView *oldSelect = [self viewWithTag:1000+old];
    CGRect mpSelect = oldSelect.frame;
    
    UIView *newSeltect = [self viewWithTag:1000+new];
    CGRect newTemp = newSeltect.frame;
    
    if(_currentBkImg){
        UIView *imgview = [oldSelect viewWithTag:2233];
        [imgview removeFromSuperview];
        
        newSeltect.backgroundColor=[UIColor clearColor];
        UIImageView *currBkImg = [UIImageView new];
        currBkImg.tag = 2233;
        currBkImg.frame = CGRectMake(0, 0, mpSelect.size.width, mpSelect.size.height);
        currBkImg.image = _currentBkImg;
        [newSeltect addSubview:currBkImg];
    }
    if (_otherBkImg) {
        UIView *imgview=[newSeltect viewWithTag:2244+1000+new];
        [imgview removeFromSuperview];
        
        oldSelect.backgroundColor = [UIColor clearColor];
        UIImageView *otherBkImg = [UIImageView new];
        otherBkImg.tag = 2244+1000+new;
        otherBkImg.frame = CGRectMake(0, 0, mpSelect.size.width, mpSelect.size.height);
        otherBkImg.image = _otherBkImg;
        [oldSelect addSubview:otherBkImg];
    }
    oldSelect.backgroundColor = _otherColor;
    newSeltect.backgroundColor = _currentColor;
    
    [UIView animateWithDuration:0.3 animations:^{
        CGFloat lx=mpSelect.origin.x;
        if (new<old)
            lx+=self->_controlSize *(self->_currentMultiple - self->_otherMultiple);
        oldSelect.frame = CGRectMake(lx, mpSelect.origin.y, self->_controlSize * self->_otherMultiple, self->_controlSize);
        
        CGFloat mx = newTemp.origin.x;
        if (new>old)
            mx -= self->_controlSize * (self->_currentMultiple - self->_otherMultiple);
        newSeltect.frame = CGRectMake(mx, newTemp.origin.y, self->_controlSize * self->_currentMultiple, self->_controlSize);
        
        // 左边的时候到右边 越过点击
        if(new-old>1) {
            for(NSInteger t = old+1;t < new; t++) {
                UIView *ms = [self viewWithTag:1000+t];
                ms.frame=CGRectMake(ms.frame.origin.x-self->_controlSize * (self->_currentMultiple - self->_otherMultiple), ms.frame.origin.y, self->_controlSize*self->_otherMultiple, self->_controlSize);
            }
        }
        // 右边选中到左边的时候 越过点击
        if(new-old<-1) {
            for(NSInteger t = new+1; t < old; t++) {
                UIView *ms = [self viewWithTag:1000+t];
                ms.frame = CGRectMake(ms.frame.origin.x+self->_controlSize * (self->_currentMultiple - self->_otherMultiple), ms.frame.origin.y, self->_controlSize*self->_otherMultiple, self->_controlSize);
            }
        }
    }];
}
 
- (void)clickAction:(UITapGestureRecognizer*)recognizer{
    
    NSInteger index = recognizer.view.tag-1000;
    NSLog(@"-----:%ld",(long)index);
    [self setCurrentPage:index];
}
 
- (BOOL)isTheSameColor:(UIColor*)color1 anotherColor:(UIColor*)color2{
    return CGColorEqualToColor(color1.CGColor, color2.CGColor);
}
 
 
@end