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

本文通过递归方式实现了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)];

    

}

 

转载于:https://www.cnblogs.com/iOS-OC/p/5515700.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值