实现图片轮播
2015-10-15 11:07
267 查看
//
// ViewController.m
// 图片轮播
//
// Created by weibiao
// Copyright © 2015年 weibiao. All rights reserved.
//
#import "ViewController.h"
#define KimageCount 5
@interface
ViewController ()<UIScrollViewDelegate>
@property (nonatomic,
strong) UIScrollView *scrollView;
@property (nonatomic,
strong) UIPageControl *pageControl;
@property (nonatomic,
strong) NSTimer *timer;//增加时钟
@end
@implementation ViewController
//scrollView懒加载
-(UIScrollView *)scrollView {
if (!_scrollView) {
_scrollView = [[UIScrollView
alloc] initWithFrame:CGRectMake(10,
20,
300, 130)];
_scrollView.backgroundColor = [UIColor
grayColor];
[self.view
addSubview:_scrollView];
//设置滚动尺寸
_scrollView.contentSize =
CGSizeMake(KimageCount *
300,
0);//0表示可滚动的范围为0,既不可y方向滚动
//取消弹簧效果
_scrollView.bounces =
NO;
//取消滚动条
_scrollView.showsHorizontalScrollIndicator =
NO;
_scrollView.showsVerticalScrollIndicator =
NO;
//设置分页
_scrollView.pagingEnabled =
YES;
//设置代理
_scrollView.delegate =
self;
}
return
_scrollView;
}
-(UIPageControl *)pageControl {
if (!_pageControl) {
//分页控件
_pageControl = [[UIPageControl
alloc] init];
//总页数
_pageControl.numberOfPages =
KimageCount;
//控件尺寸
CGSize size = [_pageControl
sizeForNumberOfPages:KimageCount];
_pageControl.bounds =
CGRectMake(0,
0, size.width, size.height);
_pageControl.center =
CGPointMake(self.view.center.x,
130);
//设置颜色
_pageControl.pageIndicatorTintColor = [UIColor
redColor];
_pageControl.currentPageIndicatorTintColor = [UIColor
blueColor];
[self.view
addSubview:_pageControl];
//添加监听方法
[_pageControl
addTarget:self
action:@selector(pageChanged:)
forControlEvents:UIControlEventValueChanged];
}
return
_pageControl;
}
//实现监听方法
-(void)pageChanged:(UIPageControl *)page {
//根据参数页数,调整轮播中的图片
CGFloat x = page.currentPage*self.scrollView.bounds.size.width;
[self.scrollView
setContentOffset:CGPointMake(x,
0) animated:YES];
}
- (void)viewDidLoad {
[super
viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
for (int i =
0; i < KimageCount; i++) {
NSString *imageName = [NSString
stringWithFormat:@"img_0%d",i+1];
UIImage *image = [UIImage
imageNamed:imageName];
UIImageView *imageView = [[UIImageView
alloc] initWithFrame:self.scrollView.bounds];
imageView.image = image;
[self.scrollView
addSubview:imageView];
}
//计算imageView的位置
[self.scrollView.subviews
enumerateObjectsUsingBlock:^(
UIImageView *imageView, NSUInteger idx,
BOOL *stop) {
CGRect frame = imageView.frame;
frame.origin.x = idx * frame.size.width;
imageView.frame = frame;
}];
//初始轮播显示页数
self.pageControl.currentPage =
0;
// NSLog(@"%@",self.scrollView.subviews);
//调用时钟
[self
startTimer];
}
-(void)startTimer {
// self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES];
self.timer = [NSTimer
timerWithTimeInterval:2.0
target:self
selector:@selector(updateTimer)
userInfo:nil
repeats:YES];
//添加到运行循环中后,此时轮播中的图片不支持拖动,解决方法是设置scrollView的代理方法
[[NSRunLoop
currentRunLoop] addTimer:self.timer
forMode:NSRunLoopCommonModes];
}
//支持拖动的scrollView的拖动方法
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {//拖动开始
//停止时钟,注意停止后,不会在进行时钟,必须调用设置的startTimer方法
[self.timer
invalidate];
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
[self
startTimer];
}
-(void)updateTimer {
NSLog(@"%ld",self.pageControl.currentPage+1);
int page = (self.pageControl.currentPage +
1) % KimageCount;
self.pageControl.currentPage = page;
//调用监听方法
[self
pageChanged:self.pageControl];
}
#pragma mark -- UIScrollView的代理方法
//修改当前的页数(小点)
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
int page =
self.scrollView.contentOffset.x /
self.scrollView.bounds.size.width;
self.pageControl.currentPage = page;
}
@end
// ViewController.m
// 图片轮播
//
// Created by weibiao
// Copyright © 2015年 weibiao. All rights reserved.
//
#import "ViewController.h"
#define KimageCount 5
@interface
ViewController ()<UIScrollViewDelegate>
@property (nonatomic,
strong) UIScrollView *scrollView;
@property (nonatomic,
strong) UIPageControl *pageControl;
@property (nonatomic,
strong) NSTimer *timer;//增加时钟
@end
@implementation ViewController
//scrollView懒加载
-(UIScrollView *)scrollView {
if (!_scrollView) {
_scrollView = [[UIScrollView
alloc] initWithFrame:CGRectMake(10,
20,
300, 130)];
_scrollView.backgroundColor = [UIColor
grayColor];
[self.view
addSubview:_scrollView];
//设置滚动尺寸
_scrollView.contentSize =
CGSizeMake(KimageCount *
300,
0);//0表示可滚动的范围为0,既不可y方向滚动
//取消弹簧效果
_scrollView.bounces =
NO;
//取消滚动条
_scrollView.showsHorizontalScrollIndicator =
NO;
_scrollView.showsVerticalScrollIndicator =
NO;
//设置分页
_scrollView.pagingEnabled =
YES;
//设置代理
_scrollView.delegate =
self;
}
return
_scrollView;
}
-(UIPageControl *)pageControl {
if (!_pageControl) {
//分页控件
_pageControl = [[UIPageControl
alloc] init];
//总页数
_pageControl.numberOfPages =
KimageCount;
//控件尺寸
CGSize size = [_pageControl
sizeForNumberOfPages:KimageCount];
_pageControl.bounds =
CGRectMake(0,
0, size.width, size.height);
_pageControl.center =
CGPointMake(self.view.center.x,
130);
//设置颜色
_pageControl.pageIndicatorTintColor = [UIColor
redColor];
_pageControl.currentPageIndicatorTintColor = [UIColor
blueColor];
[self.view
addSubview:_pageControl];
//添加监听方法
[_pageControl
addTarget:self
action:@selector(pageChanged:)
forControlEvents:UIControlEventValueChanged];
}
return
_pageControl;
}
//实现监听方法
-(void)pageChanged:(UIPageControl *)page {
//根据参数页数,调整轮播中的图片
CGFloat x = page.currentPage*self.scrollView.bounds.size.width;
[self.scrollView
setContentOffset:CGPointMake(x,
0) animated:YES];
}
- (void)viewDidLoad {
[super
viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
for (int i =
0; i < KimageCount; i++) {
NSString *imageName = [NSString
stringWithFormat:@"img_0%d",i+1];
UIImage *image = [UIImage
imageNamed:imageName];
UIImageView *imageView = [[UIImageView
alloc] initWithFrame:self.scrollView.bounds];
imageView.image = image;
[self.scrollView
addSubview:imageView];
}
//计算imageView的位置
[self.scrollView.subviews
enumerateObjectsUsingBlock:^(
UIImageView *imageView, NSUInteger idx,
BOOL *stop) {
CGRect frame = imageView.frame;
frame.origin.x = idx * frame.size.width;
imageView.frame = frame;
}];
//初始轮播显示页数
self.pageControl.currentPage =
0;
// NSLog(@"%@",self.scrollView.subviews);
//调用时钟
[self
startTimer];
}
-(void)startTimer {
// self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES];
self.timer = [NSTimer
timerWithTimeInterval:2.0
target:self
selector:@selector(updateTimer)
userInfo:nil
repeats:YES];
//添加到运行循环中后,此时轮播中的图片不支持拖动,解决方法是设置scrollView的代理方法
[[NSRunLoop
currentRunLoop] addTimer:self.timer
forMode:NSRunLoopCommonModes];
}
//支持拖动的scrollView的拖动方法
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {//拖动开始
//停止时钟,注意停止后,不会在进行时钟,必须调用设置的startTimer方法
[self.timer
invalidate];
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
[self
startTimer];
}
-(void)updateTimer {
NSLog(@"%ld",self.pageControl.currentPage+1);
int page = (self.pageControl.currentPage +
1) % KimageCount;
self.pageControl.currentPage = page;
//调用监听方法
[self
pageChanged:self.pageControl];
}
#pragma mark -- UIScrollView的代理方法
//修改当前的页数(小点)
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
int page =
self.scrollView.contentOffset.x /
self.scrollView.bounds.size.width;
self.pageControl.currentPage = page;
}
@end
相关文章推荐
- 代码工具1. 自动根据json字符串生成javabean类
- 阿里2015实习生招聘在线测试----编程题,设计有限任务响应队列
- 关于iOS9中的App Transport Security相关说明及适配
- 数据结构与算法分析:栈与队列
- Building Apps Faster with Android Data Binding
- ajaxFileUpload 异步上传文件
- pthread_attr_init线程属性
- 短信发送注意事项
- iOS Font
- mmap 的理解
- #pragma详解
- <LeetCode><Easy> 219 ContainDuplicate 2
- C++面试问题
- android studio -- 图片应该放在drawable还是mipmap?
- 剥开ios 系统sandbox神秘面纱
- Future 类
- iOS:iOS中的几种动画
- spring Bean的生命周期
- python写文件乱码
- 线性表--单链表