美文网首页首页推荐
iOS类似淘宝头条的文字滚动广告

iOS类似淘宝头条的文字滚动广告

作者: 傅hc | 来源:发表于2018-09-25 09:20 被阅读17次

    最近两个项目都用到类似淘宝头条的功能,就是文字上下无限循环滚动,所以决定简单封装一下,以便以后使用。由于习惯用SDCycleScrollView来做广告图的无限循环滚动,所以这里文字无限滚动的核心是源于此。

    效果如下:


    demo效果

    考虑到这种滚动需求样式的多变性,这里的封装还是基于 UICollectionView,提供上下和左右滚动两种方式,而且基于UICollectionView的复用特性,性能也是不错的。如果样式不一样,直接替换FHCTopLineCollectionViewCell为你自定义的cell即可。

    FHCTopLineViewDelegate提供了两个代理方法

    /**
     点击滚动文字回调
     @param topLine FHCTopLineView
     @param index 选中文字的index
     */
    - (void)topLineView:(FHCTopLineView *)topLine didSelectItemAtIndex:(NSInteger)index;
    
    
    /**
     文字滚动的回调
     @param topLine FHCTopLineView
     @param index 滚动到的位置index
     */
    - (void)topLineView:(FHCTopLineView *)topLine didScrollToIndex:(NSInteger)index;
    

    平时用习惯了,所以控制滚动的属性也是参照SDCycleScrollView

    ///////////////////////////  滚动控制接口 ///////////////////////////////
    
    /** 自动滚动间隔时间,默认2s */
    @property (nonatomic, assign) CGFloat autoScrollTimeInterval;
    
    /** 是否无限循环,默认Yes */
    @property (nonatomic,assign) BOOL infiniteLoop;
    
    /** 是否自动滚动,默认Yes */
    @property (nonatomic,assign) BOOL autoScroll;
    
    /** 是否允许用户拖动,默认Yes */
    @property (nonatomic, assign) BOOL enableDrag;
    
    /** 文字滚动方向,默认为垂直滚动 */
    @property (nonatomic, assign) UICollectionViewScrollDirection scrollDirection;
    
    /** 代理 */
    @property (nonatomic, weak) id<FHCTopLineViewDelegate> delegate;
    
    ///////////////////////////// 数据源接口 ////////////////////////////////////
    
    /** 数据源数组 */
    @property (nonatomic, strong) NSArray *titlesGroup;
    

    Demo地址:FHCTopLineView

    相关文章

      网友评论

        本文标题:iOS类似淘宝头条的文字滚动广告

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