码迷,mamicode.com
首页 > 其他好文 > 详细

利用递归 实现UIScrollView无限滚动的效果

时间:2016-05-21 23:04:58      阅读:356      评论:0      收藏:0      [点我收藏+]

标签:

项目需求

利用递归 实现UIScrollView无限滚动的效果。

 

上机试题, 

#import "ViewController.h"

 

 @interface ViewController (){

 UIScrollView *mainScroll;

 BOOL isFinish;

 int x;

 }

 

 @end

 

 @implementation ViewController

 

 

 - (void)viewDidLoad {

 [super viewDidLoad];

 

 x=0;

 isFinish = YES;

 

 mainScroll = [[UIScrollView alloc]init];

 mainScroll.frame = self.view.bounds;

 mainScroll.contentSize = CGSizeMake(self.view.bounds.size.width*8, self.view.bounds.size.height);

 

 for(int i = 0; i < 8; i++){

 UIImageView *imgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",i+1]]];

 imgView.frame = CGRectMake(self.view.bounds.size.width*i, 0, self.view.bounds.size.width, self.view.bounds.size.height);

 [mainScroll addSubview:imgView];

 }

 [self.view addSubview:mainScroll];

 [self moveScroll];

 

 }

 

 //No.1

 //开始写代码,实现八张图片连续滚动到最后一张之后按相反的顺序再滚回到第一张,循环滚动不结束。

 -(void)moveScroll

{

 

 

 

 

}

 

 

 

 

 

 

 //end_code

 

 

 - (void)didReceiveMemoryWarning {

 [super didReceiveMemoryWarning];

 }

 

 @end

 

 

 

答案:

//No.1

//开始写代码,实现八张图片连续滚动到最后一张之后按相反的顺序再滚回到第一张,循环滚动不结束。

-(void)moveScroll{

    [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(move) userInfo:nil repeats:YES];

}

 

static int count = -1;

-(void)move

{

    x = mainScroll.contentOffset.x/self.view.bounds.size.width;

    if (x == 0 || x == 7) {

        count = -count;

    }

    x = x +count;

    [mainScroll setContentOffset:CGPointMake(x*self.view.bounds.size.width, 0)];

    

}

 

利用递归 实现UIScrollView无限滚动的效果

标签:

原文地址:http://www.cnblogs.com/iOS-OC/p/5515700.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!