swift和oc邏輯上都是一樣的,只是寫法不一樣,可以使用一個view,也可以使用一個viewController,兩種都可以的,使用view注意初始化的時候給他一個frame,vc的話,直接在本控制器里面寫控制即可!
1,創建一個vc,然后在里面初始化一個scrollview,升值contentsize為3 * 頁面寬度,然后添加圖片,最后可以實現相應的代理方法,判斷最后是點擊進入主頁,還是滑動
具體代碼如下:
import UIKit class hDisplayViewController: UIViewController,UIScrollViewDelegate { //頁面數量 var numOfPages = 3 override func viewDidLoad() { super.viewDidLoad() let frame = self.view.bounds //scrollView的初始化 let scrollView = UIScrollView() scrollView.frame = self.view.bounds scrollView.delegate = self //為了能讓內容橫向滾動,設置橫向內容寬度為3個頁面的寬度總和 scrollView.contentSize = CGSize(width:frame.size.width * CGFloat(numOfPages), height:frame.size.height) print("\(frame.size.width*CGFloat(numOfPages)),\(frame.size.height)") scrollView.isPagingEnabled = true scrollView.showsHorizontalScrollIndicator = false scrollView.showsVerticalScrollIndicator = false scrollView.scrollsToTop = false for i in 0..<numOfPages{ let imgfile = "bg\(Int(i+1)).png" print(imgfile) let image = UIImage(named:"\(imgfile)") let imgView = UIImageView(image: image) imgView.frame = CGRect(x:frame.size.width*CGFloat(i), y:CGFloat(0), width:frame.size.width, height:frame.size.height) scrollView.addSubview(imgView) } scrollView.contentOffset = CGPoint.zero self.view.addSubview(scrollView) } func scrollViewDidScroll(_ scrollView: UIScrollView) { print("scrolled:\(scrollView.contentOffset)") let twidth = CGFloat(numOfPages-1) * self.view.bounds.size.width //如果在最后一個頁面繼續滑動的話就會跳轉到主頁面 if scrollView.contentOffset.x > twidth { // let mainStoryboard = UIStoryboard(name:"Main", bundle:nil) // let viewController = mainStoryboard.instantiateInitialViewController() // self.present(MainVC(), animated: true, completion: nil) let rootVC = UIApplication.shared.delegate as! AppDelegate
rootVC.window?.rootViewController = MainVC() } } }
2,在appdelegate里面寫如下代碼,判斷是否第一次安裝,
//這里判斷是否第一次啟動APP if (!(UserDefaults.standard.bool(forKey: "everLaunched"))) { UserDefaults.standard.set(true, forKey:"everLaunched") let guideViewController = hDisplayViewController() self.window!.rootViewController=guideViewController; print("guideview launched!") }
如下圖:
這樣就完成了!
下面分享一個oc版的:
.h
@interface hDisplayView : UIView
.m :注意實現代理方法,判斷偏移量或者其他的都行,因為是view,也可以添加手機或者按鈕(建議frame是全屏幕大小的),然后實現響應事件隱藏就行了!
#import "hDisplayView.h" @interface hDisplayView ()<UIScrollViewDelegate> { UIScrollView *_bigScrollView; NSMutableArray *_imageArray; UIPageControl *_pageControl; } @end @implementation hDisplayView -(instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { _imageArray = [@[@"閃屏1.png",@"閃屏2.png", @"閃屏3.png",@"閃屏4.png"]mutableCopy]; // _imageArray = [NSMutableArray arrayWithObjects:@"閃屏1.png",@"閃屏2.png", @"閃屏3.png",@"閃屏4.png", nil]; UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, MainScreen_width, MainScreen_height)]; scrollView.contentSize = CGSizeMake((_imageArray.count + 1)*MainScreen_width, MainScreen_height); //設置反野效果,不允許反彈,不顯示水平滑動條,設置代理為自己 scrollView.pagingEnabled = YES;//設置分頁 scrollView.bounces = NO; scrollView.showsHorizontalScrollIndicator = NO; scrollView.delegate = self; [self addSubview:scrollView]; _bigScrollView = scrollView; for (int i = 0; i < _imageArray.count; i++) { UIImageView *imageView = [[UIImageView alloc]init]; imageView.frame = CGRectMake(i * MainScreen_width, 0, MainScreen_width, MainScreen_height); UIImage *image = [UIImage imageNamed:_imageArray[i]]; imageView.image = image; [scrollView addSubview:imageView]; } UIPageControl *pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(MainScreen_width/2, MainScreen_height - 60, 0, 40)]; pageControl.numberOfPages = _imageArray.count; pageControl.backgroundColor = [UIColor clearColor]; [self addSubview:pageControl]; _pageControl = pageControl; //添加手勢 UITapGestureRecognizer *singleRecognizer; singleRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleSingleTapFrom)]; singleRecognizer.numberOfTapsRequired = 1; [scrollView addGestureRecognizer:singleRecognizer]; } return self; } -(void)handleSingleTapFrom { if (_pageControl.currentPage == 3) { self.hidden = YES; } } -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { if (scrollView == _bigScrollView) { CGPoint offSet = scrollView.contentOffset; _pageControl.currentPage = offSet.x/(self.bounds.size.width);//計算當前的頁碼 [scrollView setContentOffset:CGPointMake(self.bounds.size.width * (_pageControl.currentPage), scrollView.contentOffset.y) animated:YES]; } if (scrollView.contentOffset.x == (_imageArray.count) *MainScreen_width) { self.hidden = YES; } }
調用:
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"everLaunched"]) { [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"everLaunched"]; [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"]; } else{ [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"firstLaunch"]; } if ([[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"]) { // 這里判斷是否第一次 hDisplayView *hvc = [[hDisplayView alloc]initWithFrame:CGRectMake(0, 0, MainScreen_width, MainScreen_height)]; [self.window.rootViewController.view addSubview:hvc]; [UIView animateWithDuration:0.25 animations:^{ hvc.frame = CGRectMake(0, 0, MainScreen_width, MainScreen_height); }]; }
然后就可以了!
比較簡單,只在此記錄下!