demo地址 1: http://download.csdn.net/detail/take8619702/4767432
demo地址 2: http://download.csdn.net/detail/take8619702/4767443
demo地址 3: http://download.csdn.net/detail/take8619702/4767459
1.普通分页滑动
myScrollView = [[UIScrollViewalloc]initWithFrame:CGRectMake(0,0,320,460)];
[myScrollViewsetContentSize:CGSizeMake(pageWidth*3,460)];
[myScrollViewsetBackgroundColor:[UIColorscrollViewTexturedBackgroundColor]];
[myScrollViewsetPagingEnabled:YES];//当此属性设置为YES时,才能自动分页
[self.viewaddSubview:myScrollView];
2.循环滑动
- 实现UIScrollViewDelegate代理: [myScrollViewsetDelegate:self];
- 初始化时,myScrollView 置于中间: [myScrollView setContentOffset:CGPointMake(pageWidth, 0)]
- 滑动结束之后,回到中间: [myScrollViewsetContentOffset:CGPointMake(pageWidth,0)];
- 代理方法:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView.contentOffset.x <&#61;0) {
[scrollView setContentOffset:CGPointMake(pageWidth,0)];
}
if (scrollView.contentOffset.x >&#61;2*pageWidth) {
[scrollView setContentOffset:CGPointMake(pageWidth,0)];
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
[scrollView setContentOffset:CGPointMake(pageWidth,0)animated:YES];
}
3.加上图片查看效果
这两句的位置不能交换
[selfloadScrollViewSubViews];
[myScrollViewsetContentOffset:CGPointMake(pageWidth,0)];
- (void)loadScrollViewSubViews
{
imageView1 &#61; [[UIImageViewalloc]initWithImage:[UIImageimageNamed:&#64;"1.jpg"]];
imageView2 &#61; [[UIImageViewalloc]initWithImage:[UIImageimageNamed:&#64;"2.jpg"]];
imageView3 &#61; [[UIImageViewalloc]initWithImage:[UIImageimageNamed:&#64;"3.jpg"]];
[imageView1 setFrame:CGRectMake( 0,0, pageWidth,460)];
[imageView2 setFrame:CGRectMake( pageWidth,0,pageWidth,460)];
[imageView3 setFrame:CGRectMake(2*pageWidth,0,pageWidth,460)];
[myScrollView addSubview:imageView1];
[myScrollView addSubview:imageView2];
[myScrollView addSubview:imageView3];
}
4.循环效果并未出现&#xff0c;还要加上交换图片的代码
- (void)previousImageViewWithImage
{
UIImage * temp &#61; [imageView1.imageretain];
imageView1.image &#61;imageView2.image;
imageView2.image &#61;imageView3.image;
imageView3.image &#61; temp;
[temp release];
}
- (void)nextImageViewWithImage
{
UIImage * temp &#61; [imageView3.imageretain];
imageView3.image &#61;imageView2.image;
imageView2.image &#61;imageView1.image;
imageView1.image &#61; temp;
[temp release];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView.contentOffset.x <&#61;0) {
currentImageCount--;
[selfpreviousImageViewWithImage];
[scrollView setContentOffset:CGPointMake(pageWidth,0)];
}
if (scrollView.contentOffset.x >&#61;2*pageWidth) {
currentImageCount&#43;&#43;;
[selfnextImageViewWithImage];
[scrollView setContentOffset:CGPointMake(pageWidth,0)];
}
}
5.完美的ScrollView循环 为什么UIImage * temp &#61; [imageView3.image retain];在此处需要retain&#xff0c;如果不加此句&#xff0c;按home键之后&#xff0c;再回到程序滑动&#xff0c;程序会crash。
感谢Eric yang&#xff08;对于此问题的解答&#xff09;
为了显示效果&#xff0c;去掉进度条。
进阶一&#xff1a;
1.知道当前所指向图片
给程序增加计数器&#xff0c;指向当前图片。
int currentImageCount;
初始化为33333331&#xff0c;跟上面的第2点同步。&#xff08;为了取模3的值&#xff0c;所以不是1&#xff0c;而是33331&#xff0c;随便几个3&#xff09;
2.如果向后滑动&#xff0c;currentImageCount&#43;&#43;;
如果向前滑动&#xff0c;currentImageCount--;
3.为了防止回滑多次&#xff0c;乱成图片&#xff0c;程序混乱
所以还需要增加判断&#xff0c;在&#xff1a;
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
if (currentImageCount &#61;&#61; 0) {
currentImageCount &#61; 33333330;
}
4.给myScrollView一个tap事件&#xff0c;点击图片&#xff0c;弹出当前图片计数&#xff08;理论计数&#xff09;
- (void)addTapEventOnMyScrollView
{
tap &#61; [[UITapGestureRecognizeralloc]initWithTarget:selfaction:&#64;selector(tapEvent)];
[myScrollViewaddGestureRecognizer:tap];
}
- (void)tapEvent
{
NSString * strMsg &#61; [NSStringstringWithFormat:&#64;"当前图片理论计数:%i",curentImage];
UIAlertView * alert &#61; [[UIAlertViewalloc]initWithTitle:&#64;"图片计数" message:strMsgdelegate:nilcancelButtonTitle:&#64;"确定"otherButtonTitles:nil];
[alert show];
[alert release];
}
5.增加一个pageControl,直观显示当前滑动
- (void)addMyPageControl
{
myPageControl &#61; [[UIPageControlalloc]initWithFrame:CGRectMake(110,420,100,20)];
[myPageControlsetNumberOfPages:3];
[myPageControl setCurrentPage:curentImage];
[self.viewaddSubview:myPageControl];
}
进阶二&#xff1a;
1.增加定时器&#xff0c;图片自动滑动
- (void)addAutoTimer
{
myAutoTimer &#61; [NSTimerscheduledTimerWithTimeInterval:5.0target:selfselector:&#64;selector(autoChangeScrollView)userInfo:nilrepeats:YES];
}
- (void)autoChangeScrollView
{
[myScrollViewsetContentOffset:CGPointMake(2*pageWidth,0)animated:YES];
}
2.为了pageControl&#xff0c;与tap事件连接&#xff0c;修攺autoChangeScrollView
- (void)autoChangeScrollView
{
[myScrollViewsetContentOffset:CGPointMake(2*pageWidth,0)animated:YES];
}
3.开始手动滑动时&#xff0c;停止定时器&#xff0c;手动滑动结束后停止定时器
- (void)stopAutoTimer
{
if (myAutoTimer) {
[myAutoTimer invalidate];
myAutoTimer &#61; nil;
}
}
- (void)restartAutoTimer
{
if (!myAutoTimer) {
[self addAutoTimer];
}
}
4.如果用户向后滑动&#xff0c;则自动向后滑动。如果用户向前滑动&#xff0c;则自动向前滑动。增加滑动方向布尔
BOOL isGoBefor;
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView.contentOffset.x <&#61;0) {
isGoBefor &#61; YES;
}
if (scrollView.contentOffset.x >&#61;2*pageWidth) {
isGoBefor &#61; NO;
}
}
自动切换函数
- (void)autoChangeScrollView
{
if (isGoBefor) {
[myScrollViewsetContentOffset:CGPointMake(0,0)animated:YES];
} else {
[myScrollViewsetContentOffset:CGPointMake(2*pageWidth,0)animated:YES];
}
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView.contentOffset.x <&#61;0) {
isGoBefor &#61; YES;
currentImageCount--;
curentImage &#61; currentImageCount%3;
//----------------------------------------------------------------------------------------------增加PageControl直观显示当前滑动位置
[myPageControl setCurrentPage:curentImage];
[selfpreviousImageViewWithImage];
[scrollView setContentOffset:CGPointMake(pageWidth,0)];
}
if (scrollView.contentOffset.x >&#61;2*pageWidth) {
isGoBefor &#61; NO;
currentImageCount&#43;&#43;;
curentImage &#61; currentImageCount%3;
//----------------------------------------------------------------------------------------------增加PageControl直观显示当前滑动位置
[myPageControl setCurrentPage:curentImage];
[selfnextImageViewWithImage];
[scrollView setContentOffset:CGPointMake(pageWidth,0)];
}
}