iOS 轮播图
2015-06-23 11:50
218 查看
- (void)viewDidLoad {
[super
viewDidLoad];
self.view.backgroundColor = [UIColor
blackColor];
self.scrollView = [[UIScrollView
alloc] initWithFrame:CGRectMake(0, Screen_Height /
2 - 100, Screen_Width,
200)];
// self.scrollView.backgroundColor = [UIColor redColor];
self.scrollView.delegate =
self;
[self.view
addSubview:_scrollView];
//
内容大小
self.scrollView.contentSize =
CGSizeMake(Screen_Width * 8,
64);
//
启用分页
self.scrollView.pagingEnabled =
YES;
//
显示水平滚动指标
self.scrollView.showsHorizontalScrollIndicator =
NO;
//
设置内容抵消
[self.scrollView
setContentOffset:CGPointMake(Screen_Width,
0) animated:YES];
self.automaticallyAdjustsScrollViewInsets =
NO;
self.pageVC = [[UIPageControl
alloc] initWithFrame:CGRectMake(Screen_Width /
7, self.scrollView.frame.origin.y
+ self.scrollView.frame.size.height -
30, Screen_Width /
1.5,
20)];
// self.pageVC.currentPage = 0;
self.pageVC.pageIndicatorTintColor = [UIColor
lightGrayColor];
self.pageVC.currentPageIndicatorTintColor = [UIColor
blueColor];
self.pageVC.numberOfPages =
6;
self.array = [NSMutableArray
arrayWithObjects:@"img00@2x.png",@"img01@2x.png",
@"img02@2x.png",@"img03@2x.png",@"img04@2x.png",@"img05@2x.png",nil];
//
设置 的
第一张 图片
id firstImage = [self.array
firstObject];
//
设置 的
最后 一张
图片
id lastImage = [self.array
lastObject];
//
把第一张图片 加在
数组中
[self.array
insertObject:lastImage atIndex:0];
[self.array
addObject:firstImage];
for (int i =
0; i < 8; i++) {
// self.scro = [[UIScrollView alloc] initWithFrame:CGRectMake(Screen_Width * i, 0, Screen_Width, Screen_Height)];
//
//
//
// self.scro.delegate = self;
//
//// self.scro.center = CGPointMake(self.scrollView.frame.size.width / 2, self.scrollView.frame.size.height / 2);
//
// //设置最大伸缩比例
// self.scro.maximumZoomScale=2.0;
// //设置最小伸缩比例
// self.scro.minimumZoomScale=0.5;
//
//
// [self.view addSubview:self.scro];
self.homeImageView = [[UIImageView
alloc] initWithFrame:CGRectMake(Screen_Width * i,
0, Screen_Width, Screen_Height /
3)];
self.homeImageView.image = [UIImage
imageNamed:@"himages_01.png"];
[self.scrollView
addSubview:self.homeImageView];
}
[self.view
addSubview:self.pageVC];
[self
addtimer];
}
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return
self.homeImageView;
}
-(NSUInteger)supportedInterfaceOrientations{
return
UIInterfaceOrientationMaskLandscapeRight;
}
- (void)addtimer
{
self.timer = [NSTimer
scheduledTimerWithTimeInterval:3.5
target:self
selector:@selector(timerAction)
userInfo:nil
repeats:YES];
}
- (void)timerAction
{
CGFloat x = (self.myIndex +
1) *
self.scrollView.frame.size.width;
[self.scrollView
setContentOffset:CGPointMake(x,
0) animated:YES];
if (self.myIndex ==
7) {
CGFloat x =
1 *
self.scrollView.frame.size.width;
[self.scrollView
setContentOffset:CGPointMake(x,
0) animated:NO];
self.myIndex = -1;
}
self.myIndex ++;
}
//滑动结束
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
if (self.scrollView.contentOffset.x
>= self.scrollView.frame.size.width * (8
- 1)) {
//
显示用户看到的第一页 也就是数组中的第二张
[self.scrollView
setContentOffset:CGPointMake(self.scrollView.frame.size.width
- 1,
0) animated:NO];
}
if (self.scrollView.contentOffset.x
<= 0) {
//
如果当前偏移量小于等于0的时候
[self.scrollView
setContentOffset:CGPointMake(self.scrollView.frame.size.width
* (8 -
2), 0)
animated:NO];
}
}
//将要开始拖缀
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[self
removeTimer];
}
// 结束拖缀
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
if (self.timer ==
nil) {
[self
addtimer];
}
}
// 滑动时
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//
当页面页面滑动一半的时候 就改变页数
int X = scrollView.frame.size.width/2;
NSInteger index = (scrollView.contentOffset.x -
self.scrollView.frame.size.width - X) / (scrollView.frame.size.width)
+ 1;
self.pageVC.currentPage = index;
self.myIndex =
self.pageVC.currentPage;
if (index == 6) {
[self.pageVC
setCurrentPage:0];
self.myIndex =
7;
}
}
-(void)removeTimer
{
[self.timer
invalidate];
self.timer =
nil;
}
[super
viewDidLoad];
self.view.backgroundColor = [UIColor
blackColor];
self.scrollView = [[UIScrollView
alloc] initWithFrame:CGRectMake(0, Screen_Height /
2 - 100, Screen_Width,
200)];
// self.scrollView.backgroundColor = [UIColor redColor];
self.scrollView.delegate =
self;
[self.view
addSubview:_scrollView];
//
内容大小
self.scrollView.contentSize =
CGSizeMake(Screen_Width * 8,
64);
//
启用分页
self.scrollView.pagingEnabled =
YES;
//
显示水平滚动指标
self.scrollView.showsHorizontalScrollIndicator =
NO;
//
设置内容抵消
[self.scrollView
setContentOffset:CGPointMake(Screen_Width,
0) animated:YES];
self.automaticallyAdjustsScrollViewInsets =
NO;
self.pageVC = [[UIPageControl
alloc] initWithFrame:CGRectMake(Screen_Width /
7, self.scrollView.frame.origin.y
+ self.scrollView.frame.size.height -
30, Screen_Width /
1.5,
20)];
// self.pageVC.currentPage = 0;
self.pageVC.pageIndicatorTintColor = [UIColor
lightGrayColor];
self.pageVC.currentPageIndicatorTintColor = [UIColor
blueColor];
self.pageVC.numberOfPages =
6;
self.array = [NSMutableArray
arrayWithObjects:@"img00@2x.png",@"img01@2x.png",
@"img02@2x.png",@"img03@2x.png",@"img04@2x.png",@"img05@2x.png",nil];
//
设置 的
第一张 图片
id firstImage = [self.array
firstObject];
//
设置 的
最后 一张
图片
id lastImage = [self.array
lastObject];
//
把第一张图片 加在
数组中
[self.array
insertObject:lastImage atIndex:0];
[self.array
addObject:firstImage];
for (int i =
0; i < 8; i++) {
// self.scro = [[UIScrollView alloc] initWithFrame:CGRectMake(Screen_Width * i, 0, Screen_Width, Screen_Height)];
//
//
//
// self.scro.delegate = self;
//
//// self.scro.center = CGPointMake(self.scrollView.frame.size.width / 2, self.scrollView.frame.size.height / 2);
//
// //设置最大伸缩比例
// self.scro.maximumZoomScale=2.0;
// //设置最小伸缩比例
// self.scro.minimumZoomScale=0.5;
//
//
// [self.view addSubview:self.scro];
self.homeImageView = [[UIImageView
alloc] initWithFrame:CGRectMake(Screen_Width * i,
0, Screen_Width, Screen_Height /
3)];
self.homeImageView.image = [UIImage
imageNamed:@"himages_01.png"];
[self.scrollView
addSubview:self.homeImageView];
}
[self.view
addSubview:self.pageVC];
[self
addtimer];
}
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return
self.homeImageView;
}
-(NSUInteger)supportedInterfaceOrientations{
return
UIInterfaceOrientationMaskLandscapeRight;
}
- (void)addtimer
{
self.timer = [NSTimer
scheduledTimerWithTimeInterval:3.5
target:self
selector:@selector(timerAction)
userInfo:nil
repeats:YES];
}
- (void)timerAction
{
CGFloat x = (self.myIndex +
1) *
self.scrollView.frame.size.width;
[self.scrollView
setContentOffset:CGPointMake(x,
0) animated:YES];
if (self.myIndex ==
7) {
CGFloat x =
1 *
self.scrollView.frame.size.width;
[self.scrollView
setContentOffset:CGPointMake(x,
0) animated:NO];
self.myIndex = -1;
}
self.myIndex ++;
}
//滑动结束
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
if (self.scrollView.contentOffset.x
>= self.scrollView.frame.size.width * (8
- 1)) {
//
显示用户看到的第一页 也就是数组中的第二张
[self.scrollView
setContentOffset:CGPointMake(self.scrollView.frame.size.width
- 1,
0) animated:NO];
}
if (self.scrollView.contentOffset.x
<= 0) {
//
如果当前偏移量小于等于0的时候
[self.scrollView
setContentOffset:CGPointMake(self.scrollView.frame.size.width
* (8 -
2), 0)
animated:NO];
}
}
//将要开始拖缀
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[self
removeTimer];
}
// 结束拖缀
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
if (self.timer ==
nil) {
[self
addtimer];
}
}
// 滑动时
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//
当页面页面滑动一半的时候 就改变页数
int X = scrollView.frame.size.width/2;
NSInteger index = (scrollView.contentOffset.x -
self.scrollView.frame.size.width - X) / (scrollView.frame.size.width)
+ 1;
self.pageVC.currentPage = index;
self.myIndex =
self.pageVC.currentPage;
if (index == 6) {
[self.pageVC
setCurrentPage:0];
self.myIndex =
7;
}
}
-(void)removeTimer
{
[self.timer
invalidate];
self.timer =
nil;
}
相关文章推荐
- iOS开发- 文件共享(利用iTunes导入文件, 并且显示已有文件)
- iOS之NSXMLParser对XML解析
- iOS开发SDWebImage使用详解
- iOS支持arm64后,按钮图片出不来的坑
- KVC 与 KVO的概念理解
- iOS 开发学习35 本地化
- iOS view的frame和bounds之区别(位置和大小)
- Xamarin iOS教程之页面控件
- [ios]iOS8 定位
- Xamarin iOS教程之页面控件
- iOS第三方支付集成
- iOS越狱后必装软件
- iOS多线程
- iOS(使用pch文件导入头文件方法)
- 构建iOS交叉编译环境
- iOS开发笔记6:图片轮播及其无限循环效果
- “自释放”在iOS开发中的应用
- iOS类的继承关系
- iOS view的frame和bounds之区别(位置和大小)
- bios基本原理