Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
maltsugar committed Apr 15, 2020
1 parent a888d29 commit b69478e
Show file tree
Hide file tree
Showing 9 changed files with 239 additions and 18 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

### 版本记录

- 1.1.1 修复[#16](https://github.com/maltsugar/RollingNotice/issues/16)[#17](https://github.com/maltsugar/RollingNotice/issues/17),感谢[@xygkevin][https://github.com/xygkevin]
- 1.1.0 新增暂停/恢复功能
- 1.0.9 数据源返回数量为0时,不再显示cell
- 1.0.8 普通用法textLabel支持左右间距设置
Expand Down
2 changes: 1 addition & 1 deletion RollingNotice.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Pod::Spec.new do |s|
#

s.name = "RollingNotice"
s.version = "1.1.0"
s.version = "1.1.1"
s.summary = "滚动公告、广告,支持灵活自定义cell。淘宝、口碑、京东、美团、天猫等等一切滚动广告 Roll Notice or Advertising. A rolling banner, custom cell like UITableViewCell supported"

# This description is used to generate tags and improve search results.
Expand Down
12 changes: 12 additions & 0 deletions RollingNotice.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
91FFD88A1FD52A36004A1A27 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 91FFD8891FD52A36004A1A27 /* main.m */; };
91FFD8931FD52A6F004A1A27 /* GYRollingNoticeView.m in Sources */ = {isa = PBXBuildFile; fileRef = 91FFD8921FD52A6F004A1A27 /* GYRollingNoticeView.m */; };
91FFD8961FD52A96004A1A27 /* GYNoticeViewCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 91FFD8951FD52A96004A1A27 /* GYNoticeViewCell.m */; };
B911E5C92447331000A758F5 /* ThirdViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = B911E5C82447331000A758F5 /* ThirdViewController.m */; };
B9C7FE0F2447121D0006323D /* DemoCodeAutoLayout.m in Sources */ = {isa = PBXBuildFile; fileRef = B9C7FE0E2447121D0006323D /* DemoCodeAutoLayout.m */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand Down Expand Up @@ -50,6 +52,10 @@
91FFD8921FD52A6F004A1A27 /* GYRollingNoticeView.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = GYRollingNoticeView.m; sourceTree = "<group>"; };
91FFD8941FD52A96004A1A27 /* GYNoticeViewCell.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GYNoticeViewCell.h; sourceTree = "<group>"; };
91FFD8951FD52A96004A1A27 /* GYNoticeViewCell.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = GYNoticeViewCell.m; sourceTree = "<group>"; };
B911E5C72447331000A758F5 /* ThirdViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ThirdViewController.h; sourceTree = "<group>"; };
B911E5C82447331000A758F5 /* ThirdViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ThirdViewController.m; sourceTree = "<group>"; };
B9C7FE0D2447121D0006323D /* DemoCodeAutoLayout.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = DemoCodeAutoLayout.h; sourceTree = "<group>"; };
B9C7FE0E2447121D0006323D /* DemoCodeAutoLayout.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = DemoCodeAutoLayout.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -90,6 +96,8 @@
91BA83E021194E8D002D2F85 /* SecondViewController.h */,
91BA83E121194E8D002D2F85 /* SecondViewController.m */,
91BA83E221194E8D002D2F85 /* SecondViewController.xib */,
B911E5C72447331000A758F5 /* ThirdViewController.h */,
B911E5C82447331000A758F5 /* ThirdViewController.m */,
916CCC9C1FDA34330070CAF3 /* CustomNoticeCell.h */,
916CCC9D1FDA34330070CAF3 /* CustomNoticeCell.m */,
916CCC9F1FDA343F0070CAF3 /* CustomNoticeCell.xib */,
Expand All @@ -98,6 +106,8 @@
919DB80F1FE0F84400029F63 /* DemoCell3.h */,
919DB8101FE0F84400029F63 /* DemoCell3.m */,
919DB8121FE0F84E00029F63 /* DemoCell3.xib */,
B9C7FE0D2447121D0006323D /* DemoCodeAutoLayout.h */,
B9C7FE0E2447121D0006323D /* DemoCodeAutoLayout.m */,
91FFD8801FD52A36004A1A27 /* Main.storyboard */,
91FFD8831FD52A36004A1A27 /* Assets.xcassets */,
91FFD8851FD52A36004A1A27 /* LaunchScreen.storyboard */,
Expand Down Expand Up @@ -194,8 +204,10 @@
files = (
91BA83E321194E8D002D2F85 /* SecondViewController.m in Sources */,
916CCC9E1FDA34330070CAF3 /* CustomNoticeCell.m in Sources */,
B9C7FE0F2447121D0006323D /* DemoCodeAutoLayout.m in Sources */,
91FFD8961FD52A96004A1A27 /* GYNoticeViewCell.m in Sources */,
919DB80E1FE0F44600029F63 /* DemoCell2.m in Sources */,
B911E5C92447331000A758F5 /* ThirdViewController.m in Sources */,
91FFD8931FD52A6F004A1A27 /* GYRollingNoticeView.m in Sources */,
91FFD87F1FD52A36004A1A27 /* ViewController.m in Sources */,
91FFD88A1FD52A36004A1A27 /* main.m in Sources */,
Expand Down
21 changes: 21 additions & 0 deletions RollingNotice/DemoCodeAutoLayout.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// DemoCodeAutoLayout.h
// RollingNotice
//
// Created by zgy on 2020/4/15.
// Copyright © 2020 qm. All rights reserved.
//

#import "GYNoticeViewCell.h"

NS_ASSUME_NONNULL_BEGIN

@interface DemoCodeAutoLayout : GYNoticeViewCell



@property (nonatomic, strong) UILabel *customLab;

@end

NS_ASSUME_NONNULL_END
50 changes: 50 additions & 0 deletions RollingNotice/DemoCodeAutoLayout.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//
// DemoCodeAutoLayout.m
// RollingNotice
//
// Created by zgy on 2020/4/15.
// Copyright © 2020 qm. All rights reserved.
//

#import "DemoCodeAutoLayout.h"

@implementation DemoCodeAutoLayout

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithReuseIdentifier:reuseIdentifier];
if (self) {
[self setupUI];
}
return self;
}

