iOS UIScrollView图片循环

- (void)viewDidLoad

{

   [super viewDidLoad];

if ([[[UIDevice currentDevice] systemVersion]floatValue]>=7) {

self.automaticallyAdjustsScrollViewInsets=NO;

   }

self.scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 20, 320, 400)];

self.scrollView.pagingEnabled=YES;

self.scrollView.delegate=self;

self.scrollView.showsHorizontalScrollIndicator=NO;

self.scrollView.showsVerticalScrollIndicator=NO;

   [self.view addSubview:self.scrollView];

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

UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(320*i, 0, 320, self.scrollView.frame.size.height)];

       imageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"image%d",i]];

       [self.scrollView addSubview:imageView];

UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 20)];

       label.text=[NSString stringWithFormat:@"%d",i];

       label.center=imageView.center;

       [imageView addSubview:label];

   }

   [self.scrollView setContentSize:CGSizeMake(320*6, 400)];

   [self.scrollView setContentOffset:CGPointMake(320, 0) animated:NO];

}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView

{

   CGPoint point=scrollView.contentOffset;

   if ((int)point.x==0) {

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

   }else if ((int)point.x==320*5)

   {

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

   }

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容