iOS - 关于轮播图的实现实例
2016-12-08 16:45
225 查看
在此,因为只有一个页面,所以没有管理定时器。如果在实际开发项目中,则需要把定时器在合适的位置 kill 掉。本博客只作为一个参考的小 Demo 。。。代码如下:
// 屏幕尺寸
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
// 照片的数量
#define PHOTOCOUNT 4
// 轮播时间的间隔
#define MOVETIME 2.0
#import "ViewController.h"
@interface
ViewController () <UIScrollViewDelegate> {
UIScrollView *_scroll;
UIPageControl *_page;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super
viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIScrollView *scrollView = [[UIScrollView
alloc] init];
scrollView.frame =
self.view.bounds;
scrollView.backgroundColor = [UIColor
blackColor];
scrollView.delegate =
self;
scrollView.pagingEnabled =
YES;
scrollView.bounces =
NO;
[self.view
addSubview:scrollView];
_scroll = scrollView;
UIPageControl *page = [[UIPageControl
alloc]
init];
page.frame =
CGRectMake((WIDTH-100)/2,
HEIGHT-50,
100, 20);
page.backgroundColor = [UIColor
blackColor];
page.numberOfPages =
PHOTOCOUNT;
page.currentPageIndicatorTintColor = [UIColor
whiteColor];
page.pageIndicatorTintColor = [UIColor
redColor];
da9a
[self.view
addSubview:page];
_page = page;
for (NSInteger i =
0; i<PHOTOCOUNT+1; i++) {
UIImageView *imageView = [[UIImageView
alloc] init];
imageView.frame =
CGRectMake(WIDTH*i,
0, WIDTH,
HEIGHT);
imageView.contentMode =
UIViewContentModeScaleAspectFit;
NSString *name = [NSString
stringWithFormat:@"%ld.jpg",i];
if (PHOTOCOUNT == i) {
name = @"0.jpg";
}
imageView.image = [UIImage
imageNamed:name];
[scrollView addSubview:imageView];
}
scrollView.contentSize =
CGSizeMake(WIDTH*(PHOTOCOUNT+1),
0);
NSTimer *timer = [NSTimer
timerWithTimeInterval:MOVETIME
target:self
selector:@selector(movePhoto)
userInfo:nil
repeats:YES];
[[NSRunLoop
currentRunLoop] addTimer:timer
forMode:NSRunLoopCommonModes];
}
- (void)movePhoto {
NSInteger contentOffSet_x =
_scroll.contentOffset.x;
if (contentOffSet_x >=
PHOTOCOUNT*WIDTH) {
[_scroll
setContentOffset:CGPointMake(0,
0) animated:NO];
_page.currentPage =
0;
}else {
[_scroll
setContentOffset:CGPointMake(contentOffSet_x+WIDTH,
0) animated:YES];
_page.currentPage =
_scroll.contentOffset.x/WIDTH
+ 1;
}
if (contentOffSet_x == (PHOTOCOUNT-1)*WIDTH)
{
_page.currentPage =
0;
}
}
OK,一个简易的轮播图就到此结束。。。
// 屏幕尺寸
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
// 照片的数量
#define PHOTOCOUNT 4
// 轮播时间的间隔
#define MOVETIME 2.0
#import "ViewController.h"
@interface
ViewController () <UIScrollViewDelegate> {
UIScrollView *_scroll;
UIPageControl *_page;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super
viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIScrollView *scrollView = [[UIScrollView
alloc] init];
scrollView.frame =
self.view.bounds;
scrollView.backgroundColor = [UIColor
blackColor];
scrollView.delegate =
self;
scrollView.pagingEnabled =
YES;
scrollView.bounces =
NO;
[self.view
addSubview:scrollView];
_scroll = scrollView;
UIPageControl *page = [[UIPageControl
alloc]
init];
page.frame =
CGRectMake((WIDTH-100)/2,
HEIGHT-50,
100, 20);
page.backgroundColor = [UIColor
blackColor];
page.numberOfPages =
PHOTOCOUNT;
page.currentPageIndicatorTintColor = [UIColor
whiteColor];
page.pageIndicatorTintColor = [UIColor
redColor];
da9a
[self.view
addSubview:page];
_page = page;
for (NSInteger i =
0; i<PHOTOCOUNT+1; i++) {
UIImageView *imageView = [[UIImageView
alloc] init];
imageView.frame =
CGRectMake(WIDTH*i,
0, WIDTH,
HEIGHT);
imageView.contentMode =
UIViewContentModeScaleAspectFit;
NSString *name = [NSString
stringWithFormat:@"%ld.jpg",i];
if (PHOTOCOUNT == i) {
name = @"0.jpg";
}
imageView.image = [UIImage
imageNamed:name];
[scrollView addSubview:imageView];
}
scrollView.contentSize =
CGSizeMake(WIDTH*(PHOTOCOUNT+1),
0);
NSTimer *timer = [NSTimer
timerWithTimeInterval:MOVETIME
target:self
selector:@selector(movePhoto)
userInfo:nil
repeats:YES];
[[NSRunLoop
currentRunLoop] addTimer:timer
forMode:NSRunLoopCommonModes];
}
- (void)movePhoto {
NSInteger contentOffSet_x =
_scroll.contentOffset.x;
if (contentOffSet_x >=
PHOTOCOUNT*WIDTH) {
[_scroll
setContentOffset:CGPointMake(0,
0) animated:NO];
_page.currentPage =
0;
}else {
[_scroll
setContentOffset:CGPointMake(contentOffSet_x+WIDTH,
0) animated:YES];
_page.currentPage =
_scroll.contentOffset.x/WIDTH
+ 1;
}
if (contentOffSet_x == (PHOTOCOUNT-1)*WIDTH)
{
_page.currentPage =
0;
}
}
OK,一个简易的轮播图就到此结束。。。
相关文章推荐
- 关于DataList使用DropDownList的分页实现 技巧实例源码
- PHP开发中关于分页功能实现的代码实例一则
- 关于使用图片轮播插件无法实现效果的问题
- IOS实例 实现基于Socket TCP/IP通讯
- 关于iOS中的多线程,多种方法实现iOS多线程。
- 关于《ASP.NET2.0网站开发实例教程》中1.1.4 Default.aspx页面的实现的说明
- 问题:关于坛友的一个js轮播效果的实现
- 星火杯项目笔记——iOS调用有道API翻译句子、在AppDelegate里实现全局访问的sqlite实例【13-10-14】
- 【IOS实例小计】图像移动--可扩展为动态实现图标变化
- 关于ios中的点赞控件效果的实现--UIControl
- iOS中的单实例的实现
- IOS页面滚动指示器实现实例
- IOS表视图动态高度实现实例
- 关于使用图片轮播插件无法实现效果的问题
- iOS 实现简单的移动UIView代码实例
- 关于Mule3.0中WebService Proxy的实现机制和实例浅析
- IOS 文本高度自适应变化实现实例
- 关于ios实现键盘隐藏的方法小节
- IOS关于从照相机获取图片的问题,初步探究通过摄像头实现颜色与形状识别机制==
- 问题:关于坛友的一个js轮播效果的实现