diff --git a/SDCycleScrollView.xcodeproj/project.pbxproj b/SDCycleScrollView.xcodeproj/project.pbxproj index f3ab0da0..3c574c7c 100644 --- a/SDCycleScrollView.xcodeproj/project.pbxproj +++ b/SDCycleScrollView.xcodeproj/project.pbxproj @@ -318,6 +318,7 @@ TargetAttributes = { 9900D16D1ABEB2490077A6CB = { CreatedOnToolsVersion = 6.1; + DevelopmentTeam = MLHLEFRACM; ProvisioningStyle = Automatic; }; 9900D1861ABEB2490077A6CB = { @@ -557,10 +558,11 @@ ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage; CODE_SIGN_IDENTITY = "iPhone Developer"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; - DEVELOPMENT_TEAM = ""; + DEVELOPMENT_TEAM = MLHLEFRACM; INFOPLIST_FILE = SDCycleScrollView/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 7.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = com.nuqian.nuqian; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE_SPECIFIER = ""; TARGETED_DEVICE_FAMILY = "1,2"; @@ -575,10 +577,11 @@ ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage; CODE_SIGN_IDENTITY = "iPhone Developer"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; - DEVELOPMENT_TEAM = ""; + DEVELOPMENT_TEAM = MLHLEFRACM; INFOPLIST_FILE = SDCycleScrollView/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 7.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = com.nuqian.nuqian; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE_SPECIFIER = ""; TARGETED_DEVICE_FAMILY = "1,2"; diff --git a/SDCycleScrollView/Info.plist b/SDCycleScrollView/Info.plist index 22062fe1..61054454 100644 --- a/SDCycleScrollView/Info.plist +++ b/SDCycleScrollView/Info.plist @@ -7,7 +7,7 @@ CFBundleExecutable $(EXECUTABLE_NAME) CFBundleIdentifier - -.$(PRODUCT_NAME:rfc1034identifier) + $(PRODUCT_BUNDLE_IDENTIFIER) CFBundleInfoDictionaryVersion 6.0 CFBundleName @@ -22,6 +22,11 @@ 1 LSRequiresIPhoneOS + NSAppTransportSecurity + + NSAllowsArbitraryLoads + + UILaunchStoryboardName LaunchScreen UIRequiredDeviceCapabilities @@ -34,10 +39,5 @@ UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight - NSAppTransportSecurity - - NSAllowsArbitraryLoads - - diff --git a/SDCycleScrollView/Lib/SDCycleScrollView/SDCollectionViewCell.h b/SDCycleScrollView/Lib/SDCycleScrollView/SDCollectionViewCell.h index 3a229bac..aeec33af 100644 --- a/SDCycleScrollView/Lib/SDCycleScrollView/SDCollectionViewCell.h +++ b/SDCycleScrollView/Lib/SDCycleScrollView/SDCollectionViewCell.h @@ -32,16 +32,22 @@ #import + + @interface SDCollectionViewCell : UICollectionViewCell @property (weak, nonatomic) UIImageView *imageView; @property (copy, nonatomic) NSString *title; +@property (nonatomic, copy) NSAttributedString *attributeTitle; @property (nonatomic, strong) UIColor *titleLabelTextColor; @property (nonatomic, strong) UIFont *titleLabelTextFont; @property (nonatomic, strong) UIColor *titleLabelBackgroundColor; @property (nonatomic, assign) CGFloat titleLabelHeight; @property (nonatomic, assign) NSTextAlignment titleLabelTextAlignment; +@property (nonatomic, assign) BOOL textScrollEnable; + +@property (nonatomic, assign) BOOL isOnlyOneLoop; @property (nonatomic, assign) BOOL hasConfigured; diff --git a/SDCycleScrollView/Lib/SDCycleScrollView/SDCollectionViewCell.m b/SDCycleScrollView/Lib/SDCycleScrollView/SDCollectionViewCell.m index 83f8ee06..60c213dd 100644 --- a/SDCycleScrollView/Lib/SDCycleScrollView/SDCollectionViewCell.m +++ b/SDCycleScrollView/Lib/SDCycleScrollView/SDCollectionViewCell.m @@ -32,15 +32,16 @@ #import "SDCollectionViewCell.h" #import "UIView+SDExtension.h" +#define defaultWidth self.frame.size.width //默认label的宽度 超过则滚动 +#define defaultInterval 3.5 //默认时间间隔 +#define delayInterval 1.0 //先定一段时间 延迟滚动 -@implementation SDCollectionViewCell -{ +@implementation SDCollectionViewCell{ __weak UILabel *_titleLabel; } -- (instancetype)initWithFrame:(CGRect)frame -{ +- (instancetype)initWithFrame:(CGRect)frame{ if (self = [super initWithFrame:frame]) { [self setupImageView]; [self setupTitleLabel]; @@ -49,60 +50,91 @@ - (instancetype)initWithFrame:(CGRect)frame return self; } -- (void)setTitleLabelBackgroundColor:(UIColor *)titleLabelBackgroundColor -{ +- (void)setTitleLabelBackgroundColor:(UIColor *)titleLabelBackgroundColor{ _titleLabelBackgroundColor = titleLabelBackgroundColor; _titleLabel.backgroundColor = titleLabelBackgroundColor; } -- (void)setTitleLabelTextColor:(UIColor *)titleLabelTextColor -{ +- (void)setTitleLabelTextColor:(UIColor *)titleLabelTextColor{ _titleLabelTextColor = titleLabelTextColor; _titleLabel.textColor = titleLabelTextColor; } -- (void)setTitleLabelTextFont:(UIFont *)titleLabelTextFont -{ +- (void)setTitleLabelTextFont:(UIFont *)titleLabelTextFont{ _titleLabelTextFont = titleLabelTextFont; _titleLabel.font = titleLabelTextFont; } -- (void)setupImageView -{ +- (void)setupImageView{ UIImageView *imageView = [[UIImageView alloc] init]; _imageView = imageView; [self.contentView addSubview:imageView]; } -- (void)setupTitleLabel -{ - UILabel *titleLabel = [[UILabel alloc] init]; +- (void)setupTitleLabel{ + UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, defaultWidth, self.frame.size.height)]; _titleLabel = titleLabel; _titleLabel.hidden = YES; + _titleLabel.numberOfLines = 1; [self.contentView addSubview:titleLabel]; } -- (void)setTitle:(NSString *)title -{ +- (void)setTitle:(NSString *)title{ _title = [title copy]; - _titleLabel.text = [NSString stringWithFormat:@" %@", title]; + _titleLabel.text = [NSString stringWithFormat:@"%@", title]; if (_titleLabel.hidden) { _titleLabel.hidden = NO; } + + if (self.textScrollEnable ) { + CGFloat width = [self boundingWidthWithString:_titleLabel.text withFont:_titleLabelTextFont withMaxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)].width; + _titleLabel.frame = CGRectMake(0, 0, width, self.frame.size.height); + + [UIView animateWithDuration:defaultInterval delay:delayInterval options:UIViewAnimationOptionTransitionNone animations:^{ + if (width>defaultWidth) { + _titleLabel.frame = CGRectMake(defaultWidth-width, 0, width, self.frame.size.height); + } + } completion:^(BOOL finished) { + }]; + } } --(void)setTitleLabelTextAlignment:(NSTextAlignment)titleLabelTextAlignment -{ +- (void)setAttributeTitle:(NSAttributedString *)attributeTitle { + _attributeTitle = [attributeTitle copy]; + if ([attributeTitle isKindOfClass:[NSAttributedString class]]) { + _titleLabel.attributedText = attributeTitle; + } + if (_titleLabel.hidden) { + _titleLabel.hidden = NO; + } + + if (self.textScrollEnable ) { + CGFloat width = [self boundingWidthWithString:_titleLabel.text withFont:_titleLabelTextFont withMaxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)].width; + _titleLabel.frame = CGRectMake(0, 0, width, self.frame.size.height); + + [UIView animateWithDuration:defaultInterval delay:delayInterval options:UIViewAnimationOptionTransitionNone animations:^{ + if (width>defaultWidth) { + _titleLabel.frame = CGRectMake(defaultWidth-width, 0, width, self.frame.size.height); + } + } completion:^(BOOL finished) { + }]; + } +} + +-(void)setTitleLabelTextAlignment:(NSTextAlignment)titleLabelTextAlignment{ _titleLabelTextAlignment = titleLabelTextAlignment; _titleLabel.textAlignment = titleLabelTextAlignment; } -- (void)layoutSubviews -{ +- (void)layoutSubviews{ [super layoutSubviews]; if (self.onlyDisplayText) { - _titleLabel.frame = self.bounds; + CGFloat width = [self boundingWidthWithString:_titleLabel.text withFont:_titleLabelTextFont withMaxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)].width; + CGRect frame = _titleLabel.frame; + frame.size = CGSizeMake(width, self.frame.size.height); + _titleLabel.frame = frame; + } else { _imageView.frame = self.bounds; CGFloat titleLabelW = self.sd_width; @@ -111,6 +143,13 @@ - (void)layoutSubviews CGFloat titleLabelY = self.sd_height - titleLabelH; _titleLabel.frame = CGRectMake(titleLabelX, titleLabelY, titleLabelW, titleLabelH); } + +} + +-(CGSize)boundingWidthWithString:(NSString *)string withFont:(UIFont *)Font withMaxSize:(CGSize)MaxSize +{ + CGRect rect = [string boundingRectWithSize:MaxSize options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:Font} context:nil]; + return rect.size; } @end diff --git a/SDCycleScrollView/Lib/SDCycleScrollView/SDCycleScrollView.h b/SDCycleScrollView/Lib/SDCycleScrollView/SDCycleScrollView.h index e6a290eb..12f51a16 100644 --- a/SDCycleScrollView/Lib/SDCycleScrollView/SDCycleScrollView.h +++ b/SDCycleScrollView/Lib/SDCycleScrollView/SDCycleScrollView.h @@ -46,6 +46,11 @@ typedef enum { SDCycleScrollViewPageContolStyleNone // 不显示pagecontrol } SDCycleScrollViewPageContolStyle; +typedef enum : NSUInteger { + SDDisplayTypeNormalText, // 显示正常文字 + SDDisplayTypeAttributeText, // 显示attribute文字 +} SDDisplayType; + @class SDCycleScrollView; @protocol SDCycleScrollViewDelegate @@ -58,21 +63,6 @@ typedef enum { /** 图片滚动回调 */ - (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didScrollToIndex:(NSInteger)index; - - - - - -// 不需要自定义轮播cell的请忽略以下两个的代理方法 - -// ========== 轮播自定义cell ========== - -/** 如果你需要自定义cell样式,请在实现此代理方法返回你的自定义cell的class。 */ -- (Class)customCollectionViewCellClassForCycleScrollView:(SDCycleScrollView *)view; - -/** 如果你自定义了cell样式,请在实现此代理方法为你的cell填充数据以及其它一系列设置 */ -- (void)setupCustomCell:(UICollectionViewCell *)cell forIndex:(NSInteger)index cycleScrollView:(SDCycleScrollView *)view; - @end @interface SDCycleScrollView : UIView @@ -91,7 +81,7 @@ typedef enum { + (instancetype)cycleScrollViewWithFrame:(CGRect)frame shouldInfiniteLoop:(BOOL)infiniteLoop imageNamesGroup:(NSArray *)imageNamesGroup; -////////////////////// 数据源API ////////////////////// +////////////////////// 数据源接口 ////////////////////// /** 网络图片 url string 数组 */ @property (nonatomic, strong) NSArray *imageURLStringsGroup; @@ -102,11 +92,7 @@ typedef enum { /** 本地图片数组 */ @property (nonatomic, strong) NSArray *localizationImageNamesGroup; - - - - -////////////////////// 滚动控制API ////////////////////// +////////////////////// 滚动控制接口 ////////////////////// /** 自动滚动间隔时间,默认2s */ @property (nonatomic, assign) CGFloat autoScrollTimeInterval; @@ -131,7 +117,8 @@ typedef enum { /** 解决viewWillAppear时出现时轮播图卡在一半的问题,在控制器viewWillAppear时调用此方法 */ - (void)adjustWhenControllerViewWillAppera; -////////////////////// 自定义样式API ////////////////////// +////////////////////// 自定义样式接口 ////////////////////// + /** 轮播图片的ContentMode,默认为 UIViewContentModeScaleToFill */ @property (nonatomic, assign) UIViewContentMode bannerImageViewContentMode; @@ -148,12 +135,18 @@ typedef enum { /** 只展示文字轮播 */ @property (nonatomic, assign) BOOL onlyDisplayText; +/** 文字超出宽度时是否支持滚动 */ +@property (nonatomic, assign) BOOL textScrollEnable; + /** pagecontrol 样式,默认为动画样式 */ @property (nonatomic, assign) SDCycleScrollViewPageContolStyle pageControlStyle; /** 分页控件位置 */ @property (nonatomic, assign) SDCycleScrollViewPageContolAliment pageControlAliment; +/** 文字显示样式 */ +@property (nonatomic, assign) SDDisplayType displayType; + /** 分页控件距离轮播图的底部间距(在默认间距基础上)的偏移量 */ @property (nonatomic, assign) CGFloat pageControlBottomOffset; @@ -190,11 +183,11 @@ typedef enum { /** 轮播文字label对齐方式 */ @property (nonatomic, assign) NSTextAlignment titleLabelTextAlignment; -/** 滚动手势禁用(文字轮播较实用) */ -- (void)disableScrollGesture; +/** 定时器拿出来启动暂停 */ +@property (nonatomic, weak) NSTimer *timer ; -////////////////////// 清除缓存API ////////////////////// +////////////////////// 清除缓存接口 ////////////////////// /** 清除图片缓存(此次升级后统一使用SDWebImage管理图片加载和缓存) */ + (void)clearImagesCache; @@ -202,4 +195,8 @@ typedef enum { /** 清除图片缓存(兼容旧版本方法) */ - (void)clearCache; +- (void)invalidateTimer; + +- (void)setupTimer; + @end diff --git a/SDCycleScrollView/Lib/SDCycleScrollView/SDCycleScrollView.m b/SDCycleScrollView/Lib/SDCycleScrollView/SDCycleScrollView.m index a2ba78e5..f0a45c6c 100644 --- a/SDCycleScrollView/Lib/SDCycleScrollView/SDCycleScrollView.m +++ b/SDCycleScrollView/Lib/SDCycleScrollView/SDCycleScrollView.m @@ -33,31 +33,27 @@ #import "SDCollectionViewCell.h" #import "UIView+SDExtension.h" #import "TAPageControl.h" -#import "SDWebImageManager.h" #import "UIImageView+WebCache.h" +#import "SDImageCache.h" #define kCycleScrollViewInitialPageControlDotSize CGSizeMake(10, 10) -NSString * const ID = @"SDCycleScrollViewCell"; +NSString * const ID = @"cycleCell"; @interface SDCycleScrollView () - @property (nonatomic, weak) UICollectionView *mainView; // 显示图片的collectionView @property (nonatomic, weak) UICollectionViewFlowLayout *flowLayout; @property (nonatomic, strong) NSArray *imagePathsGroup; -@property (nonatomic, weak) NSTimer *timer; @property (nonatomic, assign) NSInteger totalItemsCount; @property (nonatomic, weak) UIControl *pageControl; - @property (nonatomic, strong) UIImageView *backgroundImageView; // 当imageURLs为空时的背景图 @end @implementation SDCycleScrollView -- (instancetype)initWithFrame:(CGRect)frame -{ +- (instancetype)initWithFrame:(CGRect)frame{ if (self = [super initWithFrame:frame]) { [self initialization]; [self setupMainView]; @@ -65,21 +61,18 @@ - (instancetype)initWithFrame:(CGRect)frame return self; } -- (void)awakeFromNib -{ - [super awakeFromNib]; +- (void)awakeFromNib{ [self initialization]; [self setupMainView]; } -- (void)initialization -{ +- (void)initialization{ _pageControlAliment = SDCycleScrollViewPageContolAlimentCenter; - _autoScrollTimeInterval = 2.0; + _autoScrollTimeInterval = 5.0; _titleLabelTextColor = [UIColor whiteColor]; - _titleLabelTextFont= [UIFont systemFontOfSize:14]; - _titleLabelBackgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5]; - _titleLabelHeight = 30; + _titleLabelTextFont= [UIFont systemFontOfSize:12]; + _titleLabelBackgroundColor = [UIColor whiteColor]; + _titleLabelHeight = 20; _titleLabelTextAlignment = NSTextAlignmentLeft; _autoScroll = YES; _infiniteLoop = YES; @@ -90,10 +83,10 @@ - (void)initialization _pageControlStyle = SDCycleScrollViewPageContolStyleClassic; _hidesForSinglePage = YES; _currentPageDotColor = [UIColor whiteColor]; - _pageDotColor = [UIColor lightGrayColor]; + _pageDotColor = [UIColor colorWithWhite:1 alpha:0.3]; _bannerImageViewContentMode = UIViewContentModeScaleToFill; - - self.backgroundColor = [UIColor lightGrayColor]; + _displayType = SDDisplayTypeNormalText; + self.backgroundColor = [UIColor whiteColor]; } @@ -142,7 +135,6 @@ - (void)setupMainView mainView.showsHorizontalScrollIndicator = NO; mainView.showsVerticalScrollIndicator = NO; [mainView registerClass:[SDCollectionViewCell class] forCellWithReuseIdentifier:ID]; - mainView.dataSource = self; mainView.delegate = self; mainView.scrollsToTop = NO; @@ -153,15 +145,6 @@ - (void)setupMainView #pragma mark - properties -- (void)setDelegate:(id)delegate -{ - _delegate = delegate; - - if ([self.delegate respondsToSelector:@selector(customCollectionViewCellClassForCycleScrollView:)] && [self.delegate customCollectionViewCellClassForCycleScrollView:self]) { - [self.mainView registerClass:[self.delegate customCollectionViewCellClassForCycleScrollView:self] forCellWithReuseIdentifier:ID]; - } -} - - (void)setPlaceholderImage:(UIImage *)placeholderImage { _placeholderImage = placeholderImage; @@ -300,16 +283,22 @@ - (void)setImagePathsGroup:(NSArray *)imagePathsGroup _totalItemsCount = self.infiniteLoop ? self.imagePathsGroup.count * 100 : self.imagePathsGroup.count; - if (imagePathsGroup.count > 1) { // 由于 !=1 包含count == 0等情况 + if (self.scrollDirection == UICollectionViewScrollDirectionHorizontal) { + if (imagePathsGroup.count != 1) { + self.mainView.scrollEnabled = YES; + [self setAutoScroll:self.autoScroll]; + } else { + self.mainView.scrollEnabled = NO; + } + }else{ self.mainView.scrollEnabled = YES; [self setAutoScroll:self.autoScroll]; - } else { - self.mainView.scrollEnabled = NO; - [self setAutoScroll:NO]; } [self setupPageControl]; - [self.mainView reloadData]; + [UIView performWithoutAnimation:^{ + [self.mainView reloadData]; + }]; } - (void)setImageURLStringsGroup:(NSArray *)imageURLStringsGroup @@ -342,6 +331,10 @@ - (void)setTitlesGroup:(NSArray *)titlesGroup { _titlesGroup = titlesGroup; if (self.onlyDisplayText) { + if (_titlesGroup.count>0) { + self.mainView.contentOffset = CGPointMake(0, 0); + } + NSMutableArray *temp = [NSMutableArray new]; for (int i = 0; i < _titlesGroup.count; i++) { [temp addObject:@""]; @@ -351,36 +344,26 @@ - (void)setTitlesGroup:(NSArray *)titlesGroup } } -- (void)disableScrollGesture { - self.mainView.canCancelContentTouches = NO; - for (UIGestureRecognizer *gesture in self.mainView.gestureRecognizers) { - if ([gesture isKindOfClass:[UIPanGestureRecognizer class]]) { - [self.mainView removeGestureRecognizer:gesture]; - } - } -} - #pragma mark - actions -- (void)setupTimer -{ - [self invalidateTimer]; // 创建定时器前先停止定时器,不然会出现僵尸定时器,导致轮播频率错误 - +- (void)setupTimer{ NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:self.autoScrollTimeInterval target:self selector:@selector(automaticScroll) userInfo:nil repeats:YES]; _timer = timer; [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes]; } -- (void)invalidateTimer -{ +- (void)invalidateTimer{ [_timer invalidate]; _timer = nil; } -- (void)setupPageControl -{ +- (void)setupPageControl{ if (_pageControl) [_pageControl removeFromSuperview]; // 重新加载数据时调整 + if (self.onlyDisplayText) { + _mainView.scrollEnabled = NO; + } + if (self.imagePathsGroup.count == 0 || self.onlyDisplayText) return; if ((self.imagePathsGroup.count == 1) && self.hidesForSinglePage) return; @@ -426,17 +409,14 @@ - (void)setupPageControl } } - -- (void)automaticScroll -{ +- (void)automaticScroll{ if (0 == _totalItemsCount) return; int currentIndex = [self currentIndex]; int targetIndex = currentIndex + 1; [self scrollToIndex:targetIndex]; } -- (void)scrollToIndex:(int)targetIndex -{ +- (void)scrollToIndex:(int)targetIndex{ if (targetIndex >= _totalItemsCount) { if (self.infiniteLoop) { targetIndex = _totalItemsCount * 0.5; @@ -447,8 +427,7 @@ - (void)scrollToIndex:(int)targetIndex [_mainView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:targetIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:YES]; } -- (int)currentIndex -{ +- (int)currentIndex{ if (_mainView.sd_width == 0 || _mainView.sd_height == 0) { return 0; } @@ -463,27 +442,21 @@ - (int)currentIndex return MAX(0, index); } -- (int)pageControlIndexWithCurrentCellIndex:(NSInteger)index -{ +- (int)pageControlIndexWithCurrentCellIndex:(NSInteger)index{ return (int)index % self.imagePathsGroup.count; } -- (void)clearCache -{ +- (void)clearCache{ [[self class] clearImagesCache]; } -+ (void)clearImagesCache -{ - [[[SDWebImageManager sharedManager] imageCache] clearDiskOnCompletion:nil]; ++ (void)clearImagesCache{ + [[[SDWebImageManager sharedManager] imageCache] clearMemory]; } #pragma mark - life circles -- (void)layoutSubviews -{ - self.delegate = self.delegate; - +- (void)layoutSubviews{ [super layoutSubviews]; _flowLayout.itemSize = self.frame.size; @@ -564,18 +537,40 @@ - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSe return _totalItemsCount; } +- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(SDCollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{ + long itemIndex = [self pageControlIndexWithCurrentCellIndex:indexPath.item]; + if (self.scrollDirection == UICollectionViewScrollDirectionVertical) { + if (_titlesGroup.count && itemIndex < _titlesGroup.count) { + if (self.displayType == SDDisplayTypeAttributeText) { + if (_titlesGroup.count <= 1) { + cell.isOnlyOneLoop = YES; + }else{ + cell.isOnlyOneLoop = NO; + } + cell.attributeTitle = _titlesGroup[itemIndex]; + }else { + cell.title = _titlesGroup[itemIndex]; + } + } + } +} + - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { SDCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath]; - - long itemIndex = [self pageControlIndexWithCurrentCellIndex:indexPath.item]; - - if ([self.delegate respondsToSelector:@selector(setupCustomCell:forIndex:cycleScrollView:)] && - [self.delegate respondsToSelector:@selector(customCollectionViewCellClassForCycleScrollView:)] && - [self.delegate customCollectionViewCellClassForCycleScrollView:self]) { - [self.delegate setupCustomCell:cell forIndex:itemIndex cycleScrollView:self]; - return cell; + if (!cell.hasConfigured) { + cell.titleLabelBackgroundColor = self.titleLabelBackgroundColor; + cell.titleLabelHeight = self.titleLabelHeight; + cell.titleLabelTextAlignment = self.titleLabelTextAlignment; + cell.titleLabelTextColor = self.titleLabelTextColor; + cell.titleLabelTextFont = self.titleLabelTextFont; + cell.hasConfigured = YES; + cell.textScrollEnable = self.textScrollEnable; + cell.imageView.contentMode = self.bannerImageViewContentMode; + cell.clipsToBounds = YES; + cell.onlyDisplayText = self.onlyDisplayText; } + long itemIndex = [self pageControlIndexWithCurrentCellIndex:indexPath.item]; NSString *imagePath = self.imagePathsGroup[itemIndex]; @@ -592,23 +587,6 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cell } else if (!self.onlyDisplayText && [imagePath isKindOfClass:[UIImage class]]) { cell.imageView.image = (UIImage *)imagePath; } - - if (_titlesGroup.count && itemIndex < _titlesGroup.count) { - cell.title = _titlesGroup[itemIndex]; - } - - if (!cell.hasConfigured) { - cell.titleLabelBackgroundColor = self.titleLabelBackgroundColor; - cell.titleLabelHeight = self.titleLabelHeight; - cell.titleLabelTextAlignment = self.titleLabelTextAlignment; - cell.titleLabelTextColor = self.titleLabelTextColor; - cell.titleLabelTextFont = self.titleLabelTextFont; - cell.hasConfigured = YES; - cell.imageView.contentMode = self.bannerImageViewContentMode; - cell.clipsToBounds = YES; - cell.onlyDisplayText = self.onlyDisplayText; - } - return cell; } @@ -625,8 +603,7 @@ - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPa #pragma mark - UIScrollViewDelegate -- (void)scrollViewDidScroll:(UIScrollView *)scrollView -{ +- (void)scrollViewDidScroll:(UIScrollView *)scrollView{ if (!self.imagePathsGroup.count) return; // 解决清除timer时偶尔会出现的问题 int itemIndex = [self currentIndex]; int indexOnPageControl = [self pageControlIndexWithCurrentCellIndex:itemIndex]; @@ -640,31 +617,31 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView } } -- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView -{ - if (self.autoScroll) { - [self invalidateTimer]; +- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{ + if (!self.onlyDisplayText ) { + if (self.autoScroll) { + [self invalidateTimer]; + } } } -- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate -{ - if (self.autoScroll) { - [self setupTimer]; +- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{ + if (!self.onlyDisplayText ) { + if (self.autoScroll) { + [self setupTimer]; + } } + } -- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView -{ +- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ [self scrollViewDidEndScrollingAnimation:self.mainView]; } -- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView -{ +- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{ if (!self.imagePathsGroup.count) return; // 解决清除timer时偶尔会出现的问题 int itemIndex = [self currentIndex]; int indexOnPageControl = [self pageControlIndexWithCurrentCellIndex:itemIndex]; - if ([self.delegate respondsToSelector:@selector(cycleScrollView:didScrollToIndex:)]) { [self.delegate cycleScrollView:self didScrollToIndex:indexOnPageControl]; } else if (self.itemDidScrollOperationBlock) { diff --git a/SDCycleScrollView/ViewController.m b/SDCycleScrollView/ViewController.m index 53303212..00a45716 100644 --- a/SDCycleScrollView/ViewController.m +++ b/SDCycleScrollView/ViewController.m @@ -45,122 +45,58 @@ @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; - - self.view.backgroundColor = [UIColor colorWithRed:0.98 green:0.98 blue:0.98 alpha:0.99]; - UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"005.jpg"]]; - backgroundView.frame = self.view.bounds; - [self.view addSubview:backgroundView]; - - UIScrollView *demoContainerView = [[UIScrollView alloc] initWithFrame:self.view.frame]; - demoContainerView.contentSize = CGSizeMake(self.view.frame.size.width, 1200); - [self.view addSubview:demoContainerView]; - + self.view.backgroundColor = [UIColor whiteColor]; self.title = @"轮播Demo"; - - // 情景一:采用本地图片实现 - NSArray *imageNames = @[@"h1.jpg", - @"h2.jpg", - @"h3.jpg", - @"h4.jpg", - @"h7" // 本地图片请填写全名 - ]; - - // 情景二:采用网络图片实现 - NSArray *imagesURLStrings = @[ - @"https://ss2.baidu.com/-vo3dSag_xI4khGko9WTAnF6hhy/super/whfpf%3D425%2C260%2C50/sign=a4b3d7085dee3d6d2293d48b252b5910/0e2442a7d933c89524cd5cd4d51373f0830200ea.jpg", - @"https://ss0.baidu.com/-Po3dSag_xI4khGko9WTAnF6hhy/super/whfpf%3D425%2C260%2C50/sign=a41eb338dd33c895a62bcb3bb72e47c2/5fdf8db1cb134954a2192ccb524e9258d1094a1e.jpg", - @"http://c.hiphotos.baidu.com/image/w%3D400/sign=c2318ff84334970a4773112fa5c8d1c0/b7fd5266d0160924c1fae5ccd60735fae7cd340d.jpg" - ]; - _imagesURLStrings = imagesURLStrings; - - // 情景三:图片配文字 - NSArray *titles = @[@"新建交流QQ群:185534916 ", - @"disableScrollGesture可以设置禁止拖动", - @"感谢您的支持,如果下载的", - @"如果代码在使用过程中出现问题", - @"您可以发邮件到gsdios@126.com" - ]; + NSArray *titleArr = @[@"如果要实现自定义cell的轮播图,必须先实现代理方法",@"由于模拟器的渲染问题,如果发现轮播时有一条线不必处理,模拟器放大到100%或者真机调试是不会出现那条线的",@"网络加载 --- 创建只上下滚动展示文字的轮播器",@"新建交流QQ群:185534916,感谢您的支持,如果下载的,如果代码在使用过程中出现问题,您可以发邮件到gsdios@126.com,disableScrollGesture可以设置禁止拖动"]; CGFloat w = self.view.bounds.size.width; - - -// >>>>>>>>>>>>>>>>>>>>>>>>> demo轮播图1 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> - - // 本地加载 --- 创建不带标题的图片轮播器 - SDCycleScrollView *cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 64, w, 180) shouldInfiniteLoop:YES imageNamesGroup:imageNames]; - cycleScrollView.delegate = self; - cycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated; - [demoContainerView addSubview:cycleScrollView]; - cycleScrollView.scrollDirection = UICollectionViewScrollDirectionVertical; - // --- 轮播时间间隔,默认1.0秒,可自定义 - //cycleScrollView.autoScrollTimeInterval = 4.0; - - -// >>>>>>>>>>>>>>>>>>>>>>>>> demo轮播图2 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> - - // 网络加载 --- 创建带标题的图片轮播器 - SDCycleScrollView *cycleScrollView2 = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 280, w, 180) delegate:self placeholderImage:[UIImage imageNamed:@"placeholder"]]; - - cycleScrollView2.pageControlAliment = SDCycleScrollViewPageContolAlimentRight; - cycleScrollView2.titlesGroup = titles; - cycleScrollView2.currentPageDotColor = [UIColor whiteColor]; // 自定义分页控件小圆标颜色 - [demoContainerView addSubview:cycleScrollView2]; - - // --- 模拟加载延迟 - dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ - cycleScrollView2.imageURLStringsGroup = imagesURLStrings; - }); - - /* - block监听点击方式 - - cycleScrollView2.clickItemOperationBlock = ^(NSInteger index) { - NSLog(@">>>>> %ld", (long)index); - }; - - */ - - -// >>>>>>>>>>>>>>>>>>>>>>>>> demo轮播图3 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> - - // 网络加载 --- 创建自定义图片的pageControlDot的图片轮播器 - SDCycleScrollView *cycleScrollView3 = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 500, w, 180) delegate:self placeholderImage:[UIImage imageNamed:@"placeholder"]]; - cycleScrollView3.currentPageDotImage = [UIImage imageNamed:@"pageControlCurrentDot"]; - cycleScrollView3.pageDotImage = [UIImage imageNamed:@"pageControlDot"]; - cycleScrollView3.imageURLStringsGroup = imagesURLStrings; - - [demoContainerView addSubview:cycleScrollView3]; - -// >>>>>>>>>>>>>>>>>>>>>>>>> demo轮播图4 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> - - // 网络加载 --- 创建只上下滚动展示文字的轮播器 - // 由于模拟器的渲染问题,如果发现轮播时有一条线不必处理,模拟器放大到100%或者真机调试是不会出现那条线的 - SDCycleScrollView *cycleScrollView4 = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 750, w, 40) delegate:self placeholderImage:nil]; + // >>>>>>>>>>>>>>>>>>>>>>>>> demo轮播图1 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> + + //富文本+超出固定宽度滚动 参数请到SDCollectionViewCell里面改(固定宽度 ,滚动间隔 ,延迟时间) + SDCycleScrollView *cycleScrollView1 = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 80, w, 20) delegate:self placeholderImage:nil]; + cycleScrollView1.backgroundColor = [UIColor whiteColor]; + cycleScrollView1.scrollDirection = UICollectionViewScrollDirectionVertical; + cycleScrollView1.onlyDisplayText = YES; + cycleScrollView1.textScrollEnable = YES; + cycleScrollView1.titleLabelTextColor = [UIColor blackColor]; + + NSMutableArray *attributeTitleArray = [NSMutableArray new]; + for (int i = 0; i < titleArr.count; i++) { + NSString *titleStr = titleArr[i]; + NSMutableAttributedString *attr = [[NSMutableAttributedString alloc]initWithString:titleStr]; + [attr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(2, 3)]; + [attributeTitleArray addObject:attr]; + } + cycleScrollView1.displayType = SDDisplayTypeAttributeText; + cycleScrollView1.titlesGroup = [attributeTitleArray copy]; + + [self.view addSubview:cycleScrollView1]; + + //正常文字+超出固定宽度滚动 + SDCycleScrollView *cycleScrollView2 = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 120, w, 20) delegate:self placeholderImage:nil]; + cycleScrollView2.backgroundColor = [UIColor whiteColor]; + cycleScrollView2.scrollDirection = UICollectionViewScrollDirectionVertical; + cycleScrollView2.onlyDisplayText = YES; + cycleScrollView2.textScrollEnable = YES; + cycleScrollView2.titleLabelTextColor = [UIColor blackColor]; + cycleScrollView2.displayType = SDDisplayTypeNormalText; + cycleScrollView2.titlesGroup = titleArr; + [self.view addSubview:cycleScrollView2]; + + //正常文字+不滚动 + SDCycleScrollView *cycleScrollView4 = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 160, w, 20) delegate:self placeholderImage:nil]; + cycleScrollView4.backgroundColor = [UIColor whiteColor]; cycleScrollView4.scrollDirection = UICollectionViewScrollDirectionVertical; cycleScrollView4.onlyDisplayText = YES; + //设置是否滚动 + cycleScrollView4.textScrollEnable = NO; + cycleScrollView4.titleLabelTextColor = [UIColor blackColor]; + cycleScrollView4.displayType = SDDisplayTypeNormalText; + cycleScrollView4.titlesGroup = titleArr; + [self.view addSubview:cycleScrollView4]; - NSMutableArray *titlesArray = [NSMutableArray new]; - [titlesArray addObject:@"纯文字上下滚动轮播"]; - [titlesArray addObject:@"纯文字上下滚动轮播 -- demo轮播图4"]; - [titlesArray addObjectsFromArray:titles]; - cycleScrollView4.titlesGroup = [titlesArray copy]; - [cycleScrollView4 disableScrollGesture]; - - [demoContainerView addSubview:cycleScrollView4]; - - -// >>>>>>>>>>>>>>>>>>>>>>>>> demo轮播图5 自定义cell的轮播图 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> - - // 如果要实现自定义cell的轮播图,必须先实现customCollectionViewCellClassForCycleScrollView:和setupCustomCell:forIndex:代理方法 - - _customCellScrollViewDemo = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 820, w, 120) delegate:self placeholderImage:[UIImage imageNamed:@"placeholder"]]; - _customCellScrollViewDemo.currentPageDotImage = [UIImage imageNamed:@"pageControlCurrentDot"]; - _customCellScrollViewDemo.pageDotImage = [UIImage imageNamed:@"pageControlDot"]; - _customCellScrollViewDemo.imageURLStringsGroup = imagesURLStrings; - - [demoContainerView addSubview:_customCellScrollViewDemo]; } - (void)viewWillAppear:(BOOL)animated @@ -168,7 +104,7 @@ - (void)viewWillAppear:(BOOL)animated [super viewWillAppear:animated]; // 如果你发现你的CycleScrollview会在viewWillAppear时图片卡在中间位置,你可以调用此方法调整图片位置 -// [你的CycleScrollview adjustWhenControllerViewWillAppera]; + // [你的CycleScrollview adjustWhenControllerViewWillAppera]; } @@ -180,39 +116,6 @@ - (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtInde [self.navigationController pushViewController:[NSClassFromString(@"DemoVCWithXib") new] animated:YES]; } - - -/* - -// 滚动到第几张图回调 -- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didScrollToIndex:(NSInteger)index -{ - NSLog(@">>>>>> 滚动到第%ld张图", (long)index); -} - - */ - - - - - - - - - - - - - - - - - - - - - - // 不需要自定义轮播cell的请忽略下面的代理方法