文字广告轮播这个就够用了

地址:

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\n" behindText:@"满150减10+满79减5"];
    NSMutableAttributedString *attributedText2 = [SGHelperTool SG_textAttachmentWithImageName:@"" imageSize:(CGSizeMake(0, 0)) frontText:@"HTC新品首发,预约送大礼包\n" behindText:@"12期免息+免费试用"];
    NSMutableAttributedString *attributedText3 = [SGHelperTool SG_textAttachmentWithImageName:@"activity" imageSize:(CGSizeMake(36, 19)) frontText:@"“挑食”进口生鲜,满199减20\n" 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.

时间: 2025-01-14 15:24:53

文字广告轮播这个就够用了的相关文章

Android无限广告轮播 - 自定义BannerView

1.概述 这其实是我第一篇想写的博客,可能是因为我遇到了太多的坑,那个时候刚入行下了很多Demo发现怎么也改不动,可能是能力有限,这次就做一个具体的实现和彻底的封装. 上次讲了Android无限广告轮播-ViewPager源码分析,有了源码分析我们对ViewPager就有了一个大概的了解,那么再来封装成自定义View,就会简单许多,附视频讲解地址:http://pan.baidu.com/s/1bpqqkGn 2.效果封装 2.1 自定义BannerViewPager extends ViewP

Android 通过ViewFlipper实现广告轮播功能并可以通过手势滑动进行广告切换

为了实现广告轮播功能,在网上找了很多方法,有的效果很好,但是代码太麻烦,并且大多是用的viewpager,总之不是很满意. 于是看了一下sdk有个控件是ViewFlipper,使用比较方便,于是尝试了一下,最终实现了所需效果.在这里与大家分享. 首先看一下效果(主要是布局方面的效果,毕竟手势识别和滑动不太好显示,懒得弄成gif了): 1.布局文件.xml <LinearLayout android:layout_width="fill_parent" android:layout

UIimage两种初始化的区别 广告轮播封装

UIimage两种初始化的区别 第一种初始化: UIImage *image = [UIImage imageNamed:@"xxx"];  注意(这种方法加载的图片如果后缀名是png的,可以不写后缀名,根据屏幕分辨率自己去匹配图片) 第二种初始化: NSString *path = [[NSBundle mainBundle] pathForResource:@"xxx.png" ofType:nil]; UIImage *image = [[UIImage al

广告轮播

1 package com.zxw.fragment; 2 3 import java.io.BufferedReader; 4 import java.io.IOException; 5 import java.io.InputStream; 6 import java.io.InputStreamReader; 7 import java.io.UnsupportedEncodingException; 8 import java.util.ArrayList; 9 import java.

android-自定义广告轮播Banner(无限循环实现)

关于广告轮播,大家肯定不会陌生,它在现手机市场各大APP出现的频率极高,它的优点在于"不占屏",可以仅用小小的固定空位来展示几个甚至几十个广告条,而且动态效果很好,具有很好的用户"友好性",下面来看几个示例图:    再来看下我仿写的效果: 关于广告轮播Banner这个东西,GitHub上面应该有现成的开源组件,不过我没去找过,觉得实现起来不会太难,就自己去仿写了,下面我说下实现的思路: 1.首先看到这个可以滑动切换图片的界面,我们很自然就会想到ViewPager控

猫猫学iOS 之广告轮播图,collectionView制作(源码)

猫猫分享,必须精品 原创文章,欢迎转载.转载请注明:翟乃玉的博客 地址:http://blog.csdn.net/u013357243 源码共享:https://github.com/znycat/NYCarouselView 效果图 源代码 NYCarouselView.h // // NYCarouselView.h // 广告轮播CollectionView // // Created by IOS on 15/12/26. // Copyright ? 2015年 com.itcat.c

猫猫学iOS 之广告轮播图,collectionView制作

猫猫分享,必须精品 原创文章,欢迎转载.转载请注明:翟乃玉的博客 地址:http://blog.csdn.net/u013357243 效果图 不多说,好不好先看效果,之前做过一个scrollView的轮播图,但是很局限,很多多余代码,今天猫猫重新做了一个用collectionView的流水布局做的一个,可以拿去做广告轮播,也可以做系统新特性哦,来,看下效果吧. 源码共享:https://github.com/znycat/NYCarouselView 很久很久以前就想做了.总而言之,猫猫代码有

安卓开发笔记——自定义广告轮播Banner(无限循环实现)

关于广告轮播,大家肯定不会陌生,它在现手机市场各大APP出现的频率极高,它的优点在于"不占屏",可以仅用小小的固定空位来展示几个甚至几十个广告条,而且动态效果很好,具有很好的用户"友好性",下面来看几个示例图:     再来看下我仿写的效果: 关于广告轮播Banner这个东西,GitHub上面应该有现成的开源组件,不过我没去找过,觉得实现起来不会太难,就自己去仿写了,下面我说下实现的思路: 1.首先看到这个可以滑动切换图片的界面,我们很自然就会想到ViewPager

android 项目学习随笔十六( 广告轮播条播放)

广告轮播条播放 if (mHandler == null) {//在此初始化mHandler , 保证消息不重复发送 mHandler = new Handler() { public void handleMessage(android.os.Message msg) { int currentItem = mViewPager.getCurrentItem(); if (currentItem < mTopNewsList.size() - 1) { currentItem++; } els