Believe in your own future, will thank yourself right now Sinner Yun

Sinner_Yun

图片循环

@interface ViewController ()<UIScrollViewDelegate>

{

    NSInteger page;

}

@end

 

@implementation ViewController

 

- (void)viewDidLoad

{

    [super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

    

    UIScrollView *sv=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 568)];

    sv.backgroundColor=[UIColor cyanColor];

    sv.contentSize=CGSizeMake(320*6, 568);

    [self.view addSubview:sv];

    

    for (int i=0; i<6; i++)

    {

        if (i==0) {

            UIImageView *iv=[[UIImageView alloc]initWithFrame:CGRectMake(320*i, 0, 320, 568)];

            iv.image=[UIImage imageNamed:[NSString stringWithFormat:@"2_4.jpg"]];

            [sv addSubview:iv];

            iv.userInteractionEnabled=YES;

        }else if (i==5)

        {

            UIImageView *iv=[[UIImageView alloc]initWithFrame:CGRectMake(320*i, 0, 320, 568)];

            iv.image=[UIImage imageNamed:[NSString stringWithFormat:@"2_1.jpg"]];

            [sv addSubview:iv];

        }else {

        UIImageView *iv=[[UIImageView alloc]initWithFrame:CGRectMake(320*i, 0, 320, 568)];

        iv.image=[UIImage imageNamed:[NSString stringWithFormat:@"2_%d.jpg",i]];

        [sv addSubview:iv];

        }

    }

    sv.pagingEnabled=YES;

    sv.delegate=self;

}

 

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{

    page=scrollView.contentOffset.x/320;

    if (!page) {

        [scrollView setContentOffset:CGPointMake(320*4, 0)];

    }

    if (page==5) {

        [scrollView setContentOffset:CGPointMake(320, 0)];

    }

}

 

posted on 2014-03-30 20:57  Sinner_Yun  阅读(201)  评论(0编辑  收藏  举报

导航