diff --git a/AutoAdLabelScroll.xcodeproj/project.xcworkspace/xcuserdata/xinqing.xcuserdatad/UserInterfaceState.xcuserstate b/AutoAdLabelScroll.xcodeproj/project.xcworkspace/xcuserdata/xinqing.xcuserdatad/UserInterfaceState.xcuserstate
new file mode 100644
index 0000000..a463c17
Binary files /dev/null and b/AutoAdLabelScroll.xcodeproj/project.xcworkspace/xcuserdata/xinqing.xcuserdatad/UserInterfaceState.xcuserstate differ
diff --git a/AutoAdLabelScroll.xcodeproj/xcuserdata/xinqing.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/AutoAdLabelScroll.xcodeproj/xcuserdata/xinqing.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist
new file mode 100644
index 0000000..0406f3f
--- /dev/null
+++ b/AutoAdLabelScroll.xcodeproj/xcuserdata/xinqing.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist
@@ -0,0 +1,37 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/AutoAdLabelScroll.xcodeproj/xcuserdata/xinqing.xcuserdatad/xcschemes/AutoAdLabelScroll.xcscheme b/AutoAdLabelScroll.xcodeproj/xcuserdata/xinqing.xcuserdatad/xcschemes/AutoAdLabelScroll.xcscheme
new file mode 100644
index 0000000..5db9031
--- /dev/null
+++ b/AutoAdLabelScroll.xcodeproj/xcuserdata/xinqing.xcuserdatad/xcschemes/AutoAdLabelScroll.xcscheme
@@ -0,0 +1,111 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/AutoAdLabelScroll.xcodeproj/xcuserdata/xinqing.xcuserdatad/xcschemes/xcschememanagement.plist b/AutoAdLabelScroll.xcodeproj/xcuserdata/xinqing.xcuserdatad/xcschemes/xcschememanagement.plist
new file mode 100644
index 0000000..d6bab5a
--- /dev/null
+++ b/AutoAdLabelScroll.xcodeproj/xcuserdata/xinqing.xcuserdatad/xcschemes/xcschememanagement.plist
@@ -0,0 +1,32 @@
+
+
+
+
+ SchemeUserState
+
+ AutoAdLabelScroll.xcscheme
+
+ orderHint
+ 0
+
+
+ SuppressBuildableAutocreation
+
+ 86C739C31CB4A0FF00E27C05
+
+ primary
+
+
+ 86C739DC1CB4A0FF00E27C05
+
+ primary
+
+
+ 86C739E71CB4A0FF00E27C05
+
+ primary
+
+
+
+
+
diff --git a/AutoAdLabelScroll/HRAdView.h b/AutoAdLabelScroll/HRAdView.h
index 749c479..05f933c 100644
--- a/AutoAdLabelScroll/HRAdView.h
+++ b/AutoAdLabelScroll/HRAdView.h
@@ -11,72 +11,73 @@
@interface HRAdView : UIView
/**
- * 广告内容数组
+ 当前索引,无内容时为NSNotFound
*/
-@property (nonatomic, copy) NSArray *adTitles;
+@property (nonatomic, assign, readonly) NSUInteger currentIndex;
+@property (nonatomic, strong, readonly) UIImageView *iconView;
/**
* 头部图片 默认为nil
*/
-@property (nonatomic, strong) UIImage *headImg;
-
+@property (nonatomic, strong) UIImage *icon;
/**
* 图片位置
*/
-@property (nonatomic, assign) UIEdgeInsets edgeInsets;
-
+@property (nonatomic, assign) UIEdgeInsets iconInsets;
/**
- * 广告字体 默认为16号系统字体
+ * 广告内容数组
*/
-@property (nonatomic, strong) UIFont *labelFont;
-
+@property (nonatomic, strong) NSArray *texts;
/**
* 广告字体颜色 默认为黑色
*/
-@property (nonatomic, strong) UIColor *color;
-
+@property (nonatomic, strong) UIColor *textColor;
/**
- * 轮播时间间隔 默认2s
- */
-@property (nonatomic, assign) NSTimeInterval time;
-
-/**
- * 是否开启点击事件 默认为NO
+ * 广告字体 默认为16号系统字体
*/
-@property (nonatomic, assign) BOOL isHaveTouchEvent;
-
+@property (nonatomic, strong) UIFont *font;
/**
- * 点击事件响应
+ 文字行数
*/
-@property (nonatomic, copy) void (^clickAdBlock)(NSUInteger index);
-
+@property (nonatomic, assign) NSInteger numberOfTextLines;
/**
* 文本对齐方式
*/
@property (nonatomic, assign) NSTextAlignment textAlignment;
-
/**
- 文字左边默认间距
+ 文字边距
*/
-@property (nonatomic, assign) CGFloat defaultMargin;
-
+@property (nonatomic, assign) UIEdgeInsets textInsets;
/**
- 文字行数
+ * 轮播时间间隔 默认2s
*/
-@property (nonatomic, assign) NSInteger numberOfTextLines;
+@property (nonatomic, assign) NSTimeInterval duration;
+/**
+ 滚动动画时长 默认1s
+ */
+@property (nonatomic, assign) NSTimeInterval animateDuration;
+/**
+ * 是否开启点击事件 默认为NO
+ */
+@property (nonatomic, assign) BOOL touchEnabled;
+/**
+ * 点击事件响应
+ */
+@property (nonatomic, copy) void (^clickAdBlock)(NSUInteger index);
/**
* 开始轮播
*/
-- (void)beginScroll;
+- (void)startScroll;
/**
* 关闭轮播
*/
-- (void)closeScroll;
+- (void)stopScroll;
/**
* 实例化方法
*/
-- (instancetype)initWithTitles:(NSArray *)titles;
+- (instancetype)initWithTexts:(NSArray *)titles;
+
@end
diff --git a/AutoAdLabelScroll/HRAdView.m b/AutoAdLabelScroll/HRAdView.m
index 59e3063..ba481b7 100644
--- a/AutoAdLabelScroll/HRAdView.m
+++ b/AutoAdLabelScroll/HRAdView.m
@@ -12,10 +12,11 @@
@interface HRAdView ()
+@property (nonatomic, assign) NSUInteger currentIndex;
/**
* 文字广告条前面的图标
*/
-@property (nonatomic, strong) UIImageView *headImageView;
+@property (nonatomic, strong) UIImageView *iconView;
/**
轮流显示的第一个Label
@@ -26,7 +27,7 @@ @interface HRAdView ()
轮流显示的第二个Label
*/
@property (nonatomic, strong) UILabel *twoLabel;
-
+@property (nonatomic, weak) UILabel *currentLabel;
/**
* 计时器
*/
@@ -37,38 +38,32 @@ @interface HRAdView ()
@implementation HRAdView
{
- NSUInteger index;
CGFloat margin;
- BOOL isBegin;
}
-- (instancetype)initWithTitles:(NSArray *)titles {
-
- self = [super init];
-
+- (instancetype)initWithFrame:(CGRect)frame
+{
+ self = [super initWithFrame:frame];
if (self) {
margin = 0;
self.clipsToBounds = YES;
- self.adTitles = titles;
- self.headImg = nil;
- self.labelFont = [UIFont systemFontOfSize:16];
- self.color = [UIColor blackColor];
- self.time = 2.0f;
+ self.icon = nil;
+ self.font = [UIFont systemFontOfSize:16];
+ self.textColor = [UIColor blackColor];
+ self.duration = 2.0f;
+ self.animateDuration = 1.f;
self.textAlignment = NSTextAlignmentLeft;
- self.isHaveTouchEvent = NO;
- self.edgeInsets = UIEdgeInsetsZero;
- self.defaultMargin = 0;
- index = 0;
+ self.touchEnabled = NO;
+ self.iconInsets = UIEdgeInsetsZero;
+ self.textInsets = UIEdgeInsetsZero;
+ _currentIndex = NSNotFound;
- if (!_headImageView) {
- _headImageView = [UIImageView new];
+ if (!_iconView) {
+ _iconView = [UIImageView new];
}
if (!_oneLabel) {
_oneLabel = [self createLabel];
- if (self.adTitles.count > 0) {
- _oneLabel.text = [NSString stringWithFormat:@"%@", self.adTitles[index]];
- }
[self addSubview:_oneLabel];
}
@@ -80,101 +75,106 @@ - (instancetype)initWithTitles:(NSArray *)titles {
return self;
}
+- (instancetype)initWithTexts:(NSArray *)titles {
+ if ([self init]) {
+ self.texts = titles;
+ if (self.texts.count > 0) {
+ _oneLabel.text = self.texts.firstObject;
+ _currentIndex = 0;
+ _currentLabel = _oneLabel;
+ }
+ }
+
+ return self;
+}
+
- (UILabel *)createLabel {
UILabel *label = [UILabel new];
- label.font = self.labelFont;
- label.textColor = self.color;
+ label.font = self.font;
+ label.textColor = self.textColor;
label.textAlignment = self.textAlignment;
label.numberOfLines = self.numberOfTextLines;
return label;
}
+- (void)animateForText:(NSString *)text {
+ UILabel *willHideLabel = self.currentLabel;
+ UILabel *willShowLabel = self.currentLabel == self.oneLabel ? self.twoLabel : self.oneLabel;
+
+ willShowLabel.text = text;
+ [UIView animateWithDuration:self.animateDuration animations:^{
+ willShowLabel.frame = CGRectMake(margin, 0, ViewWidth - margin - self.textInsets.right, ViewHeight - self.textInsets.top - self.textInsets.bottom);
+ willHideLabel.frame = CGRectMake(margin, -ViewHeight, ViewWidth - margin - self.textInsets.right, ViewHeight - self.textInsets.top - self.textInsets.bottom);
+ } completion:^(BOOL finished) {
+ willHideLabel.frame = CGRectMake(margin, ViewHeight, ViewWidth - margin - self.textInsets.right, ViewHeight - self.textInsets.top - self.textInsets.bottom);
+ self.currentLabel = willShowLabel;
+ }];
+}
+
- (void)timeRepeat {
- if (self.adTitles.count <= 1) {
- [self.timer invalidate];
- self.timer = nil;
+ if (self.texts.count == 0 && _currentIndex != NSNotFound) {
+ _currentIndex = NSNotFound;
+ [self animateForText:@""];
return;
}
- __block UILabel *currentLabel;
- __block UILabel *hidenLabel;
- __weak typeof(self) weakself = self;
- [self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
- if ([obj isKindOfClass:[UILabel class]]) {
- UILabel *label = obj;
- NSString *string = weakself.adTitles[index];
- if ([label.text isEqualToString:string]) {
- currentLabel = label;
- }else{
- hidenLabel = label;
- }
- }
- }];
+ if (self.texts.count == 1 && _currentIndex != 0) {
+ _currentIndex = 0;
+ [self animateForText:self.texts.firstObject];
+ return;
+ }
- if (index != self.adTitles.count - 1) {
- index++;
+ if (_currentIndex < self.texts.count - 1) {
+ _currentIndex ++;
} else {
- index = 0;
+ _currentIndex = 0;
}
-
- hidenLabel.text = [NSString stringWithFormat:@"%@", self.adTitles[index]];
- [UIView animateWithDuration:1 animations:^{
- hidenLabel.frame = CGRectMake(margin, 0, ViewWidth - margin, ViewHeight);
- currentLabel.frame = CGRectMake(margin, -ViewHeight, ViewWidth - margin, ViewHeight);
- } completion:^(BOOL finished) {
- currentLabel.frame = CGRectMake(margin, ViewHeight, ViewWidth - margin, ViewHeight);
- }];
+ [self animateForText:self.texts[_currentIndex]];
}
- (void)layoutSubviews {
[super layoutSubviews];
- if (self.headImg) {
- [self addSubview:self.headImageView];
-
- self.headImageView.frame = CGRectMake(self.edgeInsets.left,
- self.edgeInsets.top,
- ViewHeight - self.edgeInsets.top - self.edgeInsets.bottom,
- ViewHeight - self.edgeInsets.top - self.edgeInsets.bottom
+ if (self.icon) {
+ [self addSubview:self.iconView];
+ self.iconView.frame = CGRectMake(self.iconInsets.left,
+ self.iconInsets.top,
+ ViewHeight - self.iconInsets.top - self.iconInsets.bottom,
+ ViewHeight - self.iconInsets.top - self.iconInsets.bottom
);
- margin = CGRectGetMaxX(self.headImageView.frame) + self.defaultMargin;
+ margin = CGRectGetMaxX(self.iconView.frame) + self.textInsets.left;
} else {
- if (self.headImageView) {
- [self.headImageView removeFromSuperview];
- self.headImageView = nil;
+ if (self.iconView) {
+ [self.iconView removeFromSuperview];
+ self.iconView = nil;
}
- margin = self.defaultMargin;
+ margin = self.textInsets.left;
}
- self.oneLabel.frame = CGRectMake(margin, 0, ViewWidth - margin, ViewHeight);
- self.twoLabel.frame = CGRectMake(margin, ViewHeight, ViewWidth - margin, ViewHeight);
+ self.oneLabel.frame = CGRectMake(margin, 0, ViewWidth - margin - self.textInsets.right, ViewHeight - self.textInsets.top - self.textInsets.bottom);
+ self.twoLabel.frame = CGRectMake(margin, ViewHeight, ViewWidth - margin - self.textInsets.right, ViewHeight - self.textInsets.top - self.textInsets.bottom);
}
-- (NSTimer *)timer {
- if (!_timer) {
- _timer = [NSTimer timerWithTimeInterval:self.time target:self selector:@selector(timeRepeat) userInfo:self repeats:YES];
+- (void)startScroll {
+ if (self.timer) {
+ [self.timer invalidate];
+ self.timer = nil;
}
- return _timer;
+ self.timer = [NSTimer timerWithTimeInterval:self.duration target:self selector:@selector(timeRepeat) userInfo:self repeats:YES];
+ [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
}
-- (void)beginScroll {
- if (self.timer.isValid) {
+- (void)stopScroll {
+ if (self.timer) {
[self.timer invalidate];
self.timer = nil;
}
-
- [[NSRunLoop mainRunLoop]addTimer:self.timer forMode:NSDefaultRunLoopMode];
-}
-
-- (void)closeScroll {
- [self.timer invalidate];
- self.timer = nil;
}
-- (void)setEdgeInsets:(UIEdgeInsets)edgeInsets {
- _edgeInsets = edgeInsets;
+- (void)setIconInsets:(UIEdgeInsets)iconInsets {
+ _iconInsets = iconInsets;
}
-- (void)setIsHaveTouchEvent:(BOOL)isHaveTouchEvent {
+- (void)setTouchEnabled:(BOOL)isHaveTouchEvent {
if (isHaveTouchEvent) {
self.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickEvent:)];
@@ -184,18 +184,18 @@ - (void)setIsHaveTouchEvent:(BOOL)isHaveTouchEvent {
}
}
-- (void)setTime:(NSTimeInterval)time {
- _time = time;
+- (void)setDuration:(NSTimeInterval)time {
+ _duration = time;
if (self.timer.isValid) {
[self.timer isValid];
self.timer = nil;
}
}
-- (void)setHeadImg:(UIImage *)headImg {
- _headImg = headImg;
+- (void)setIcon:(UIImage *)headImg {
+ _icon = headImg;
- self.headImageView.image = headImg;
+ _iconView.image = headImg;
}
- (void)setTextAlignment:(NSTextAlignment)textAlignment {
@@ -205,16 +205,16 @@ - (void)setTextAlignment:(NSTextAlignment)textAlignment {
self.twoLabel.textAlignment = _textAlignment;
}
-- (void)setColor:(UIColor *)color {
- _color = color;
- self.oneLabel.textColor = _color;
- self.twoLabel.textColor = _color;
+- (void)setTextColor:(UIColor *)color {
+ _textColor = color;
+ self.oneLabel.textColor = color;
+ self.twoLabel.textColor = color;
}
-- (void)setLabelFont:(UIFont *)labelFont {
- _labelFont = labelFont;
- self.oneLabel.font = _labelFont;
- self.twoLabel.font = _labelFont;
+- (void)setFont:(UIFont *)labelFont {
+ _font = labelFont;
+ self.oneLabel.font = labelFont;
+ self.twoLabel.font = labelFont;
}
- (void)setNumberOfTextLines:(NSInteger)numberOfTextLines {
@@ -224,17 +224,9 @@ - (void)setNumberOfTextLines:(NSInteger)numberOfTextLines {
}
- (void)clickEvent:(UITapGestureRecognizer *)tapGestureRecognizer {
- [self.adTitles enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
- if (index % 2 == 0 && [self.oneLabel.text isEqualToString:obj]) {
- if (self.clickAdBlock) {
- self.clickAdBlock(index);
- }
- } else if (index % 2 != 0 && [self.twoLabel.text isEqualToString:obj]) {
- if (self.clickAdBlock) {
- self.clickAdBlock(index);
- }
- }
- }];
+ if (self.clickAdBlock) {
+ self.clickAdBlock(_currentIndex);
+ }
}
diff --git a/AutoAdLabelScroll/ViewController.m b/AutoAdLabelScroll/ViewController.m
index e4d8a01..f1c11c3 100644
--- a/AutoAdLabelScroll/ViewController.m
+++ b/AutoAdLabelScroll/ViewController.m
@@ -21,16 +21,16 @@ - (void)viewDidLoad {
[super viewDidLoad];
NSArray *array = @[@"111111111111111111111111111111111111111111111111111",@"22222222",@"33333333"];
- HRAdView * view = [[HRAdView alloc]initWithTitles:array];
+ HRAdView * view = [[HRAdView alloc] initWithTexts:array];
view.frame = CGRectMake(5, 64, self.view.frame.size.width-10, 44);
view.textAlignment = NSTextAlignmentLeft;//默认
- view.isHaveTouchEvent = YES;
- view.labelFont = [UIFont boldSystemFontOfSize:17];
- view.color = [UIColor redColor];
- view.time = 2.0f;
- view.defaultMargin = 10;
+ view.touchEnabled = YES;
+ view.font = [UIFont boldSystemFontOfSize:17];
+ view.textColor = [UIColor redColor];
+ view.duration = 3.0f;
+ view.textInsets = UIEdgeInsetsMake(0, 10, 0, 0);
view.numberOfTextLines = 2;
- view.edgeInsets = UIEdgeInsetsMake(8, 8,8, 10);
+ view.iconInsets = UIEdgeInsetsMake(8, 8,8, 10);
__weak typeof(self) weakself = self;
view.clickAdBlock = ^(NSUInteger index){
DetailViewController *vc = [[DetailViewController alloc]init];
@@ -39,7 +39,7 @@ - (void)viewDidLoad {
[weakself.navigationController pushViewController:vc animated:YES];
NSLog(@"%@",array[index]);
};
- view.headImg = [UIImage imageNamed:@"laba.png"];
+ view.icon = [UIImage imageNamed:@"laba.png"];
[self.view addSubview:view];
self.adView = view;
view.backgroundColor = [UIColor whiteColor];
@@ -63,18 +63,29 @@ - (void)viewDidLoad {
[self.view addSubview:endScrollBtn];
+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
+// view.texts = @[@"tihuan1", @"tihuan2", @"tihuan3", @"tihuan4", @"tihuan5", @"tihuan6"];
+ view.texts = nil;
+ });
+
+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
+ view.texts = @[@"tihuan1", @"tihuan2", @"tihuan3", @"tihuan4", @"tihuan5", @"tihuan6"];
+ });
+
+
+
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
- [self.adView beginScroll];
+ [self.adView startScroll];
}
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
- [self.adView closeScroll];
+ [self.adView stopScroll];
}
@@ -83,7 +94,7 @@ - (void)startScroll:(UIButton *)sender{
/**
* 手动控制滚动
*/
- [self.adView beginScroll];
+ [self.adView startScroll];
[self scaleTheView:sender];
}
@@ -91,7 +102,7 @@ - (void)endScroll:(UIButton *)sender{
/**
* 停止滚动
*/
- [self.adView closeScroll];
+ [self.adView startScroll];
[self scaleTheView:sender];
}