// cell 高度30
- (void)setupUI
{
UIButton *testBtn = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[self.contentView addSubview:testBtn];
// testBtn.frame = CGRectMake(10, 5, 20, 20);
testBtn.translatesAutoresizingMaskIntoConstraints = NO;
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[v]" options:0 metrics:nil views:@{@"v": testBtn}]];
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[v]" options:0 metrics:nil views:@{@"v": testBtn}]];

[testBtn addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[v(30)]" options:0 metrics:nil views:@{@"v": testBtn}]];
[testBtn addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[v(60)]" options:0 metrics:nil views:@{@"v": testBtn}]];



_customLab = [[UILabel alloc]init];
_customLab.textColor = [UIColor redColor];
_customLab.backgroundColor = [UIColor lightGrayColor];
[self.contentView addSubview:_customLab];

_customLab.translatesAutoresizingMaskIntoConstraints = NO;
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[v]|" options:0 metrics:nil views:@{@"v": _customLab}]];
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-80-[v]" options:0 metrics:nil views:@{@"v": _customLab}]];
[_customLab addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[v(160)]" options:0 metrics:nil views:@{@"v": _customLab}]];


}

@end
55 changes: 42 additions & 13 deletions RollingNotice/GYRollingNoticeView/GYRollingNoticeView.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@

@interface GYRollingNoticeView ()

{
int _cIdx;
BOOL _needTryRoll;
}

@property (nonatomic, strong) NSMutableDictionary *cellClsDict;
@property (nonatomic, strong) NSMutableArray *reuseCells;
Expand All @@ -28,6 +32,7 @@ @interface GYRollingNoticeView ()
@end

@implementation GYRollingNoticeView
@dynamic currentIndex;

- (instancetype)init
{
Expand Down Expand Up @@ -62,6 +67,16 @@ - (void)setupNoticeViews
[self addGestureRecognizer:[self createTapGesture]];
}

- (void)layoutSubviews
{
[super layoutSubviews];
if (_needTryRoll) {
[self reloadDataAndStartRoll];
_needTryRoll = NO;
}

}



- (void)registerClass:(nullable Class)cellClass forCellReuseIdentifier:(NSString *)identifier
Expand Down Expand Up @@ -106,25 +121,29 @@ - (__kindof GYNoticeViewCell *)dequeueReusableCellWithIdentifier:(NSString *)ide
- (void)layoutCurrentCellAndWillShowCell
{
int count = (int)[self.dataSource numberOfRowsForRollingNoticeView:self];
if (_currentIndex > count - 1) {
_currentIndex = 0;
if (_cIdx > count - 1) {
_cIdx = 0;
}

int willShowIndex = _currentIndex + 1;
int willShowIndex = _cIdx + 1;
if (willShowIndex > count - 1) {
willShowIndex = 0;
}
// NSLog(@">>>>%d", _currentIndex);
// NSLog(@">>>>%d", _cIdx);

float w = self.frame.size.width;
float h = self.frame.size.height;

// NSLog(@"count: %d, _currentIndex:%d willShowIndex: %d", count, _currentIndex, willShowIndex);
// NSLog(@"count: %d, _cIdx:%d willShowIndex: %d", count, _cIdx, willShowIndex);

if (!(w > 0 && h > 0)) {
_needTryRoll = YES;
return;
}
if (!_currentCell) {
// 第一次没有currentcell
// currentcell is null at first time
_currentCell = [self.dataSource rollingNoticeView:self cellAtIndex:_currentIndex];
_currentCell = [self.dataSource rollingNoticeView:self cellAtIndex:_cIdx];
_currentCell.frame = CGRectMake(0, 0, w, h);
[self addSubview:_currentCell];
return;
Expand Down Expand Up @@ -179,7 +198,7 @@ - (void)stopRoll

_status = GYRollingNoticeViewStatusIdle;
_isAnimating = NO;
_currentIndex = 0;
_cIdx = 0;
[_currentCell removeFromSuperview];
[_willShowCell removeFromSuperview];
_currentCell = nil;
Expand Down Expand Up @@ -209,7 +228,7 @@ - (void)timerHandle
if (self.isAnimating) return;

[self layoutCurrentCellAndWillShowCell];
_currentIndex ++;


float w = self.frame.size.width;
float h = self.frame.size.height;
Expand All @@ -232,18 +251,16 @@ - (void)timerHandle
self.currentCell = self.willShowCell;
}
self.isAnimating = NO;

self -> _cIdx ++;
}];
}


- (void)handleCellTapAction
{
int count = (int)[self.dataSource numberOfRowsForRollingNoticeView:self];
if (_currentIndex > count - 1) {
_currentIndex = 0;
}
if ([self.delegate respondsToSelector:@selector(didClickRollingNoticeView:forIndex:)]) {
[self.delegate didClickRollingNoticeView:self forIndex:_currentIndex];
[self.delegate didClickRollingNoticeView:self forIndex:self.currentIndex];
}
}

Expand All @@ -254,6 +271,18 @@ - (UITapGestureRecognizer *)createTapGesture

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event{}


#pragma mark-

- (int)currentIndex {
int count = (int)[self.dataSource numberOfRowsForRollingNoticeView:self];
if (_cIdx > count - 1) {
_cIdx = 0;
}
return _cIdx;
}


#pragma mark- lazy
- (NSMutableDictionary *)cellClsDict
{
Expand Down
17 changes: 17 additions & 0 deletions RollingNotice/ThirdViewController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// ThirdViewController.h
// RollingNotice
//
// Created by zgy on 2020/4/15.
// Copyright © 2020 qm. All rights reserved.
//

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface ThirdViewController : UIViewController

@end

NS_ASSUME_NONNULL_END
90 changes: 90 additions & 0 deletions RollingNotice/ThirdViewController.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
//
// ThirdViewController.m
// RollingNotice
//
// Created by zgy on 2020/4/15.
// Copyright © 2020 qm. All rights reserved.
//

#import "ThirdViewController.h"
#import "DemoCodeAutoLayout.h"
#import "GYRollingNoticeView.h"

@interface ThirdViewController ()<GYRollingNoticeViewDataSource, GYRollingNoticeViewDelegate>

{
NSArray *_arr;

}
@property (nonatomic, strong) GYRollingNoticeView *noticeView;
@end

@implementation ThirdViewController

- (instancetype)init
{
self = [super init];
if (self) {

_arr = @[@"小米千元全面屏:抱歉,久等!625献上",
// @"可怜狗狗被抛弃,苦苦等候主人半年",
// @"三星中端新机改名,全面屏火力全开",
// @"学会这些,这5种花不用去花店买了",
// @"华为nova2S发布,剧透了荣耀10?"
];


_noticeView = [[GYRollingNoticeView alloc] init];
_noticeView.delegate = self;
_noticeView.dataSource = self;

[self.view addSubview:_noticeView];


[_noticeView registerClass:[DemoCodeAutoLayout class] forCellReuseIdentifier:@"DemoCodeAutoLayout"];

[_noticeView reloadDataAndStartRoll];

_noticeView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-160-[v]" options:0 metrics:nil views:@{@"v": _noticeView}]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[v]-20-|" options:0 metrics:nil views:@{@"v": _noticeView}]];

[_noticeView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[v(30)]" options:0 metrics:nil views:@{@"v": _noticeView}]];

}
return self;
}


- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
}


- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[_noticeView stopRoll];
}


#pragma mark- <GYRollingNoticeViewDataSource, GYRollingNoticeViewDelegate>
- (NSInteger)numberOfRowsForRollingNoticeView:(GYRollingNoticeView *)rollingView
{
return _arr.count;
}
- (__kindof GYNoticeViewCell *)rollingNoticeView:(GYRollingNoticeView *)rollingView cellAtIndex:(NSUInteger)index
{
DemoCodeAutoLayout *cell = [rollingView dequeueReusableCellWithIdentifier:@"DemoCodeAutoLayout"];
cell.customLab.text = _arr[index];
return cell;

}

- (void)didClickRollingNoticeView:(GYRollingNoticeView *)rollingView forIndex:(NSUInteger)index
{
NSLog(@"点击的index: %d", rollingView.currentIndex);
}

@end
Loading

0 comments on commit b69478e

Please sign in to comment.