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

阅读: 评论:0

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

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

地址:

GitHUb:.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];[tView 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);_Color = [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 SGAdvertScrollViewstatic 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 = llectionView = [[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 (unt == 0) return; // 为解决加载数据延迟问题// 默认显示最中间的那组[llectionView 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 {unt;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {SGAdvertScrollViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"tipsCell" forIndexPath:indexPath];if (self.isHaveMutableAttributedString == YES) {cell.tipsLabel.attributedText = pArr[indexPath.item];} else { = pArr[indexPath.item];}if (self.titleFont != nil) {cell.tipsLabel.font = self.titleFont;}if (self.titleColor != nil) {Color = 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 (unt == 0) return; // 为解决加载网络图片延迟问题// 1、当前正在展示的位置NSIndexPath *currentIndexPath = [[llectionView indexPathsForVisibleItems] lastObject];//SGDebugLog(@"currentIndexPath - - %@", currentIndexPath);// 马上显示回最中间那组的数据NSIndexPath *resetCurrentIndexPath = [NSIndexPath indexPathForItem:currentIndexPath.item inSection:SGMaxSections / 2];//SGDebugLog(@"section - - %ld; item - - %ld", resetCurrentIndexPath.section, resetCurrentIndexPath.item);
    [llectionView scrollToItemAtIndexPath:resetCurrentIndexPath atScrollPosition:UICollectionViewScrollPositionBottom animated:NO];// 2、计算出下一个需要展示的位置NSInteger nextItem = resetCurrentIndexPath.item + 1;NSInteger nextSection = resetCurrentIndexPath.section;if (nextItem == unt) {nextItem = 0;nextSection++;}NSIndexPath *nextIndexPath = [NSIndexPath indexPathForItem:nextItem inSection:nextSection];// 3、通过动画滚动到下一个位置
    [llectionView scrollToItemAtIndexPath:nextIndexPath atScrollPosition:UICollectionViewScrollPositionBottom animated:YES];
}#pragma mark - - - setting
- (void)setImage:(UIImage *)image {_image = image;_imageView.image = image;
}- (void)setTitleArray:(NSArray *)titleArray {_titleArray = pArr = [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 属性 转换成 NSMutableAttributedStringNSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithAttributedString:imageText_AttributedStr];// imageText 前面的文字转换成 NSMutableAttributedStringNSMutableAttributedString *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减10n" 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减20n" 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.

 

转载于:.html

本文发布于:2024-01-27 20:46:37,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/17063595972538.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:用了   文字广告
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23