美文网首页iOS学习专题iOS进阶之路小知识点
iOS 实现UIScrollView分页滑动宽度自定义

iOS 实现UIScrollView分页滑动宽度自定义

作者: Mr_Lucifer | 来源:发表于2017-01-02 11:23 被阅读4816次

前言

App中最常用 轮播图, 关于它的实现有很多方法 如 :** Anination, UIScrollView, UICollectionView .** 动画是另一种思路, UICollectionView 继承于 UIScrollView. 作者今天就用 UIScrollView 讲一下 , 分页效果下 滑动宽度小于屏幕宽度 露出上下页内容, 或 滑动视图之间 间隙问题 . 如图 :

分页效果下 滑动宽度小于屏幕宽度 露出上下页内容 滑动视图之间 间隙

一 思路整理

有些文章写过 实现方法, 是把 pagingEnabled 设为NO, 自己写出 分页效果. 这么写也可以. 作者更喜欢使用 系统的方法. 实现该效果, 有以下核心代码 :

  1. pagingEnabled 设为YES, 用系统方法实现.
  2. clipsToBounds 设为NO, 为了显现上下页内容
  3. 分页滑动宽度 系统默认为 UIScrollViewwidth.
  4. 算法公式 : **(2 * i+ 1) *b + i * a ** => 说明 : b : 图片间距一半, a : 图片宽

二 封装控件 RollView .h



#import <UIKit/UIKit.h>

/** 设置代理 */
@protocol RollViewDelegate <NSObject>

-(void)didSelectPicWithIndexPath:(NSInteger)index;
@end


@interface RollView : UIView

@property (nonatomic, assign) id<RollViewDelegate> delegate;

/**
 初始化
 
 @param frame 设置View大小
 @param distance 设置Scroll距离View两侧距离
 @param gap 设置Scroll内部 图片间距
 @return 初始化返回值
 */
- (instancetype)initWithFrame:(CGRect)frame withDistanceForScroll:(float)distance withGap:(float)gap;

/** 滚动视图数据 */
-(void)rollView:(NSArray *)dataArr;

@end

三 封装控件 RollView .m


#import "RollView.h"

@interface RollView ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) NSArray *rollDataArr;   // 图片数据

@property (nonatomic, assign) float halfGap;   // 图片间距的一半

@end

@implementation RollView

- (instancetype)initWithFrame:(CGRect)frame withDistanceForScroll:(float)distance withGap:(float)gap
{
    
    self = [super initWithFrame:frame];
    if (self) {
        
        self.halfGap = gap / 2;
        
        /** 设置 UIScrollView */
        self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(distance, 0, self.frame.size.width - 2 * distance, self.frame.size.height)];
        [self addSubview:self.scrollView];
        self.scrollView.pagingEnabled = YES;
        self.scrollView.delegate = self;
        
        self.scrollView.clipsToBounds = NO;
        
        /** 添加手势 */
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        tap.numberOfTapsRequired = 1;
        tap.numberOfTouchesRequired = 1;
        [self.scrollView addGestureRecognizer:tap];
        self.scrollView.showsHorizontalScrollIndicator = NO;
        
        /** 数据初始化 */
        self.rollDataArr = [NSArray array];
        
    }
    
    
    return self;
}

