学习了下引导页的实现方法,如果设计到了版权或者抄袭问题请联系我删除,本文只作为我学习记录一下。好了让我们直接上效果图:
屏幕录制2020-06-17午2.54.25.gif
实现这个效果是通过UICollectionView,将每个图片放到Cell中实现的下面让我们用代码来说话吧:
ZYGuidePageViewController.h
文件
/**采用的Controller中添加CollectionView的列表对象来实现引导滚动*/
#import <UIKit/UIKit.h>
@interface ZYGuidePageViewController : UIViewController
/**
构造自定义引导页面
*imgArray 引导的图片
*showButton 跳过按钮
*showPageCount 页导航条
*sColor 选中的颜色
*unColor 为选中的要色
*/
- (instancetype)initPageWithImageArry:(NSArray *)imgArray
isShowSkipButton:(BOOL)showButton
isShowPageCount:(BOOL)showPageCount
selColor:(UIColor *)sColor
unSelColor:(UIColor *)unColor;
@end
ZYGuidePageViewController.m
文件
#import "ZYGuidePageViewController.h"
#import "ZYGuideCollectionViewCell.h"
#import "ViewController.h"
@interface ZYGuidePageViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
//启动页图片
@property (nonatomic, copy) NSArray * imageArray;
//collecetion
@property (nonatomic, strong) UICollectionView *zy_collectionView;
//是否现实跳过按钮
@property (nonatomic, assign) BOOL isShowSkipButton;
//是否现实pageControl
@property (nonatomic, assign) BOOL isShowPageView;
//pageControl
@property (nonatomic, strong) UIPageControl * zy_pageControl;
/* 小圆点选中颜色 */
@property (nonatomic, strong) UIColor *selColor;
/* 小圆点未选中颜色 */
@property (nonatomic, strong) UIColor *unselColor;
/* 跳过按钮 */
@property (nonatomic, strong) UIButton *skipButton;
@end
@implementation ZYGuidePageViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self zy_collectionView];
self.zy_pageControl.hidden = !_isShowPageView;
[self.skipButton setTitle:@"跳过" forState:0];
self.skipButton.hidden = !_isShowSkipButton;
if (@available(iOS 13.0, *)) {
self.automaticallyAdjustsScrollViewInsets = NO;
}
}
- (instancetype)initPageWithImageArry:(NSArray *)imgArray isShowSkipButton:(BOOL)showButton isShowPageCount:(BOOL)showPageCount selColor:(UIColor *)sColor unSelColor:(UIColor *)unColor{
self = [super self];
if(self){
self.imageArray = imgArray;
self.isShowSkipButton = showButton;
self.isShowPageView = showPageCount;
self.selColor = sColor;
self.unselColor = unColor;
}
return self;
}
#pragma mark - <UICollectionViewDataSource>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return _imageArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
ZYGuideCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ZYGuideCellID forIndexPath:indexPath];
NSString * imgName = IS_IPHONE_X ? [NSString stringWithFormat:@"%@_x",_imageArray[indexPath.row]] : _imageArray[indexPath.row];
[cell initCellwithImageNameString:imgName];
return cell;
}
#pragma mark - <UICollectionViewDelegateFlowLayout>
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(YSCREEN_WIDTH, YSCREEN_HEIGHT);
}
///当滑动到最后一张图片时再左滑动就切换视图控制器
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
// if(_imageArray.count < 2) return;
// if (scrollView.contentOffset.x > (_imageArray.count - 1) * ScreenW) {
// [self restoreRootViewController:[[DCTabBarController alloc] init]];
// }
}
///判断当前滑动到第几页,圆角就选中第几个
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
if(!_isShowPageView)return;
CGPoint rect = scrollView.contentOffset;
NSInteger page = rect.x/scrollView.frame.size.width;
_zy_pageControl.currentPage = page;
}
///点击跳过按钮
- (void)skipButtonClick{
}
- (UICollectionView *)zy_collectionView{
if(!_zy_collectionView){
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.minimumLineSpacing = 0;
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
_zy_collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout];
_zy_collectionView.delegate = self;
_zy_collectionView.dataSource = self;
_zy_collectionView.pagingEnabled = YES;
_zy_collectionView.bounces = NO;
_zy_collectionView.showsHorizontalScrollIndicator = NO;
[self.view insertSubview:_zy_collectionView atIndex:0];
[_zy_collectionView registerClass:[ZYGuideCollectionViewCell class] forCellWithReuseIdentifier:ZYGuideCellID];
}
return _zy_collectionView;
}
- (UIPageControl *)zy_pageControl{
if(!_zy_pageControl){
_zy_pageControl = [[UIPageControl alloc] init];
_zy_pageControl.numberOfPages = self.imageArray.count;
_zy_pageControl.userInteractionEnabled = false;
UIColor * unColor = (_unselColor == nil) ? [UIColor lightGrayColor] : self.unselColor;
[_zy_pageControl setPageIndicatorTintColor:unColor];
UIColor * seColor = (_selColor == nil) ? [UIColor darkGrayColor] : _selColor;
[_zy_pageControl setCurrentPageIndicatorTintColor:seColor];
_zy_pageControl.frame = CGRectMake(0, YSCREEN_HEIGHT * 0.95, YSCREEN_WIDTH, 35);
[self.view addSubview:_zy_pageControl];
}
return _zy_pageControl;
}
- (UIButton *)skipButton
{
if (!_skipButton) {
_skipButton = [UIButton buttonWithType:UIButtonTypeCustom];
_skipButton.frame = CGRectMake(YSCREEN_WIDTH - 85, 30, 65, 30);
[_skipButton addTarget:self action:@selector(skipButtonClick) forControlEvents:UIControlEventTouchUpInside];
_skipButton.hidden = YES;
_skipButton.backgroundColor = [[UIColor lightGrayColor]colorWithAlphaComponent:0.8];
_skipButton.titleLabel.font = [UIFont systemFontOfSize:14];
_skipButton.layer.cornerRadius = 15;
_skipButton.layer.masksToBounds = YES;
[self.view addSubview:_skipButton];
}
return _skipButton;
}
@end
这里就不上传cell
的代码,就是自定义cell
中放UIImageView
到这里就实现了我们简单的自定义一个Controller
来实现我们的引导页的效果
下面是我们的实现部分:
- (void)isFirstLoginApp{
NSArray * arry = @[@"guide1",@"guide2",@"guide3",@"guide4"];
if([ZYTools zy_GetUserDefaultsWithKey:@"version"]){
[ZYTools zy_saveUserDefaultsWithKey:@"version" withId:(NSObject *)@"1"];
ZYGuidePageViewController *vc = [[ZYGuidePageViewController alloc] initPageWithImageArry:arry isShowSkipButton:YES isShowPageCount:YES selColor:[UIColor blueColor] unSelColor:[UIColor darkGrayColor]];
self.windows.rootViewController = vc;
}else{
self.windows.rootViewController = [[ViewController alloc] init];
}
}
好的这样子我们的引导就实现了,如果还有什么不懂或者有疑问的可以留言。
网友评论