美文网首页
iOS UIScrollView图片循环

iOS UIScrollView图片循环

作者: 丶绅士丿丨丨 | 来源:发表于2015-12-15 10:58 被阅读78次

    - (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];

       }

    }

    相关文章

      网友评论

          本文标题:iOS UIScrollView图片循环

          本文链接:https://www.haomeiwen.com/subject/iqpuhttx.html