UIScrollView代码实现循环滚动
2012-11-20 16:46
525 查看
iPhone开发应用中循环滚动一个UIScrollView代码实现是本文要介绍的内容,主要是介绍UIScrollView来实现循环滚动的案例,来看详细内容。
// testScrollViewViewController.m
// testScrollView
// Created by cash on 11-7-4.
// Copyright 2011年 xbiii3s@gmail.com. All rights reserved.
#import "testScrollViewViewController.h"
@implementation testScrollViewViewController
@synthesize scrollView, slideImages;
#define WIDTH_OF_SCROLL_PAGE 320
#define HEIGHT_OF_SCROLL_PAGE 460
#define WIDTH_OF_IMAGE 320 #define HEIGHT_OF_IMAGE 460
#define LEFT_EDGE_OFSET 0
- (void)viewDidLoad {
scrollView = [[UIScrollView alloc] init];
CGRect scrollFrame;
scrollFrame.origin.x = 0;
scrollFrame.origin.y = 0;
scrollFrame.size.width = WIDTH_OF_SCROLL_PAGE;
scrollFrame.size.height = HEIGHT_OF_SCROLL_PAGE;
scrollView = [[UIScrollView alloc] initWithFrame:scrollFrame];
scrollView.bounces = YES;
scrollView.pagingEnabled = YES;
scrollView.delegate = self;
scrollView.userInteractionEnabled = YES;
slideImages = [[NSMutableArray alloc] init];
[slideImages addObject:@"IMG_0116.PNG"];
[slideImages addObject:@"IMG_0118.PNG"];
[slideImages addObject:@"IMG_0119.PNG"];
[slideImages addObject:@"main_bg.png"];
//add the last image first
UIImageView *imageView = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:[slideImages objectAtIndex:([slideImages count]-1)]]]
imageView.frame = CGRectMake(LEFT_EDGE_OFSET, 0, WIDTH_OF_IMAGE, HEIGHT_OF_IMAGE);
[scrollView addSubview:imageView];
[imageView release];
for (int i = 0;i<[slideImages count];i++) {
//loop this bit
UIImageView *imageView = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:[slideImages objectAtIndex:i]]];
imageView.frame = CGRectMake((WIDTH_OF_IMAGE * i) + LEFT_EDGE_OFSET + 320, 0, WIDTH_OF_IMAGE, HEIGHT_OF_IMAGE);
[scrollView addSubview:imageView];
[imageView release];
}
//add the first image at the end
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[slideImages objectAtIndex:0]]];
imageView.frame = CGRectMake((WIDTH_OF_IMAGE * ([slideImages count] + 1)) + LEFT_EDGE_OFSET, 0, WIDTH_OF_IMAGE, HEIGHT_OF_IMAGE);
[scrollView addSubview:imageView];
[imageView release];
[scrollView setContentSize:CGSizeMake(WIDTH_OF_SCROLL_PAGE * ([slideImages count] + 2), HEIGHT_OF_IMAGE)];
[scrollView setContentOffset:CGPointMake(0, 0)];
[self.view addSubview:scrollView];
[self.scrollView scrollRectToVisible:CGRectMake(WIDTH_OF_IMAGE,0,WIDTH_OF_IMAGE,HEIGHT_OF_IMAGE) animated:NO];
[super viewDidLoad];} - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
int currentPage = floor((self.scrollView.contentOffset.x - self.scrollView.frame.size.width
/ ([slideImages count]+2)) / self.scrollView.frame.size.width) + 1;
if (currentPage==0) {
//go last but 1 page
[self.scrollView scrollRectToVisible:CGRectMake(WIDTH_OF_IMAGE * [slideImages count],0,WIDTH_OF_IMAGE,HEIGHT_OF_IMAGE) animated:NO];
} else
if (currentPage==([slideImages count]+1)) {
//如果是最后+1,也就是要开始循环的第一个
[self.scrollView scrollRectToVisible:CGRectMake(WIDTH_OF_IMAGE,0,WIDTH_OF_IMAGE,HEIGHT_OF_IMAGE) animated:NO];
}
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[scrollView release];
[slideImages release];
[super dealloc];
}
@end
// testScrollViewViewController.m
// testScrollView
// Created by cash on 11-7-4.
// Copyright 2011年 xbiii3s@gmail.com. All rights reserved.
#import "testScrollViewViewController.h"
@implementation testScrollViewViewController
@synthesize scrollView, slideImages;
#define WIDTH_OF_SCROLL_PAGE 320
#define HEIGHT_OF_SCROLL_PAGE 460
#define WIDTH_OF_IMAGE 320 #define HEIGHT_OF_IMAGE 460
#define LEFT_EDGE_OFSET 0
- (void)viewDidLoad {
scrollView = [[UIScrollView alloc] init];
CGRect scrollFrame;
scrollFrame.origin.x = 0;
scrollFrame.origin.y = 0;
scrollFrame.size.width = WIDTH_OF_SCROLL_PAGE;
scrollFrame.size.height = HEIGHT_OF_SCROLL_PAGE;
scrollView = [[UIScrollView alloc] initWithFrame:scrollFrame];
scrollView.bounces = YES;
scrollView.pagingEnabled = YES;
scrollView.delegate = self;
scrollView.userInteractionEnabled = YES;
slideImages = [[NSMutableArray alloc] init];
[slideImages addObject:@"IMG_0116.PNG"];
[slideImages addObject:@"IMG_0118.PNG"];
[slideImages addObject:@"IMG_0119.PNG"];
[slideImages addObject:@"main_bg.png"];
//add the last image first
UIImageView *imageView = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:[slideImages objectAtIndex:([slideImages count]-1)]]]
imageView.frame = CGRectMake(LEFT_EDGE_OFSET, 0, WIDTH_OF_IMAGE, HEIGHT_OF_IMAGE);
[scrollView addSubview:imageView];
[imageView release];
for (int i = 0;i<[slideImages count];i++) {
//loop this bit
UIImageView *imageView = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:[slideImages objectAtIndex:i]]];
imageView.frame = CGRectMake((WIDTH_OF_IMAGE * i) + LEFT_EDGE_OFSET + 320, 0, WIDTH_OF_IMAGE, HEIGHT_OF_IMAGE);
[scrollView addSubview:imageView];
[imageView release];
}
//add the first image at the end
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[slideImages objectAtIndex:0]]];
imageView.frame = CGRectMake((WIDTH_OF_IMAGE * ([slideImages count] + 1)) + LEFT_EDGE_OFSET, 0, WIDTH_OF_IMAGE, HEIGHT_OF_IMAGE);
[scrollView addSubview:imageView];
[imageView release];
[scrollView setContentSize:CGSizeMake(WIDTH_OF_SCROLL_PAGE * ([slideImages count] + 2), HEIGHT_OF_IMAGE)];
[scrollView setContentOffset:CGPointMake(0, 0)];
[self.view addSubview:scrollView];
[self.scrollView scrollRectToVisible:CGRectMake(WIDTH_OF_IMAGE,0,WIDTH_OF_IMAGE,HEIGHT_OF_IMAGE) animated:NO];
[super viewDidLoad];} - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
int currentPage = floor((self.scrollView.contentOffset.x - self.scrollView.frame.size.width
/ ([slideImages count]+2)) / self.scrollView.frame.size.width) + 1;
if (currentPage==0) {
//go last but 1 page
[self.scrollView scrollRectToVisible:CGRectMake(WIDTH_OF_IMAGE * [slideImages count],0,WIDTH_OF_IMAGE,HEIGHT_OF_IMAGE) animated:NO];
} else
if (currentPage==([slideImages count]+1)) {
//如果是最后+1,也就是要开始循环的第一个
[self.scrollView scrollRectToVisible:CGRectMake(WIDTH_OF_IMAGE,0,WIDTH_OF_IMAGE,HEIGHT_OF_IMAGE) animated:NO];
}
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[scrollView release];
[slideImages release];
[super dealloc];
}
@end
相关文章推荐
- UIScrollView代码实现循环滚动
- UIScrollView代码实现循环滚动
- [转]iPhone开发应用中UIScrollView代码实现循环滚动
- iPhone开发应用中UIScrollView代码实现循环滚动
- UIScrollView代码实现循环滚动
- iOS开发学习--纯代码 UIScrollView 无限循环的实现——代码类封装
- iOS之UIScrollView实现无限滚动,思路与代码
- UIScrollView实现循环滚动和自动滚动
- 实现UIScrollView的循环滚动
- 利用UIScrollView实现循环滚动翻页的方法
- iOS_UIScrollView实现无限滚动,思路与代码
- 用UIScrollView实现相册,并设置滚动条消失,改变UIPageControl的颜色,实现循环滚动相册
- iOS UIImageView利用UIScrollView实现图片循环滚动
- 实现UIScrollView循环滚动的三种方法
- iOS UIScrollView实现无限滚动,思路与代码
- iPhone开发之UIScrollView滚动组件的使用——纯代码实现图形的横向与纵向滚动
- UIScrollView实现循环滚动---快捷方式
- UIScrollView自动滚动 循环滚动视图实现
- 实现UIScrollView循环滚动的三种方法
- iOS UISCrollView循环滚动 实现