iOS无线滚动系列,轮播图、滚动消息

来源:互联网 发布:1991莫斯科摇滚知乎 编辑:程序博客网 时间:2024/06/14 20:07
类似轮播图,实现无限循环。循环的是一个view,上面放什么东西都可以,比如滚动的消息之类的东西,上面有图片有为类的。  
思想:在最后面一个后面拼接第一个,然后判断滚动式图偏移量已经最大的时候,证明是最后面那个第一个,然后是关键了
将滚动式图的偏移量移动到

[_scrollViewsetContentOffset:CGPointMake(0,0) animated:NO];

注意 animated设为NO,悄悄的移到最开始的位置,不要动画,你可以设为YES试试,滚动会闪

紧接着

[_scrollViewsetContentOffset:CGPointMake(0,_scrollView.frame.size.height)animated:YES];

这个时候 animated 是YES,看起来就像是从第一个,滚动到第二个。

for (int i = 0; i < (self.modelList.count > 1 ? self.modelList.count + 1 : self.modelList.count); i++) {              CGFloat height = self.scrollView.frame.size.height;          CGFloat width = self.scrollView.frame.size.width;                    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];          button.frame = CGRectMake(0, height * i, width, height);          button.tag = TagParam + i;          [self.scrollView addSubview:button];          [button addTarget:self action:@selector(checkMessageDetail:) forControlEvents:UIControlEventTouchUpInside];                    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 15, 45, 45)];          imageView.backgroundColor = BackGround_Color;          [button addSubview:imageView];                    UILabel *titleLabel = [UILabel labelShortWithColor:Color_666666 font:17];          [button addSubview:titleLabel];          [titleLabel makeConstraints:^(MASConstraintMaker *make) {              make.left.equalTo(imageView.right).offset(20);              make.top.equalTo(button.top).offset(20);              make.right.equalTo(button.right).offset(-20);          }];                    UILabel *contentLabel = [UILabel labelShortWithColor:Color_999999 font:12];          [button addSubview:contentLabel];          [contentLabel makeConstraints:^(MASConstraintMaker *make) {              make.left.right.equalTo(titleLabel);              make.bottom.equalTo(button.bottom).offset(-20);          }];                    VHomeSystemMessageModel *model ;          if (i == self.modelList.count) {              model = [self.modelList firstObject];              imageView.image = vImage(@"");              titleLabel.text = model.title;              contentLabel.text = model.content;          }else {              model = self.modelList[i];;              imageView.image = vImage(@"");              titleLabel.text = model.title;              contentLabel.text = model.content;          }                }            _timer = [NSTimer timerWithTimeInterval:2 target:self selector:@selector(doCycleScroll) userInfo:nil repeats:YES];      [[NSRunLoop mainRunLoop]addTimer:_timer forMode:NSRunLoopCommonModes];            //当数组里面的数据大于1的时候,才会滚动      if (self.modelList.count > 1) {          [_timer fire];      }      _scrollView.contentOffset = CGPointMake(0, 0);
//上下滚动
-(void)doCycleScroll{    CGFloat offsety = _scrollView.contentOffset.y;    offsety += _scrollView.frame.size.height;    if (offsety > _scrollView.contentSize.height) {        [_scrollView setContentOffset:CGPointMake(0, 0) animated:NO];        [_scrollView setContentOffset:CGPointMake(0, _scrollView.frame.size.height) animated:YES];    }else{        [_scrollView setContentOffset:CGPointMake(0, offsety) animated:YES];    }}





0 0