UIScrollView

//滚动视图,单其本身也是一个独立的视图

myscrollView = [[UIScrollView alloc]initWithFrame:self.view.bounds];

myscrollView.backgroundColor = [UIColor grayColor];

//指定滚动视图的滚动尺寸

//如果想让滚动视图正常工作,必须要指定当前滚动视图的滚动尺寸大于2个以上当前屏幕尺寸

myscrollView.contentSize = CGSizeMake(self.view.bounds.size.width *2, self.view.bounds.size.height);

//指定滚动视图滚动方式为分页滚动

myscrollView.pagingEnabled = YES;

//隐藏滚动条

myscrollView.showsHorizontalScrollIndicator = NO;

myscrollView.showsVerticalScrollIndicator = NO;

//设置是否可以滚动

//    scrollView.scrollEnabled = NO;

//关闭滚动视图回弹效果

//    scrollView.bounces = NO;

//设置滚动条样式

//    scrollView.indicatorStyle = UIScrollViewIndicatorStyleBlack;

//    scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 50, 0, 0);

//指定scrollView的代理为当前类的实例

myscrollView.delegate = self;

[self.view addSubview:myscrollView];


UIImageView * imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 568)];

imageView1.image = [UIImage imageNamed:@"记私密"];

[myscrollView addSubview:imageView1];

UIImageView * imageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(320, 0, 320, 568)];

imageView2.image = [UIImage imageNamed:@"玩游戏"];

[myscrollView addSubview:imageView2];

//创建分页指示器

pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, 553, 320, 15)];

//指定pageControl相关参数

//控制页面下圆点数

pageControl.numberOfPages = 2;

//设置背景颜色

pageControl.backgroundColor = [UIColor grayColor];

//分页指示器 行为的调用

[pageControl addTarget:self action:@selector(pageControlClick:) forControlEvents:UIControlEventValueChanged];

[self.view addSubview:pageControl];

//

pageCount = 0;

// NSTimer 计时器

  [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(imageViewScroll:) userInfo:nil repeats:YES];

}

//滚动视图开始滚动时执行

-(void)scrollViewDidScroll:(UIScrollView *)scrollView

{

NSLog(@"滚动");

//输出当前滚动视图偏移量

//    NSLog(@"%lf",scrollView.contentOffset.x);

if ((int)scrollView.contentOffset.x % (int)self.view.bounds.size.width == 0) {

pageControl.currentPage = (int)scrollView.contentOffset.x / (int)self.view.bounds.size.width;

}

}


//计时器执行方法

-(void)imageViewScroll:(id)sender

{

if (pageCount == 1) {

pageCount = 0;

}else{

pageCount++;

}

[myscrollView setContentOffset:CGPointMake(self.view.bounds.size.width *pageCount, 0) animated:YES];

pageControl.currentPage = pageCount;

}

//分页指示器的方法

-(void)pageControlClick:(UIPageControl *)sender

{

NSLog(@"%ld",sender.currentPage);

//通过offset偏移量来修改当前滚动视图的滚动位置

[myscrollView setContentOffset:CGPointMake(self.view.bounds.size.width * sender.currentPage, 0) animated:YES];

}

你可能感兴趣的:(UIScrollView)