AppDelegate.m
import "ViewController.h"
import "NewsViewController.h"
// 获取NSUserDefaults单例对象
NSUserDefaults *theUser=[NSUserDefaults standardUserDefaults];
// 通过单例对象获取key:isOpened的value值
BOOL isOpend=[theUser boolForKey:@"isOpened"];
if (isOpend == YES) {
// 直接进入到主页面或者登陆页面
NewsViewController *theNvct=[[NewsViewController alloc]init];
self.window.rootViewController=theNvct;
}else
{
// 直接进入到主页面或者登陆页面(体验页面)
ViewController *theView=[[ViewController alloc]init];
self.window.rootViewController=theView;
// 通过通过单例对象对key:isOpened赋值为YES
[theUser setBool:YES forKey:@"isOpened"];
}
//#import "ViewController.m"
import "NewsViewController.h"
import "AppDelegate.h"
@interface ViewController ()<UIScrollViewDelegate>
{
//创建滚动控件
UIScrollView *theScroll;
//创建数组
NSArray *theArray;
//分页控件
UIPageControl *thePage;
//添加计数器(用来实现页面的自动滚动)
NSTimer *theTimer;
//创建整形类(更方便判断滚动的页面)
NSInteger timerPage;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//获取页面宽度个高度
float width=self.view.frame.size.width;
float hight=self.view.frame.size.height;
//初始化滚动控件
theScroll=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, width, hight)];
//获取X轴
float X=0.0;
for (int i=0; i<4; i++) {
//给滚动视图添加图片 0 0
// 375 0
// 375*2 0
// 375*3 0
//给图片添加成背景图
UIImageView *theImage=[[UIImageView alloc]initWithFrame:CGRectMake(X, 0, width, hight)];
//将4张图片用数组形式存入数据中
theArray=@[[UIImage imageNamed:@"1.jpg"],[UIImage imageNamed:@"2.jpg"],[UIImage imageNamed:@"3.jpg"],[UIImage imageNamed:@"4.jpg"]];
//将图片与数据进行绑定
theImage.image=theArray[i];
//将图片添加到滚动视图上
[theScroll addSubview:theImage];
//每张图片都要变化宽度变
X +=width;
}
//设置滚动视图总共的大小(总共宽度)
theScroll.contentSize=CGSizeMake(width*4, hight);
//滚动视图是否按页跳转
theScroll.pagingEnabled=YES;
//隐藏滚动条
theScroll.showsHorizontalScrollIndicator=NO; //水平
// theScroll.showsVerticalScrollIndicator=NO 垂直
theScroll.delegate=self;
//初始化页码
thePage=[[UIPageControl alloc]initWithFrame:CGRectMake(width/2-50, 570, 100, 30)];
//设置页码个数
thePage.numberOfPages=4;
//设置页码初始页
thePage.currentPage=0;
//设置页码颜色
thePage.pageIndicatorTintColor=[UIColor blueColor];
//设置当前页码颜色
thePage.currentPageIndicatorTintColor=[UIColor yellowColor];
//把滚动控件添加到视图上
[self.view addSubview:theScroll];
//把分页控件添加到视图上
[self.view addSubview:thePage];
//给计时器添加方法
theTimer=[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(scrollChange) userInfo:nil repeats:YES];
// Do any additional setup after loading the view, typically from a nib.
}
//设置计时器方法
-(void)scrollChange
{
timerPage++;
if (timerPage >= theArray.count-1) {
//销毁定时器
[theTimer invalidate];
}
// //点的大小 为了把点 放到中间
[theScroll setContentOffset:CGPointMake(timerPage*theScroll.frame.size.width, 0) animated:YES];
}
//遵守两个协议方法
//表示在滑动滚动视图的时候调用此方法
-
(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGPoint point=scrollView.contentOffset;//偏移量
//当前点的第几张图片
thePage.currentPage=point.x/scrollView.frame.size.width;if (thePage.currentPage == theArray.count-1) {
//创建一个按钮
UIButton *theButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
theButton.frame=CGRectMake(180, 500, 100, 30);[theButton setTitle:@"立即体验" forState:UIControlStateNormal]; [theButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; theButton.backgroundColor=[UIColor redColor]; [theButton addTarget:self action:@selector(NextNewPageChange) forControlEvents:UIControlEventTouchUpInside]; //把按钮添加到滚动视图上 [self.view addSubview:theButton];
}
}
//设置方法跳转下一页面
-(void)NextNewPageChange
{
NewsViewController *theNew=[[NewsViewController alloc]init];
AppDelegate *app=(AppDelegate*)[UIApplication sharedApplication].delegate;
app.window.rootViewController=theNew;
}
//实现页码和滚动视图的关联
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
//当前页 把滚动的页数与滚动控件进行关联
thePage.currentPage=theScroll.contentOffset.x/self.view.frame.size.width;
}
//跳转页面