Skip to content

Commit

Permalink
添加文字轮播方法;添加解决CycleScrollview会在viewWillAppear时图片卡在中间位置的方法
Browse files Browse the repository at this point in the history
  • Loading branch information
gsdios committed May 27, 2016
1 parent 96e15be commit c234e3d
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 17 deletions.
6 changes: 3 additions & 3 deletions SDCycleScrollView.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ Pod::Spec.new do |s|


s.name = "SDCycleScrollView"
s.version = "1.63"
s.summary = "简单易用的图片无限轮播器. 1.63版本修复内容:修复自定义图片的pagecontrol刷新图片数据时崩溃bug;设置单张图片时停止轮播"
s.version = "1.64"
s.summary = "简单易用的图片无限轮播器. 1.64版本修复内容:修复自定义图片的pagecontrol刷新图片数据时崩溃bug;设置单张图片时停止轮播;修复手动拖动图片时didScrollToIndex:代理方法返回index不准确问题"

s.homepage = "https://github.com/gsdios/SDCycleScrollView"

Expand All @@ -15,7 +15,7 @@ s.platform = :ios
s.platform = :ios, "7.0"


s.source = { :git => "https://github.com/gsdios/SDCycleScrollView.git", :tag => "1.63"}
s.source = { :git => "https://github.com/gsdios/SDCycleScrollView.git", :tag => "1.64"}


s.source_files = "SDCycleScrollView/Lib/SDCycleScrollView/**/*.{h,m}"
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,7 @@

@property (nonatomic, assign) BOOL hasConfigured;

/** 只展示文字轮播 */
@property (nonatomic, assign) BOOL onlyDisplayText;

@end
21 changes: 13 additions & 8 deletions SDCycleScrollView/Lib/SDCycleScrollView/SDCollectionViewCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -86,21 +86,26 @@ - (void)setTitle:(NSString *)title
{
_title = [title copy];
_titleLabel.text = [NSString stringWithFormat:@" %@", title];
if (_titleLabel.hidden) {
_titleLabel.hidden = NO;
}
}


- (void)layoutSubviews
{
[super layoutSubviews];

_imageView.frame = self.bounds;

CGFloat titleLabelW = self.sd_width;
CGFloat titleLabelH = _titleLabelHeight;
CGFloat titleLabelX = 0;
CGFloat titleLabelY = self.sd_height - titleLabelH;
_titleLabel.frame = CGRectMake(titleLabelX, titleLabelY, titleLabelW, titleLabelH);
_titleLabel.hidden = !_titleLabel.text;
if (self.onlyDisplayText) {
_titleLabel.frame = self.bounds;
} else {
_imageView.frame = self.bounds;
CGFloat titleLabelW = self.sd_width;
CGFloat titleLabelH = _titleLabelHeight;
CGFloat titleLabelX = 0;
CGFloat titleLabelY = self.sd_height - titleLabelH;
_titleLabel.frame = CGRectMake(titleLabelX, titleLabelY, titleLabelW, titleLabelH);
}
}