#pragma mark - 视图数据
-(void)rollView:(NSArray *)dataArr{

    self.rollDataArr = dataArr;
    
    
    //循环创建添加轮播图片, 前后各添加一张
    for (int i = 0; i < self.rollDataArr.count + 2; i++) {
        
        for (UIView *underView in self.scrollView.subviews) {
            
            if (underView.tag == 400 + i) {
                [underView removeFromSuperview];
            }
        }
        
        UIImageView *picImageView = [[UIImageView alloc] init];
        picImageView.userInteractionEnabled = YES;
        picImageView.tag = 400 + i ;

        /**  说明
         *   1. 设置完 ScrollView的width, 那么分页的宽也为 width.
         *   2. 图片宽为a 间距为 gap, 那么 图片应该在ScrollView上居中, 距离ScrollView左右间距为halfGap.
         *   与 ScrollView的width关系为 width = halfGap + a + halfGap.
         *   3. distance : Scroll距离 底层视图View两侧距离.  
         *   假设 要露出上下页内容大小为 m ,   distance = m + halfGap
         *
         *  图片位置对应关系 :
         *  0 ->  1 * halfGap ;
         *  1 ->  3 * halfGap + a ;
         *  2 ->  5 * halfGap + 2 * a ;
              .
              .
         *  i   -> (2 * i +1) *  halfGap + i *(width - 2 * halfGap )
         */


        picImageView.frame = CGRectMake((2 * i + 1) * self.halfGap + i * (self.scrollView.frame.size.width - 2 * self.halfGap), 0, (self.scrollView.frame.size.width - 2 * self.halfGap), self.frame.size.height);
        
        //设置图片
        if (i == 0) {
    
            picImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@", self.rollDataArr[self.rollDataArr.count - 1]]];
            
        }else if (i == self.rollDataArr.count+1) {
            
            picImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@", self.rollDataArr[0]]];
        }else {
            
            picImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@", self.rollDataArr[i - 1]]];
        }
        
        [self.scrollView addSubview:picImageView];
    }
    //设置轮播图当前的显示区域
    self.scrollView.contentOffset = CGPointMake(self.scrollView.frame.size.width, 0);
    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * (self.rollDataArr.count + 2), 0);
    
}

#pragma mark - UIScrollViewDelegate 方法
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
       
    NSInteger curIndex = scrollView.contentOffset.x  / self.scrollView.frame.size.width;
    
    if (curIndex == self.rollDataArr.count + 1) {
        
        scrollView.contentOffset = CGPointMake(self.scrollView.frame.size.width, 0);
    }else if (curIndex == 0){
        
        scrollView.contentOffset = CGPointMake(self.scrollView.frame.size.width * self.rollDataArr.count, 0);
    }
    
}

#pragma mark - 轻拍手势的方法
-(void)tapAction:(UITapGestureRecognizer *)tap{
    
    if ([self.rollDataArr isKindOfClass:[NSArray class]] && (self.rollDataArr.count > 0)) {
        
        [_delegate didSelectPicWithIndexPath:(self.scrollView.contentOffset.x / self.scrollView.frame.size.width)];
    }else{
        
        [_delegate didSelectPicWithIndexPath:-1];
    }
    
}

四 调用


#import "ViewController.h"
#import "RollView.h"

@interface ViewController ()<RollViewDelegate>

@property (nonatomic, strong) RollView *rollView;

@end

-(void)creatPicRollView{
    
    
    
    self.rollView = [[RollView alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, 150) withDistanceForScroll:12.0f withGap:8.0f];
   
    /** 全屏宽滑动 视图之间间隙,  将 Distance 设置为 -12.0f */
   // self.rollView = [[RollView alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, 150) withDistanceForScroll: -12.0f withGap:8.0f];
   // self.rollView.backgroundColor = [UIColor blackColor];
    
    self.rollView.delegate = self;
   
    [self.view addSubview:self.rollView];
    
    
    NSArray *arr = @[@"1.jpg",
                     @"2.jpg",
                     @"3.jpg"];
    
    [self.rollView rollView:arr];
}

#pragma mark - 滚动视图协议
-(void)didSelectPicWithIndexPath:(NSInteger)index{
    
    if (index != -1) {
        
        NSLog(@"%ld", (long)index);
    }
    
}

五 效果

分页效果下 滑动宽度小于屏幕宽度 露出上下页内容 **滑动视图之间有间隙**

以 上 !

相关文章

