zoukankan      html  css  js  c++  java
  • 文字广告轮播这个就够用了

    地址:

    GitHUb:https://github.com/kingsic/SGAdvertScrollView.git;

    #import <UIKit/UIKit.h>
    @class SGAdvertScrollView;
    
    /** delegate */
    @protocol SGAdvertScrollViewDelegate <NSObject>
    - (void)advertScrollView:(SGAdvertScrollView *)advertScrollView didSelectedItemAtIndex:(NSInteger)index;
    
    @end
    
    @interface SGAdvertScrollView : UIView
    /** 左边提示图片 */
    @property (nonatomic, strong) UIImage *image;
    /** 右边标题数组(要么是NSString,要么NSMutableAttributedString,不可混用)*/
    @property (nonatomic, strong) NSArray *titleArray;
    /** 设置滚动时间间隔(默认 3s) */
    @property (nonatomic, assign) CGFloat timeInterval;
    /** 标题字体大小(默认 12) */
    @property (nonatomic, strong) UIFont *titleFont;
    /** 标题字体颜色(默认 黑色) */
    @property (nonatomic, strong) UIColor *titleColor;
    /** titleArray 是否包含 NSMutableAttributedString 默认为NO,如果包含必须设置为 YES */
    @property (nonatomic, assign) BOOL isHaveMutableAttributedString;
    
    /** delegate_SG */
    @property (nonatomic, weak) id<SGAdvertScrollViewDelegate> advertScrollViewDelegate;
    
    @end
    #import "SGAdvertScrollView.h"
    
    @interface SGAdvertScrollViewCell : UICollectionViewCell
    @property (nonatomic, strong) UILabel *tipsLabel;
    @end
    
    @implementation SGAdvertScrollViewCell
    
    - (instancetype)initWithFrame:(CGRect)frame {
        if (self = [super initWithFrame:frame]) {
            self.backgroundColor = [UIColor clearColor];
    
            [self.contentView addSubview:self.tipsLabel];
        }
        return self;
    }
    - (UILabel *)tipsLabel {
        if (!_tipsLabel) {
            _tipsLabel = [[UILabel alloc] init];
            _tipsLabel.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
            _tipsLabel.textColor = [UIColor blackColor];
            _tipsLabel.numberOfLines = 2;
            _tipsLabel.font = [UIFont systemFontOfSize:12];
        }
        return _tipsLabel;
    }
    @end
    
    #pragma mark - - - SGAdvertScrollView
    @interface SGAdvertScrollView () <UICollectionViewDelegate, UICollectionViewDataSource>
    @property (nonatomic, strong) UIImageView *imageView;
    @property (nonatomic, strong) UICollectionView *collectionView;
    @property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;
    @property (nonatomic, strong) NSTimer *timer;
    @property (nonatomic, strong) NSArray *tempArr;
    @end
    
    @implementation SGAdvertScrollView
    
    static NSUInteger  const SGMaxSections = 100;
    static CGFloat const SGMargin = 10;
    
    - (void)awakeFromNib {
        [super awakeFromNib];
        [self setupSubviews];
    }
    
    - (instancetype)initWithFrame:(CGRect)frame {
        if (self = [super initWithFrame:frame]) {
            [self setupSubviews];
        }
        return self;
    }
    
    - (void)setupSubviews {
        self.backgroundColor = [UIColor whiteColor];
        [self setupLeftImageView];
        [self setupCollectionView];
        self.timeInterval = 3.0;
        self.isHaveMutableAttributedString = NO;
        // 添加定时器
        [self addTimer];
    }
    
    - (void)setupLeftImageView {
        self.imageView = [[UIImageView alloc] init];
        [self addSubview:_imageView];
    }
    
    - (void)setupCollectionView {
        self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
        _flowLayout.minimumLineSpacing = 0;
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_flowLayout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.scrollsToTop = NO;
        _collectionView.scrollEnabled = NO;
        _collectionView.pagingEnabled = YES;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.backgroundColor = [UIColor clearColor];
        // 注册
        [_collectionView registerClass:[SGAdvertScrollViewCell class] forCellWithReuseIdentifier:@"tipsCell"];
        [self addSubview:_collectionView];
    }
    
    - (void)layoutSubviews {
        [super layoutSubviews];
        
        // 设置图片尺寸
        CGFloat imageViewX = SGMargin;
        CGFloat imageViewY = 0.5 * SGMargin;
        CGFloat imageViewH = self.frame.size.height - 2 * imageViewY;
        CGFloat imageViewW = imageViewH;
        _imageView.frame = CGRectMake(imageViewX, imageViewY, imageViewW, imageViewH);
        
        // 设置 collectionView 尺寸
        CGFloat collectionViewX = CGRectGetMaxX(_imageView.frame) + SGMargin;
        CGFloat collectionViewY = 0;
        CGFloat collectionViewW = self.frame.size.width - collectionViewX - SGMargin;
        CGFloat collectionViewH = self.frame.size.height;
        _collectionView.frame = CGRectMake(collectionViewX, collectionViewY, collectionViewW, collectionViewH);
    
        // 设置 UICollectionViewFlowLayout 尺寸
        _flowLayout.itemSize = CGSizeMake(_collectionView.frame.size.width, _collectionView.frame.size.height);
        
        // 默认显示最中间的那组
        [self defaultSelectedScetion];
    }
    /// 默认选中的组
    - (void)defaultSelectedScetion {
        if (self.tempArr.count == 0) return; // 为解决加载数据延迟问题
        // 默认显示最中间的那组
        [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:SGMaxSections / 2] atScrollPosition:UICollectionViewScrollPositionBottom animated:NO];
    }
    
    #pragma mark - - - UICollectionView 的 dataSource 方法
    - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
        return SGMaxSections;
    }
    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
        return self.tempArr.count;
    }
    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
        SGAdvertScrollViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"tipsCell" forIndexPath:indexPath];
        if (self.isHaveMutableAttributedString == YES) {
            cell.tipsLabel.attributedText = self.tempArr[indexPath.item];
        } else {
            cell.tipsLabel.text = self.tempArr[indexPath.item];
        }
        if (self.titleFont != nil) {
            cell.tipsLabel.font = self.titleFont;
        }
        if (self.titleColor != nil) {
            cell.tipsLabel.textColor = self.titleColor;
        }
        return cell;
    }
    #pragma mark - - - UICollectionView 的 delegate 方法
    - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
        if (self.advertScrollViewDelegate && [self.advertScrollViewDelegate respondsToSelector:@selector(advertScrollView:didSelectedItemAtIndex:)]) {
            [self.advertScrollViewDelegate advertScrollView:self didSelectedItemAtIndex:indexPath.item];
        }
    }
    
    #pragma mark - - - 创建定时器
    - (void)addTimer {
        [self removeTimer];
        self.timer = [NSTimer scheduledTimerWithTimeInterval:self.timeInterval target:self selector:@selector(beginUpdateUI) userInfo:nil repeats:YES];
        [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
    }
    #pragma mark - - - 移除定时器
    - (void)removeTimer {
        [_timer invalidate];
        _timer = nil;
    }
    #pragma mark - - - 定时器执行方法 - 更新UI
    - (void)beginUpdateUI {
        if (self.tempArr.count == 0) return; // 为解决加载网络图片延迟问题
    
        // 1、当前正在展示的位置
        NSIndexPath *currentIndexPath = [[self.collectionView indexPathsForVisibleItems] lastObject];
        //SGDebugLog(@"currentIndexPath - - %@", currentIndexPath);
        // 马上显示回最中间那组的数据
        NSIndexPath *resetCurrentIndexPath = [NSIndexPath indexPathForItem:currentIndexPath.item inSection:SGMaxSections / 2];
        //SGDebugLog(@"section - - %ld; item - - %ld", resetCurrentIndexPath.section, resetCurrentIndexPath.item);
        [self.collectionView scrollToItemAtIndexPath:resetCurrentIndexPath atScrollPosition:UICollectionViewScrollPositionBottom animated:NO];
    
        // 2、计算出下一个需要展示的位置
        NSInteger nextItem = resetCurrentIndexPath.item + 1;
        NSInteger nextSection = resetCurrentIndexPath.section;
        if (nextItem == self.tempArr.count) {
            nextItem = 0;
            nextSection++;
        }
    
        NSIndexPath *nextIndexPath = [NSIndexPath indexPathForItem:nextItem inSection:nextSection];
        // 3、通过动画滚动到下一个位置
        [self.collectionView scrollToItemAtIndexPath:nextIndexPath atScrollPosition:UICollectionViewScrollPositionBottom animated:YES];
    }
    
    #pragma mark - - - setting
    - (void)setImage:(UIImage *)image {
        _image = image;
        _imageView.image = image;
    }
    
    - (void)setTitleArray:(NSArray *)titleArray {
        _titleArray = titleArray;
        self.tempArr = [NSArray arrayWithArray:titleArray];
    }
    
    - (void)setTimeInterval:(CGFloat)timeInterval {
        _timeInterval = timeInterval;
        [self addTimer];
    }
    
    
    @end
    #import <Foundation/Foundation.h>
    #import <UIKit/UIKit.h>
    
    @interface SGHelperTool : NSObject
    
    /** 简单的图文混排 */
    + (NSMutableAttributedString *)SG_textAttachmentWithImageName:(NSString *)imageName imageSize:(CGSize)imageSize frontText:(NSString *)frontText behindText:(NSString *)behindText;
    
    @end
    #import "SGHelperTool.h"
    
    @implementation SGHelperTool
    
    /**
     *  简单的图文混排
     *
     *  @param imageName   图片名
     *  @param imageSize   图片大小
     *  @param frontText   图片前面内容
     *  @param behindText   图片后面内容
     *
     *  @return attributedText
     */
    + (NSMutableAttributedString *)SG_textAttachmentWithImageName:(NSString *)imageName imageSize:(CGSize)imageSize frontText:(NSString *)frontText behindText:(NSString *)behindText {
        NSTextAttachment *imageText = [[NSTextAttachment alloc] init];
        imageText.image = [UIImage imageNamed:imageName];
        // 将 NSTextAttachment 转换成 NSAttributedString 属性
        NSAttributedString *imageText_AttributedStr = [NSAttributedString attributedStringWithAttachment:imageText];
        // 将 NSAttributedString 属性 转换成 NSMutableAttributedString
        NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithAttributedString:imageText_AttributedStr];
        
        // imageText 前面的文字转换成 NSMutableAttributedString
        NSMutableAttributedString *front_mAttribStr = [[NSMutableAttributedString alloc] initWithString:frontText];
        // 将文字插在图片前面
        [attributedText insertAttributedString:front_mAttribStr atIndex:0];
        
        NSMutableAttributedString *behind_mAttribStr = [[NSMutableAttributedString alloc] initWithString:behindText];
        [behind_mAttribStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, behindText.length)];
        // 将文字插在图片后面
        [attributedText appendAttributedString:behind_mAttribStr];
        
        // 设置图片的大小
        imageText.bounds = CGRectMake(0, - 5, imageSize.width, imageSize.height);
    
        // 调整行间距
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle setLineSpacing:5];
        [attributedText addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, attributedText.length)];
        
        return attributedText;
    }
    
    @end

    在需要的使用:

    #import "ViewController.h"
    #import "SGAdvertScrollView.h"
    #import "SGHelperTool.h"
    #import "DetailViewController.h"
    
    @interface ViewController () <SGAdvertScrollViewDelegate>
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        
        // 例一
        SGAdvertScrollView *advertScrollView = [[SGAdvertScrollView alloc] init];
        advertScrollView.frame = CGRectMake(0, 150, self.view.frame.size.width, 30);
        advertScrollView.titleColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
        advertScrollView.image = [UIImage imageNamed:@"horn_icon"];
        advertScrollView.titleArray = @[@"常见电商类 app 滚动播放广告信息", @"采用代理模式封装, 可进行事件点击处理", @"建议去 github 上下载"];
        advertScrollView.titleFont = [UIFont systemFontOfSize:14];
        advertScrollView.advertScrollViewDelegate = self;
        [self.view addSubview:advertScrollView];
        
        
        // 例二
        SGAdvertScrollView *advertScrollView2 = [[SGAdvertScrollView alloc] init];
        advertScrollView2.frame = CGRectMake(0, 250, self.view.frame.size.width, 44);
        advertScrollView2.image = [UIImage imageNamed:@"Tmall_rendian"];
        NSMutableAttributedString *attributedText1 = [SGHelperTool SG_textAttachmentWithImageName:@"hot" imageSize:(CGSizeMake(36, 19)) frontText:@"聚惠女王节,香米更低价满150减10
    " behindText:@"满150减10+满79减5"];
        NSMutableAttributedString *attributedText2 = [SGHelperTool SG_textAttachmentWithImageName:@"" imageSize:(CGSizeMake(0, 0)) frontText:@"HTC新品首发,预约送大礼包
    " behindText:@"12期免息+免费试用"];
        NSMutableAttributedString *attributedText3 = [SGHelperTool SG_textAttachmentWithImageName:@"activity" imageSize:(CGSizeMake(36, 19)) frontText:@"“挑食”进口生鲜,满199减20
    " behindText:@"领券满199减20+进口直达"];
        advertScrollView2.isHaveMutableAttributedString = YES;
        advertScrollView2.titleArray = @[attributedText1, attributedText2, attributedText3];
        [self.view addSubview:advertScrollView2];
    }
    
    /// 代理方法(点击进行跳转)
    - (void)advertScrollView:(SGAdvertScrollView *)advertScrollView didSelectedItemAtIndex:(NSInteger)index {
        
        NSLog(@"点击的是 = %ld", (long)index);
        
        
        DetailViewController *nextVC = [[DetailViewController alloc] init];
        [self.navigationController pushViewController:nextVC animated:YES];
    }
    
    
    @end

    挺好用的,收藏一下.Mark.

  • 相关阅读:
    mysql事物中行锁与表锁
    https的实现原理
    基于http的断点续传和多线程下载
    Cookie与Session
    centos 7 安装python3
    为CentOS下的Docker安装配置python3【转】
    Jmeter如何提取响应头部的JSESSIONID【转】
    centOS7 安装nginx
    centos 7.X关闭防火墙和selinux
    (四)从输入URL到页面加载发生了什么
  • 原文地址:https://www.cnblogs.com/LzwBlog/p/6627847.html
Copyright © 2011-2022 走看看