判斷是不是第一次打開如果是就走輪播圖 如果不是就不走輪播圖
標(biāo)簽控制器繼承
ViewController *theVc = [[ViewController alloc]init];
self.window.rootViewController = theVc;
self.window.backgroundColor = [UIColor whiteColor];
<UIScrollViewDelegate>
{
//創(chuàng)建滑動(dòng)控件
UIScrollView *theScroll;
//分頁控件
UIPageControl *thePage;
//創(chuàng)建整型類
NSInteger tegPageTime;
//創(chuàng)建數(shù)組;
NSArray *theArray;
//計(jì)數(shù)器
NSTimer *theTime;
}
//===========
//獲取屏幕的寬
float width = self.view.frame.size.width;
//獲取屏幕的高
float height = self.view.frame.size.height;
//代替 初始化滾動(dòng)控件
theScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, width, height)];
//獲取屏幕X軸
float X = 0.0;
//一共四張圖片
for (int i = 0; i < 4; i++)
{
//圖片初始化
UIImageView *theImg = [[UIImageView alloc] initWithFrame:CGRectMake(X, 0, width, height)];
//將圖片添加到視圖里
theArray = @[[UIImage imageNamed:@"1"],[UIImage imageNamed:@"1"],[UIImage imageNamed:@"1"],[UIImage imageNamed:@"1"],];
//將圖片與數(shù)組下標(biāo)綁定
theImg.image = theArray[i];
//將圖片添加到滾動(dòng)視圖中
[theScroll addSubview:theImg];
//設(shè)置滾動(dòng)視圖的坐標(biāo) 原代碼
//(width=self.view.frame.size.width+self.view.frame.size.width+self.view.frame.size.width+self.view.frame.size.width,)
X += width;
}
//設(shè)置滾動(dòng)視圖的大小
theScroll.contentSize = CGSizeMake(width * 4, height);
// 設(shè)置滾動(dòng)視圖按頁滾動(dòng)
theScroll.pagingEnabled = YES;
//滾動(dòng)條隱藏
theScroll.showsHorizontalScrollIndicator = NO;
//設(shè)置代理
theScroll.delegate = self;
//初始化頁碼
thePage = [[UIPageControl alloc]initWithFrame:CGRectMake(width /2-50, 570, 100, 30)];
//頁數(shù)
thePage.numberOfPages = 4;
//初始頁第一頁
thePage.currentPage = 0;
//清除原來顏色
thePage.backgroundColor = [UIColor clearColor];
//沒動(dòng)原來
thePage.pageIndicatorTintColor = [UIColor greenColor];
//當(dāng)前翻頁顯示的顏色
thePage.currentPageIndicatorTintColor = [UIColor redColor];
//順序不能亂
[self.view addSubview:theScroll];
[self.view addSubview:thePage];
//記錄當(dāng)前圖片
tegPageTime = thePage.currentPage;
//第一個(gè)幾秒鐘換一下 事件
theTime = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(scroll) userInfo:nil repeats:YES];
///===========
//事件
-(void)scroll
{
tegPageTime ++;
if (tegPageTime>=theArray.count)
{
tegPageTime = 0;
}
[theScroll setContentOffset:CGPointMake(tegPageTime*theScroll.frame.size.width, 0) animated:YES];
}
//表示在滑動(dòng)滾動(dòng)視圖的時(shí)候調(diào)用此方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGPoint point=scrollView.contentOffset;//偏移
//獲得所在位置
thePage.currentPage=point.x / scrollView.frame.size.width;
UIButton *theBtn = [[UIButton alloc]init];
if (thePage.currentPage == 3)
{
//停止圖片滾動(dòng)
[theTime setFireDate:[NSDate distantFuture]];
theBtn.frame = CGRectMake(160, 607, 100, 40);
//設(shè)置按鈕背景顏色
theBtn.backgroundColor = [UIColor blueColor];
//設(shè)置按鈕為圓角
theBtn.layer.cornerRadius = 8;
//添加按鈕文字
[theBtn setTitle:@"立即登錄" forState:UIControlStateNormal];
//添加按鈕觸發(fā)事件:必須要用(UIControlEventTouchUpInside)
[theBtn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:theBtn];
//設(shè)置與用戶無法交互(這樣即可解決按鈕在前面的那頁中顯示)
//? scrollView.userInteractionEnabled = NO;
}
}
//實(shí)現(xiàn)頁碼和滾動(dòng)視圖的關(guān)聯(lián)
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
thePage.currentPage = theScroll.contentOffset.x / self.view.frame.size.width;
}
-(void)click
{
oneViewController *VC = [[oneViewController alloc]init];
[self presentViewController:VC animated:YES completion:nil];
}