滚动

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;

}

//跳转页面

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,015评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,262评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,727评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,986评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,363评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,610评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,871评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,582评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,297评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,551评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,053评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,385评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,035评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,079评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,841评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,648评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,550评论 2 270

推荐阅读更多精彩内容