@end
8 changes: 6 additions & 2 deletions SDCycleScrollView/Lib/SDCycleScrollView/SDCycleScrollView.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ typedef enum {
+ (instancetype)cycleScrollViewWithFrame:(CGRect)frame shouldInfiniteLoop:(BOOL)infiniteLoop imageNamesGroup:(NSArray *)imageNamesGroup;



////////////////////// 数据源接口 //////////////////////

/** 网络图片 url string 数组 */
Expand Down Expand Up @@ -114,10 +113,12 @@ typedef enum {
/** block方式监听滚动 */
@property (nonatomic, copy) void (^itemDidScrollOperationBlock)(NSInteger currentIndex);


/** 解决viewWillAppear时出现时轮播图卡在一半的问题,在控制器viewWillAppear时调用此方法 */
- (void)adjustWhenControllerWillAppera;

////////////////////// 自定义样式接口 //////////////////////


/** 轮播图片的ContentMode,默认为 UIViewContentModeScaleToFill */
@property (nonatomic, assign) UIViewContentMode bannerImageViewContentMode;

Expand All @@ -130,6 +131,9 @@ typedef enum {
/** 是否在只有一张图时隐藏pagecontrol,默认为YES */
@property(nonatomic) BOOL hidesForSinglePage;

/** 只展示文字轮播 */
@property (nonatomic, assign) BOOL onlyDisplayText;

/** pagecontrol 样式,默认为动画样式 */
@property (nonatomic, assign) SDCycleScrollViewPageContolStyle pageControlStyle;

Expand Down
30 changes: 26 additions & 4 deletions SDCycleScrollView/Lib/SDCycleScrollView/SDCycleScrollView.m
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,19 @@ - (void)setLocalizationImageNamesGroup:(NSArray *)localizationImageNamesGroup
self.imagePathsGroup = [localizationImageNamesGroup copy];
}

- (void)setTitlesGroup:(NSArray *)titlesGroup
{
_titlesGroup = titlesGroup;
if (self.onlyDisplayText) {
NSMutableArray *temp = [NSMutableArray new];
for (int i = 0; i < _titlesGroup.count; i++) {
[temp addObject:@""];
}
self.backgroundColor = [UIColor clearColor];
self.imageURLStringsGroup = [temp copy];
}
}

#pragma mark - actions

- (void)setupTimer
Expand All @@ -355,7 +368,7 @@ - (void)setupPageControl
{
if (_pageControl) [_pageControl removeFromSuperview]; // 重新加载数据时调整

if (self.imagePathsGroup.count == 0) return;
if (self.imagePathsGroup.count == 0 || self.onlyDisplayText) return;

if ((self.imagePathsGroup.count == 1) && self.hidesForSinglePage) return;

Expand Down Expand Up @@ -428,7 +441,7 @@ - (int)currentIndex
} else {
index = (_mainView.contentOffset.y + _flowLayout.itemSize.height * 0.5) / _flowLayout.itemSize.height;
}
return index;
return MAX(0, index);
}

- (void)clearCache
Expand Down Expand Up @@ -506,6 +519,14 @@ - (void)dealloc {

#pragma mark - public actions

- (void)adjustWhenControllerWillAppera
{
long targetIndex = [self currentIndex];
if (targetIndex < _totalItemsCount) {
[_mainView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:targetIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
}
}


#pragma mark - UICollectionViewDataSource

Expand All @@ -521,7 +542,7 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cell

NSString *imagePath = self.imagePathsGroup[itemIndex];

if ([imagePath isKindOfClass:[NSString class]]) {
if (!self.onlyDisplayText && [imagePath isKindOfClass:[NSString class]]) {
if ([imagePath hasPrefix:@"http"]) {
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:imagePath] placeholderImage:self.placeholderImage];
} else {
Expand All @@ -531,7 +552,7 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cell
}
cell.imageView.image = image;
}
} else if ([imagePath isKindOfClass:[UIImage class]]) {
} else if (!self.onlyDisplayText && [imagePath isKindOfClass:[UIImage class]]) {
cell.imageView.image = (UIImage *)imagePath;
}

Expand All @@ -547,6 +568,7 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cell
cell.hasConfigured = YES;
cell.imageView.contentMode = self.bannerImageViewContentMode;
cell.clipsToBounds = YES;
cell.onlyDisplayText = self.onlyDisplayText;
}

return cell;
Expand Down
24 changes: 24 additions & 0 deletions SDCycleScrollView/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,30 @@ - (void)viewDidLoad {

[demoContainerView addSubview:cycleScrollView3];

// >>>>>>>>>>>>>>>>>>>>>>>>> demo轮播图4 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

// 网络加载 --- 创建只上下滚动展示文字的轮播器
// 由于模拟器的渲染问题,如果发现轮播时有一条线不必处理,模拟器放大到100%或者真机调试是不会出现那条线的
SDCycleScrollView *cycleScrollView4 = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 750, w, 40) delegate:self placeholderImage:nil];
cycleScrollView4.scrollDirection = UICollectionViewScrollDirectionVertical;
cycleScrollView4.onlyDisplayText = YES;

NSMutableArray *titlesArray = [NSMutableArray new];
[titlesArray addObject:@"纯文字上下滚动轮播"];
[titlesArray addObject:@"纯文字上下滚动轮播 -- demo轮播图4"];
[titlesArray addObjectsFromArray:titles];
cycleScrollView4.titlesGroup = [titlesArray copy];

[demoContainerView addSubview:cycleScrollView4];

}

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];

// 如果你发现你的CycleScrollview会在viewWillAppear时图片卡在中间位置,你可以调用此方法调整图片位置
// [你的CycleScrollview adjustWhenControllerWillAppera];
}


Expand Down

0 comments on commit c234e3d

Please sign in to comment.