HomeHeaderView.m 5.3 KB
Newer Older
admin's avatar
admin committed
1 2 3 4 5 6 7 8 9
//
//  HomeHeaderView.m
//  redstar
//
//  Created by admin on 15/10/26.
//  Copyright © 2015年 ZWF. All rights reserved.
//

#import "HomeHeaderView.h"
10
#define ImageCount 5
admin's avatar
admin committed
11 12

@interface HomeHeaderView () <UIScrollViewDelegate>
admin's avatar
admin committed
13
@property (nonatomic, strong) UIView *bgView; // 背景View
admin's avatar
admin committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
@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
{
admin's avatar
admin committed
33 34
    [self.customPageControl setNumberOfPages:2];
    self.bgView.backgroundColor = [UIColor whiteColor];
admin's avatar
admin committed
35 36 37

}

38

admin's avatar
admin committed
39 40 41 42 43
#pragma mark - UIScrollView Delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat pageWidth = _scrollView.bounds.size.width;
    float fractionalPage = _scrollView.contentOffset.x / pageWidth;
    NSInteger nearestNumber = lround(fractionalPage);
admin's avatar
admin committed
44
    
admin's avatar
admin committed
45 46 47 48 49 50 51 52
    if (_customPageControl.currentPage != nearestNumber) {
        _customPageControl.currentPage = nearestNumber;
        // 当scrollView在滑动
        if (_scrollView.dragging) {
            // 更新选中的小点点的位置
            [_customPageControl updateCurrentPageDisplay];
        }
    }
admin's avatar
admin committed
53 54
}

admin's avatar
admin committed
55 56
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    [_customPageControl updateCurrentPageDisplay] ;
admin's avatar
admin committed
57 58 59
}


admin's avatar
admin committed
60
- (UIView *)bgView
admin's avatar
admin committed
61
{
admin's avatar
admin committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    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;
admin's avatar
admin committed
83 84 85 86 87
}

- (UIScrollView *)scrollView
{
    if (!_scrollView) {
admin's avatar
admin committed
88
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 86)];
admin's avatar
admin committed
89
        _scrollView.bounces = NO;
admin's avatar
admin committed
90
        _scrollView.backgroundColor = [UIColor whiteColor];
admin's avatar
admin committed
91
        _scrollView.pagingEnabled = YES;
92
        _scrollView.delegate = self;
admin's avatar
admin committed
93 94 95 96 97 98 99
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.showsVerticalScrollIndicator = NO;
        [self addSubview:_scrollView];
    }
    return _scrollView;
}

admin's avatar
admin committed
100
- (CustomPageControl *)customPageControl
admin's avatar
admin committed
101
{
admin's avatar
admin committed
102 103 104 105 106 107 108 109 110 111 112 113
    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];
admin's avatar
admin committed
114
        
admin's avatar
admin committed
115 116
        NSLayoutConstraint *bgImageHeight = [NSLayoutConstraint constraintWithItem:_customPageControl attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:10];
        [self.bgView addConstraint:bgImageHeight];
admin's avatar
admin committed
117
        
admin's avatar
admin committed
118 119 120 121 122 123 124 125
        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];
admin's avatar
admin committed
126 127
        
    }
admin's avatar
admin committed
128 129
    
    return _customPageControl;
admin's avatar
admin committed
130
}
admin's avatar
admin committed
131

admin's avatar
admin committed
132
@end