黑马程序员技术交流社区
标题: 图片自动轮播器 [打印本页]
作者: 余丽丽 时间: 2015-8-12 00:13
标题: 图片自动轮播器
效果图:
代码:
// Created by 余丽丽 on 15/8/11.
#import "ViewController.h"
//scrollView中存放的imageView的个数
#define IMGCOUNT 30
//scrollView图片轮播的时间间隔
#define TIME 1
@interface ViewController ()<UIScrollViewDelegate>
//滚动控件
@property (weak,nonatomic)UIScrollView *scrollView;
//页码显示控件
@property (weak,nonatomic)UIPageControl *pageContol;
//定时器
@property (weak,nonatomic)NSTimer * timer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//动态创建UIScrollView
//1、动态创建scrollView
UIScrollView *scrollView = [[UIScrollView alloc]init];
//2、设置scrollView相关属性 view W375 H667
//设置frame
scrollView.frame = CGRectMake(37, 77, 320, 512);
//设置滑动条
scrollView.showsHorizontalScrollIndicator=NO;
//设置分页
scrollView.pagingEnabled=YES;
//设置代理
scrollView.delegate=self;
//设置内容大小
scrollView.contentSize=CGSizeMake(scrollView.frame.size.width*IMGCOUNT, 0);
//3、添加scrollView到view中,同时跟self.scrollView进行绑定
[self.view addSubview:scrollView];
self.scrollView = scrollView;
//动态创建UIImageView
//获取主资源包
NSBundle *mainBundle = [NSBundle mainBundle];
//要加载的图片的统一规格
CGFloat imgW = 320;
CGFloat imgH = 512;
CGFloat imgY = 0;
for (int i =0; i<IMGCOUNT; i++) {
UIImageView *imgView =[[UIImageView alloc]init];
//设置image
//生成图片名字
NSString *string=[NSString stringWithFormat:@"knockout_%02d",i];
//获取图片路径
NSString *path = [mainBundle pathForResource:string ofType:@"jpg"];
imgView.image = [UIImage imageWithContentsOfFile:path];
//设置frame
CGFloat imgX = imgW*i;
imgView.frame = CGRectMake(imgX, imgY, imgW, imgH);
//添加到scrollview中
[self.scrollView addSubview:imgView];
}
//动态创建一个UIPageControl
UIPageControl* pageControl = [[UIPageControl alloc]init];
//设置UIPageControl
//设置页数
[pageControl setNumberOfPages:IMGCOUNT];
//设置颜色为蓝色
pageControl.pageIndicatorTintColor = [UIColor blueColor];
//当前页码颜色为红色
pageControl.currentPageIndicatorTintColor = [UIColor redColor];
//设置frame
pageControl.frame=CGRectMake(0, 600, 320, 27);
//将pageControl添加到view中
[self.view addSubview:pageControl];
//将pageControl与self.pageContol绑定
self.pageContol = pageControl;
self.pageContol.currentPage=0;
//4、开启自动轮播
[self starTimer];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
//监听拖动前
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
//停止计时器
[self overTimer];
}
//监听拖动时
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//获取scrollView的偏移
CGPoint point = self.scrollView.contentOffset;
//获取scrollView的宽度
CGFloat imageH =self.scrollView.frame.size.width;
//计算page
self.pageContol.currentPage=(point.x+imageH*0.5)/imageH;
}
//监听拖动后
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
//开启计时器
[self starTimer];
}
//开启计时器
-(void)starTimer
{
self.timer = [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
}
//结束计时器
-(void)overTimer
{
[self.timer invalidate];
self.timer=nil;
}
//轮播到下一页,自动播放才调用
-(void)nextPage
{
//设置页码
self.pageContol.currentPage = (++self.pageContol.currentPage)%(IMGCOUNT);
//图片偏移
CGFloat offX =self.scrollView.frame.size.width*self.pageContol.currentPage;
[self.scrollView setContentOffset:CGPointMake(offX, 0) animated:NO];
}
@end
作者: 巧克黑力 时间: 2015-8-12 00:35
这么晚了还不睡
作者: 余丽丽 时间: 2015-8-12 21:17
你不是更晚?
作者: 巧克黑力 时间: 2015-8-12 21:39
想想人生,想想未来,就睡不着了,
作者: xiaoguang 时间: 2015-8-13 21:19
观摩一下
作者: 张恒宇 时间: 2015-8-13 22:35
这样做有什么实际的意义吗。。。深深的觉得不如用cocostudio做个json文件导入Xcode中比这个要使用,,,
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) |
黑马程序员IT技术论坛 X3.2 |