您的位置:首页 > 其它

scrollView ——自动轮播并且设置图片底部小圆点

2016-08-13 16:24 323 查看
#import "ScrollViewController.h"
#import "Global.h"

#define ScrollViewHeight (SCREENHEIGHT - 100)

@interface ScrollViewController ()

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, strong) NSTimer *timer;

@end

@implementation ScrollViewController

- (void)viewDidLoad
{
[super viewDidLoad];

[self initView];
}

- (void)initView
{

//设置滚动视图的主体
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, ScrollViewHeight)];

_scrollView.pagingEnabled = YES;
_scrollView.bounces = NO;

_scrollView.delegate = self;

for (int i = 1; i < 5; ++i)
{

UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(0, (i - 1) * ScrollViewHeight, SCREENWIDTH, ScrollViewHeight)];

imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg", i]];
[_scrollView addSubview:imageView];
}

//设置第五张图片和第一张图片相同

UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(0, 4 * ScrollViewHeight, SCREENWIDTH, ScrollViewHeight)];

imageView.image = [UIImage imageNamed:@"1.jpg"];
[_scrollView addSubview:imageView];

_scrollView.contentSize = CGSizeMake(SCREENWIDTH, ScrollViewHeight * 5);

[self.view addSubview:_scrollView];

//设置时钟
_timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(changeView) userInfo:nil repeats:YES];

//设置底部文字
_label = [[UILabel alloc] initWithFrame:CGRectMake(SCREENWIDTH / 2 - 50, ScrollViewHeight, SCREENWIDTH, 50)];
_label.textColor = [UIColor whiteColor];
_label.text = @"这是第1页";
[self.view addSubview:_label];

//设置视图上的小圆点
_pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, ScrollViewHeight - 50, SCREENWIDTH, 30)];
_pageControl.numberOfPages = 4;
_pageControl.currentPage = 0;

[_pageControl addTarget:self action:@selector(pageTurn:) forControlEvents:UIControlEventValueChanged];

[self.view addSubview:_pageControl];

}

//设置时钟调用的方法
- (void)changeView
{
float resultPiontY = 0.0;
//这里获取的信息是未翻页前的信息。

//当前的点不在最后一页的时候,每次正常翻页,当是最后一页的时候,把最后一页变为第一页(小圆点当前页数设置为1,当前点设置为0.0)
//注意:当前的计算点是下一页的点,当前计算的页数是下一页的页数
int i = 1;
if (_scrollView.contentOffset.y < 3 * ScrollViewHeight)
{
i = _scrollView.contentOffset.y / (SCREENHEIGHT - 100) + 1;
}
else if (_scrollView.contentOffset.y == 3 *ScrollViewHeight)
{
i = 0;
}
else
{
_scrollView.contentOffset = CGPointMake(0, 0);
}

_label.text = [NSString stringWithFormat:@"这是第%d页", i + 1];

_pageControl.currentPage = i;

resultPiontY = _scrollView.contentOffset.y + ScrollViewHeight;

[_scrollView setContentOffset:CGPointMake(0, resultPiontY) animated:YES];
}

//设置小圆点点击方法
- (void)pageTurn:(UIPageControl *)sender
{

[_scrollView setContentOffset:CGPointMake(0, sender.currentPage * _scrollView.frame.size.height) animated:YES];
}

//当开始拖拽的时候停止时钟
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[_timer setFireDate:[NSDate distantFuture]];
}

//当结束拖拽时执行的方法,
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
_label.text = [NSString stringWithFormat:@"这是第%d页", (int)(scrollView.contentOffset.y / (SCREENHEIGHT - 100)) + 1];

}
//当结束滚当时执行的方法,变更页数,并且在5秒后开始时钟
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
//这里获取的信息是已经翻过页的信息
int i = 0;
if (_scrollView.contentOffset.y < 4 * ScrollViewHeight)
{
i = scrollView.contentOffset.y / (SCREENHEIGHT - 100);
}
else
{
_scrollView.contentOffset = CGPointMake(0, 0);
i = 0;
}

_label.text = [NSString stringWithFormat:@"这是第%d页", i + 1];

_pageControl.currentPage = i;

[_timer setFireDate:[NSDate dateWithTimeInterval:5.0 sinceDate:[NSDate date]]];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息