01.轮播图之一 :scrollView 轮播

  接触的每个项目,都会用到轮播图的部分,轮播图都写了好多次,用过各种各样的方式写:

  这篇总结的博客,我将分为几个篇幅写,希望写完这几篇博客之后,我能总结出自己写这个轮播的优缺和不同之处

  1.  scrollview 制作轮播
  2.  tableview 制作轮播
  3.  collectionview 制作轮播
  4. 三个imageview 制作轮播,两个imageView 制作轮播
  5. 一个imageView 制作轮播
  6. 设计轮播计时器 和 总结代码的优缺点(希望顺利写到这篇博客)

  介绍完篇幅了,现在进入本篇的主题,用scrollview 制作 轮播;

  使用的还是那个很老套的思路,就是填补最初一张和最后一张 图片,向后滚动到最后一张的时候,设置成第二张的,向左滚动到第一张的时候,

  设置成倒数第二张;逻辑图 就是这样了:::::

2 0 1 2 0

  总之就是这样的吧:

  顺利的谱写代码--- 写代码的艺术家 v^v

 1.创建一个view 作为轮播图封装 ----ScrollViewShuffling

@interface ScrollViewShuffling : UIView

// 这个是滚动视图 传入的数组 @property (nonatomic,strong)NSArray
*array; @end

下面就是具体实现了,重中之重:::::::

@interface ScrollViewShuffling ()<UIScrollViewDelegate>

@property (nonatomic,strong)UIScrollView *scrollView;
@property (nonatomic,strong)NSMutableArray *scrollArray;

@end

@implementation ScrollViewShuffling
@synthesize array = _array;

-(instancetype)initWithFrame:(CGRect)frame{

    if (self == [super initWithFrame:frame]) {
    }
    return self;
}

-(UIScrollView*)scrollView{

    if (_scrollView == nil) {
        CGRect scrollRect = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
        _scrollView = [[UIScrollView alloc]initWithFrame:scrollRect];
        [self addSubview:_scrollView];
        _scrollView.pagingEnabled = YES;
        _scrollView.delegate = self;
    }
    return _scrollView;
}

-(void)setArray:(NSArray *)array{

    NSAssert(array.count != 0, @"传入的滚动数组是 空的");
    _array = array;
    [self prepareData];
    [self initImageViews];
}

-(void)prepareData{

    self.scrollArray = [NSMutableArray new];
//    首位 添加数组最后的元素
    [self.scrollArray addObject:_array.lastObject];
//   添加数组元素
    [self.scrollArray addObjectsFromArray:_array];
//    末尾 补充第一个元素
    [self.scrollArray addObject:_array.firstObject];
}

-(void)initImageViews{

    self.scrollView.contentSize = CGSizeMake(self.scrollArray.count *self.frame.size.width, 0);
    self.scrollView.contentOffset = CGPointMake(self.frame.size.width, 0) ;
    
    for (int index = 0; index < self.scrollArray.count; index++) {
        
        CGRect imageRect = CGRectMake(index*self.frame.size.width, 0, self.frame.size.width, self.frame.size.height);
        UIImageView  *imageView = [[UIImageView alloc]initWithFrame:imageRect];
//        imageView.image =
        imageView.backgroundColor = (UIColor*)self.scrollArray[index];
        [self.scrollView addSubview:imageView];
    }
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    if (scrollView == self.scrollView) {
        //检测移动的位移
        if (scrollView.contentOffset.x == (self.scrollArray.count-1) * self.frame.size.width ) {
           
            CGPoint startPoint = CGPointMake(self.frame.size.width, 0);
            [self.scrollView setContentOffset:startPoint animated:false];
            
        }else if (scrollView.contentOffset.x == 0){
            
            CGPoint endPoint = CGPointMake((self.scrollArray.count-2) * self.frame.size.width,0);
            [self.scrollView setContentOffset:endPoint animated:false];
        }else{
//            正常的滚动
        }
    }
}

现在实现了滚动的基本功能。

  1. 一定要设置scollview 的contensize和 contentoffset
  2. 设置scollview 的代理
  3. 数组asset 这个是考虑传空值,才加入的,如果有更好的方法,可以换掉吧

外界调用::::

/*
引入头文件
创建 + 赋值
*/
-(void)prepareScrollShuffling{

    ScrollViewShuffling *scrollShuffling = [[ScrollViewShuffling alloc]initWithFrame:CGRectMake(10, 20, self.view.frame.size.width-20, 100)];
    
    [self.view addSubview:scrollShuffling];;
    scrollShuffling.array = self.arr;
}

 

scollview 就先写到这里好了,待续………………

 

posted @ 2017-04-26 16:23  user_bo  阅读(304)  评论(0编辑  收藏  举报