您的位置:首页 > 其它

图片轮播器[ScrollView,PageControl]

2015-09-03 15:23 225 查看
//

// ViewController.m

// 图片轮播器

//

// Created by qianfeng on 15/8/6.

// Copyright (c) 2015年
坏叔叔. All rights reserved.

//

#import "ViewController.h"

@interface
ViewController () <UIScrollViewDelegate>
{

NSInteger _currentPage;
}

@property (strong,
nonatomic) UIPageControl *pageControl;

@property (nonatomic,
strong) UIScrollView *scrollView;

@property (strong,
nonatomic) NSTimer *timer;

@end

@implementation ViewController

- (void)viewDidLoad {

[super
viewDidLoad];

// 1. 创建ScrollView、Pagecontrol

[self
createScrollViewAndPageControl];

// 2. 启动定时器
[self
addTimer];
}

- (void)createScrollViewAndPageControl {

self.scrollView = [[UIScrollView
alloc] init];

self.scrollView.frame =
CGRectMake(10,
20, 300,
150);

self.scrollView.backgroundColor = [UIColor
blueColor];
[self.view
addSubview:self.scrollView];

//
设置代理

self.scrollView.delegate =
self;

CGFloat imageW =
self.scrollView.frame.size.width;

CGFloat imageH =
self.scrollView.frame.size.height;

CGFloat imageY = 0;

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

UIImageView *imageView = [[UIImageView
alloc] init];

CGFloat imageX = i * imageW;
imageView.frame =
CGRectMake(imageX, imageY, imageW, imageH);

// 获取图片名

NSString *name = [NSString
stringWithFormat:@"img_0%d",i+1];
imageView.image = [UIImage
imageNamed:name];

[self.scrollView
addSubview:imageView];
}

self.scrollView.contentSize =
CGSizeMake(imageW *
5, imageH);

self.scrollView.showsHorizontalScrollIndicator =
NO;

self.scrollView.pagingEnabled =
YES;

//
添加pageControl

self.pageControl = [[UIPageControl
alloc] initWithFrame:CGRectMake(10,
150,
300, 20)];

[self.pageControl
setBackgroundColor:[UIColor
clearColor]]; //
清除背景色

self.pageControl.pageIndicatorTintColor = [UIColor
grayColor];

self.pageControl.numberOfPages =
5;

_currentPage =
self.pageControl.numberOfPages;
[self.view
addSubview:self.pageControl];

//
调用点击pagecontrol上的点
方法

[self
performSelector:@selector(clickedPoint)
withObject:nil
afterDelay:0.5];

[self.pageControl
addTarget:self
action:@selector(pageControlTouched:)
forControlEvents:UIControlEventTouchUpInside];
}

/**

* 点击pagecontrol上的点可以调整图片显示

*/
- (void)clickedPoint {

for (int i =
0; i < self.pageControl.subviews.count; i++) {

UIView *oneView = [self.pageControl.subviews
objectAtIndex:i];
oneView.tag =
10 + i;

// NSLog(@"%@", oneView);

// 添加手势

UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer
alloc]
initWithTarget:self
action:@selector(tapGR:)];
[oneView
addGestureRecognizer:tapGR];
}
}

- (void)tapGR:(UITapGestureRecognizer *)tapGR {

UIView *theView = tapGR.view;

self.pageControl.currentPage = theView.tag -
10;

CGPoint offset =
CGPointMake(self.scrollView.frame.size.width
* self.pageControl.currentPage,
0);

[self.scrollView
setContentOffset:offset animated:YES];
}

/**

* 页面之间的滑动及转换

*/
- (void)pageControlTouched:(UIPageControl *)pageControl {

if (pageControl.currentPage ==
0 && _currentPage ==
0) {
pageControl.currentPage =
4;
}else
if (pageControl.currentPage ==
4 && _currentPage ==
4){
pageControl.currentPage =
0;
}

_currentPage = pageControl.currentPage;

CGPoint offset = CGPointMake(self.scrollView.frame.size.width
* pageControl.currentPage,
0);

[self.scrollView
setContentOffset:offset animated:YES];
}

/**

* 代理方法
在拖拽的时候暂停定时器并释放timer

*/
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {

[self.timer
invalidate];

self.timer =
nil;
}

/**

* 代理方法
在拖拽的结束启动定时器

*/
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{

// 1.
启动定时器
[self
addTimer];
}

/**

* 代理方法

*/
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

if (self.timer) {

return;
}

self.pageControl.currentPage = (scrollView.contentOffset.x + scrollView.frame.size.width
* 0.5)/ scrollView.frame.size.width;
}
- (void)addTimer
// 定时器
{

// self.timer = [NSTimer timerWithTimeInterval:2 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];

// [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];

self.timer = [NSTimer
scheduledTimerWithTimeInterval:2.0
target:self
selector:@selector(nextImage)
userInfo:nil
repeats:YES];
}

- (void)nextImage
{

// 1.下一页

if (self.pageControl.currentPage ==
4) {

self.pageControl.currentPage =
0;
}
else {

self.pageControl.currentPage++;
}

// 2.设置滚动

CGPoint offset =
CGPointMake(self.scrollView.frame.size.width
* self.pageControl.currentPage,
0);

[self.scrollView
setContentOffset:offset animated:YES];
}

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