小圆点:UIPageControl

2021-12-27  本文已影响0人  阳光下的叶子呵
#import "HomeGuidePageView.h"
#import "HomeGuidePageViewCell.h"

@interface HomeGuidePageView ()<UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UIPageControl *pageControl;

@property (nonatomic, strong) UIButton *startBut; // 立即体验
@property (nonatomic, strong) UIButton *skipBut; // 跳过

@property (nonatomic, strong) NSArray *iconArr;

@end

@implementation HomeGuidePageView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createArray];
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    
    UIEdgeInsets insets = UIEdgeInsetsMake(20, 0, 0, 0);
    if (@available(iOS 11.0, *)) {
        insets = [UIApplication sharedApplication].keyWindow.safeAreaInsets;
    }
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.pagingEnabled = YES;
    collectionView.scrollEnabled = YES;
    collectionView.showsHorizontalScrollIndicator = NO;
    [self addSubview:collectionView];
    [collectionView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    [collectionView registerClass:[HomeGuidePageViewCell class] forCellWithReuseIdentifier:NSStringFromClass([HomeGuidePageViewCell class])];
    self.collectionView = collectionView;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    _pageControl = [[UIPageControl alloc] init];
    _pageControl.numberOfPages = self.iconArr.count; // 指定页面个数
    _pageControl.currentPage = 0; // 指定pagecontroll的值,默认选中的小白点(第一个)
    _pageControl.pageIndicatorTintColor = RGBA(212, 212, 212, 1);// 设置非选中页的圆点颜色
    _pageControl.currentPageIndicatorTintColor = RGBA(25, 170, 167, 1); // 设置选中页的圆点颜色
    [self addSubview:self.pageControl];
    [self.pageControl makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.centerY.mas_equalTo(self.mas_bottom).offset(-[UIScreen mainScreen].bounds.size.width/375*50);
        make.width.mas_equalTo([UIScreen mainScreen].bounds.size.width/375*180);
        make.height.mas_equalTo([UIScreen mainScreen].bounds.size.width/375*20);
    }];
//    [self.pageControl addTarget:self action:@selector(clickSelectPage:) forControlEvents:UIControlEventValueChanged];
    
    _startBut = [UIButton buttonWithType:UIButtonTypeCustom];
    [_startBut setBackgroundImage:[UIImage imageNamed:@"homeGuidePageView5_icon"] forState:UIControlStateNormal];
    [self.startBut addTarget:self action:@selector(clickSkipAction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.startBut];
    [self.startBut makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.centerY.mas_equalTo(self.mas_bottom).offset(-[UIScreen mainScreen].bounds.size.width/375*50);
        make.width.mas_equalTo([UIScreen mainScreen].bounds.size.width/375*114);
        make.height.mas_equalTo([UIScreen mainScreen].bounds.size.width/375*35);
    }];
    self.startBut.hidden = YES;
    
    _skipBut = [UIButton buttonWithType:UIButtonTypeCustom];
    [_skipBut setBackgroundImage:[UIImage imageNamed:@"homeGuidePageView6_icon"] forState:UIControlStateNormal];
    [self.skipBut addTarget:self action:@selector(clickSkipAction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.skipBut];
    [self.skipBut makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mas_top).offset([UIScreen mainScreen].bounds.size.width/375*20+insets.top);
        make.right.mas_equalTo(self.mas_right).offset(-[UIScreen mainScreen].bounds.size.width/375*20);
        make.width.mas_equalTo([UIScreen mainScreen].bounds.size.width/375*74);
        make.height.mas_equalTo([UIScreen mainScreen].bounds.size.width/375*35);
    }];
    
}

- (void)createArray {
    
    self.iconArr = @[
        @{@"icon_img":@"homeGuidePageView1_icon", @"num":@"1"},
        @{@"icon_img":@"homeGuidePageView2_icon", @"num":@"2"},
        @{@"icon_img":@"homeGuidePageView3_icon", @"num":@"3"},
        @{@"icon_img":@"homeGuidePageView4_icon", @"num":@"4"}
    ];
}

- (void)clickSelectPage:(UIPageControl *)pageControl {
    
//    pageControl.currentPage
    
}

- (void)clickSkipAction {
    [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"homeGuidePage"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    [self removeFromSuperview];
    if (self.removeGuidePageViewBlock) {
        self.removeGuidePageViewBlock();
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

//    NSLog(@"开始滑动 scrollView.contentOffset.x = %.2f ", scrollView.contentOffset.x);

     // 整个引导页先弄一个UIViewController,在VC中添加其他需要的东西,比如一个UICollectionView、“跳过”按钮,和翻页控件UIpageControl
    // 可以设置刚开始进入APP时的引导图,可以使用UICollectionView,再在上面添加“跳过”按钮
    // 原理:当scrollView.contentOffset.x < 0时,禁止向右滑动,同理,滑到最后一个item,也设置禁止在向左滑动
    if (scrollView.contentOffset.x < 0 || scrollView.contentOffset.x > [UIScreen mainScreen].bounds.size.width*3) {
        self.collectionView.scrollEnabled = NO;
    } else {
        self.collectionView.scrollEnabled = YES;
    }
    
//    if (scrollView.contentOffset.x > [UIScreen mainScreen].bounds.size.width*4) {
//        self.collectionView.scrollEnabled = NO;
//    } else {
//        self.collectionView.scrollEnabled = YES;
//    }
    
    if (scrollView.contentOffset.x >= [UIScreen mainScreen].bounds.size.width*3) {
        self.pageControl.hidden = YES;
        self.startBut.hidden = NO;
        self.skipBut.hidden = YES;
    } else {
        self.pageControl.hidden = NO;
        self.startBut.hidden = YES;
        self.skipBut.hidden = NO;
    }
    
    if (scrollView.contentOffset.x < [UIScreen mainScreen].bounds.size.width*1) {
        self.pageControl.currentPage = 0;
    } else if (scrollView.contentOffset.x >= [UIScreen mainScreen].bounds.size.width*1 && scrollView.contentOffset.x < [UIScreen mainScreen].bounds.size.width*2) {
        self.pageControl.currentPage = 1;
    } else if (scrollView.contentOffset.x >= [UIScreen mainScreen].bounds.size.width*2 && scrollView.contentOffset.x < [UIScreen mainScreen].bounds.size.width*3) {
        self.pageControl.currentPage = 2;
    } else if (scrollView.contentOffset.x >= [UIScreen mainScreen].bounds.size.width*3 && scrollView.contentOffset.x < [UIScreen mainScreen].bounds.size.width*4) {
        self.pageControl.currentPage = 3;
    }
    
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.iconArr.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
   // item 大小
   return CGSizeMake(self.frame.size.width, self.frame.size.height);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
   // 行间距(上下)
//    return 40;
   return [UIScreen mainScreen].bounds.size.width/375*0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
   // 列间距(左右)
   return [UIScreen mainScreen].bounds.size.width/375*0;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
   
    return UIEdgeInsetsMake(0, 0, 0, 0);
   
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
   
    HomeGuidePageViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([HomeGuidePageViewCell class]) forIndexPath:indexPath];
   if (self.iconArr.count > 0) {
           
           cell.iconDic = self.iconArr[indexPath.row];
   }
       
   return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
   
}
上一篇下一篇

猜你喜欢

热点阅读