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
//
// HomeHeaderView.m
// redstar
//
// Created by admin on 15/10/26.
// Copyright © 2015年 ZWF. All rights reserved.
//
#import "HomeHeaderView.h"
#define ImageCount 5
@interface HomeHeaderView () <UIScrollViewDelegate>
@property (nonatomic, strong) UIView *bgView; // 背景View
@end
@implementation HomeHeaderView
#pragma mark - init
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = kMineBackGroundColor;
[self setup];
}
return self;
}
#pragma mark - Private Methods
- (void)setup
{
[self.customPageControl setNumberOfPages:2];
self.bgView.backgroundColor = [UIColor whiteColor];
}
#pragma mark - UIScrollView Delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat pageWidth = _scrollView.bounds.size.width;
float fractionalPage = _scrollView.contentOffset.x / pageWidth;
NSInteger nearestNumber = lround(fractionalPage);
if (_customPageControl.currentPage != nearestNumber) {
_customPageControl.currentPage = nearestNumber;
// 当scrollView在滑动
if (_scrollView.dragging) {
// 更新选中的小点点的位置
[_customPageControl updateCurrentPageDisplay];
}
}
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
[_customPageControl updateCurrentPageDisplay] ;
}
- (UIView *)bgView
{
if (!_bgView) {
_bgView = [[UIView alloc] init];
_bgView.translatesAutoresizingMaskIntoConstraints = NO;
_bgView.layer.borderWidth = 0.5;
_bgView.layer.borderColor = kSeparateLineCGColor;
[self addSubview:_bgView] ;
NSLayoutConstraint *bgImageTop = [NSLayoutConstraint constraintWithItem:_bgView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
[self addConstraint:bgImageTop];
NSLayoutConstraint *bgImageRight = [NSLayoutConstraint constraintWithItem:_bgView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1.0 constant:0];
[self addConstraint:bgImageRight];
NSLayoutConstraint *bgImageBottom = [NSLayoutConstraint constraintWithItem:_bgView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-10];
[self addConstraint:bgImageBottom];
NSLayoutConstraint *bgImageLeft = [NSLayoutConstraint constraintWithItem:_bgView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0];
[self addConstraint:bgImageLeft];
}
return _bgView;
}
- (UIScrollView *)scrollView
{
if (!_scrollView) {
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 86)];
_scrollView.bounces = NO;
_scrollView.backgroundColor = [UIColor whiteColor];
_scrollView.pagingEnabled = YES;
_scrollView.delegate = self;
_scrollView.showsHorizontalScrollIndicator = NO;
_scrollView.showsVerticalScrollIndicator = NO;
[self addSubview:_scrollView];
}
return _scrollView;
}
- (CustomPageControl *)customPageControl
{
if (!_customPageControl) {
_customPageControl = [[CustomPageControl alloc] init];
_customPageControl.translatesAutoresizingMaskIntoConstraints = NO;
_customPageControl.backgroundColor = [UIColor clearColor];
[_customPageControl setCenter: CGPointMake(self.bgView.center.x, self.bgView.bounds.size.height-7.5f)] ;
[_customPageControl setCurrentPage:0] ;
[_customPageControl setDefersCurrentPageDisplay: YES] ;
[_customPageControl setOnColor:kLineColor] ;
[_customPageControl setOffColor:kLineColor] ;
[_customPageControl setIndicatorDiameter:7.5f] ;
[_customPageControl setIndicatorSpace:8.0f] ;
[self.bgView addSubview:_customPageControl];
NSLayoutConstraint *bgImageHeight = [NSLayoutConstraint constraintWithItem:_customPageControl attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:10];
[self.bgView addConstraint:bgImageHeight];
NSLayoutConstraint *bgImageRight = [NSLayoutConstraint constraintWithItem:_customPageControl attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.bgView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0];
[self.bgView addConstraint:bgImageRight];
NSLayoutConstraint *bgImageBottom = [NSLayoutConstraint constraintWithItem:_customPageControl attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.bgView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-5];
[self.bgView addConstraint:bgImageBottom];
NSLayoutConstraint *bgImageLeft = [NSLayoutConstraint constraintWithItem:_customPageControl attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.bgView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0];
[self.bgView addConstraint:bgImageLeft];
}
return _customPageControl;
}
@end