滚动

作者: 9898a | 来源:发表于2017-09-21 23:14 被阅读0次

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;

}

//跳转页面

相关文章

  • 监听scrollview停止滚动

    方法1 滚动开始 滚动结束: 手动停止滚动 自动停止滚动 方法2 滚动开始 滚动结束

  • css滚动

    布局:水平滚动,垂直滚动 垂直滚动: 水平滚动: css样式:

  • css滚动

    布局:水平滚动,垂直滚动 垂直滚动: 水平滚动: css样式:

  • CSS 隐藏滚动条 但可以滚动

    垂直滚动,用鼠标滚轮滚动 横向滚动 其他垂直滚动方法

  • UIScrollView常用方法

    -指定滚动视图的滚动尺寸。滚动视图正常工作必须要指定滚动视图的滚动尺寸 scrollView.contentSiz...

  • UIScrollView 代理

    滚动时执行 开始滚动时执行 滚动后手指离开屏幕时执行 滚动后手指离开后执行 手指后滚动时 滚动结束执行

  • scroll和wheel事件

    scroll 1 .scroll事件在滚动条滚动时被触发2 . scroll检测的是滚动条的滚动,当滚动条不能滚动...

  • 几种无限不间断滚动效果

    1.向上滚动 2.向下滚动 3.向左滚动 4.向右滚动

  • WeChat小程序视图篇~Scroll-view

    scroll-view 是什么? 微信小程序滚动视图,分为横向滚动,纵向滚动。以及滚动事件。 如何用? 纵向滚动-...

  • 微信小程序 - 6.组件(scroll-view)

    scroll-viewscroll-view组件为滚动视图,分为水平滚动和垂直滚动。注意滚动视图垂直滚动时一定要设...

网友评论

      本文标题:滚动

      本文链接:https://www.haomeiwen.com/subject/xeqxextx.html