网友评论

  • 大冲哥:为什么不用collectionview呢
  • 422f52eaab16:怎么控制左右滑动到头部的时候不要无限循环滚动呢?
  • 奔跑吧小蚂蚁:你好,这个思路是我见到最简单的一个,不过作者有一段话你写备注的时候写错了,关于图片位置的对应 即:
    I=0 --->halfGap
    I=1 --->2*halfGap+a
    I=2 --->5*hakfGap+2a
    --------也就是你写的表达式 即:
    picImageView.frame = CGRectMake((2 * i + 1) * self.halfGap + i * (self.scrollView.frame.size.width - 2 * self.halfGap), 0, (self.scrollView.frame.size.width - 2 * self.halfGap), self.frame.size.height);
    Mr_Lucifer:@夏末朵 哦, 你说的对, 确实备注写错了, 已修正.
  • hznil:我要点击每一张图片怎么实现呢?直接给图片添加事件无法响应,大神教我
    Mr_Lucifer:没懂. 我有手势. didSelectPicWithIndexPath.方法可以获取到 点击的每一张图片.
  • ma_yongsong:这个方法只能说是取巧做到了,实际上超出UIScollView的左右两边的那个小部分是不能拖动
    godBlessMe__:- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    if (CGRectContainsPoint(CGRectMake(self.bounds.size.width, 0, self.distance, self.frame.size.height), point)) {
    return self.scrollView;
    }
    return [super hitTest:point withEvent:event];
    }
    响应链 找到当前响应区域把scrollView返回就可以响应了
    Mr_Lucifer:修改一下代码 还是可以做到的. 我的需求也仅是为了 提示用户 左右侧还有视图而已. 如果你的需求是 左右两侧暴露的部分很大, 那需要修改下封装. 或者自己写一个方法了.
  • 笙绳省盛:非常好的封装
  • LD_左岸:我用的CollectionView如果想做出这种效果来该怎么做,请指点
    Mr_Lucifer:@左岸__ 不是轮播, 只是单纯的滑动是吧?
    LD_左岸:@Mr_Lucifer //设置图片
    if (i == 0) {

    picImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@", self.rollDataArr[0]]];

    }else if (i == self.rollDataArr.count+1) {

    picImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@", self.rollDataArr[0]]];
    }else {

    picImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@", self.rollDataArr[i]]];
    }

    [self.scrollView addSubview:picImageView];
    我这样修改了您的代码 不会发生什么问题吧 我想实现的功能比您这个稍微简单一点 只需要从左滑到右 滑到最后一张 再从最后一张滑到第一张即可
    for (int i = 0; i < self.rollDataArr.count ; i++) {

    for (UIView *underView in self.scrollView.subviews) {

    if (underView.tag == 400 + i) {
    [underView removeFromSuperview];
    }
    }

    //设置轮播图当前的显示区域
    self.scrollView.contentOffset = CGPointMake(0, 0);
    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * (self.rollDataArr.count), 0);
    把滚动视图将要停止时的代理方法给去了
    总共修改了以上几个地方
    感谢大神的无私奉献
    Mr_Lucifer:CollectionView 更简单. 设计UICollectionViewFlowLayout 关键代码 1) flowLayout.minimumLineSpacing = 20.0f; 2) flowLayout.itemSize = self.bounds.size; flowLayout.sectionInset = UIEdgeInsetsMake(0, flowLayout.minimumLineSpacing / 2, 0, 0);

    设计 UICollectionView 关键代码 1) [[UICollectionView alloc] initWithFrame:CGRectMake(-flowLayout.minimumLineSpacing / 2, 0, self.bounds.size.width + flowLayout.minimumLineSpacing, self.bounds.size.height) collectionViewLayout:flowLayout]; 大概就这样.
  • 丐帮头:就是我这边按照你这个写了,最后一张图片总是显示比较慢,就是滚动完成之后,最后那张图片才显示出来,显得不流畅,有办法解决吗?
    小多多:@Mr_Lucifer 应该是cell复用的问题
    丐帮头:楼主,有demo吗,我这边uicollectionview还是一样
    Mr_Lucifer: @丐帮头 网速问题? 不知道你怎么写的。常规的 3-123-1这样。如果要再苛刻点用23-123-12这样布局。要完美用 collectionView 来写。关键代码基本相同。
  • 9aa1f8425b9f:程序猿 你好呀
  • 你家毕老师:这个思路厉害了,学习了
  • LambZhou:你好~有Demo 吗?
    Mr_Lucifer:@LambZhou 去掉轮播效果呗? 把前后两种 多加的 图片 去掉, [//设置轮播图当前的显示区域] 偏移量设置为0, 偏移范围 也去掉2;
    LambZhou:如果不要左边露出的那一部分,该怎么改代码?
    Mr_Lucifer:@LambZhou 这就是 所有.

本文标题:iOS 实现UIScrollView分页滑动宽度自定义

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