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

项目需求

利用递归 实现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)];

    

}

 

posted @ 2016-05-21 21:56  雄鹰和猪  阅读(211)  评论(0)    收藏  举报