移动开发平台 mPaaS 轮播组件

By | 2021年4月23日

AUBannerView 为图片轮播组件。

效果图

接口说明

  
  1. typedef NS_ENUM(NSUInteger, AUBannerStyle) {
  2. AUBannerStyleDeepColor, // 深色样式
  3. AUBannerStyleLightColor // 浅色样式
  4. };
  5. @interface AUBannerViewConfig : NSObject
  6. @property (nonatomic, assign) AUBannerStyle style; // 默认的样式
  7. @property (nonatomic, strong) UIColor *pageControlNormalColor; // 默认色
  8. @property (nonatomic, strong) UIColor *pageControlSelectedColor; // 选中色
  9. @property (nonatomic, assign) CGFloat pageControlMarginBottom; // 分页标识距离底部的margin
  10. @property (nonatomic, assign) BOOL pageControlDotTapEnabled; // 分页标识圆点是否支持点击(默认为NO)
  11. @property (nonatomic, assign) UIEdgeInsets contentViewMargin; // 内容区边距
  12. @property (nonatomic, assign) UIEdgeInsets contentViewPadding; // 内容空白区,滚动时会经过该区域
  13. @property (nonatomic, assign) BOOL autoTurn; // 自动轮播(默认为 YES)
  14. @property (nonatomic, assign) BOOL autoStartTurn; // 自动开始轮播
  15. @property (nonatomic, assign) CGFloat duration; // 自动轮播间隔
  16. @end
  17. @class AUBannerView;
  18. @protocol AUBannerViewDelegate <NSObject>
  19. @required
  20. - (NSInteger)numberOfItemsInBannerView:(AUBannerView *)bannerView;
  21. - (UIView *)bannerView:(AUBannerView *)bannerView itemViewAtPos:(NSInteger)pos;
  22. @optional
  23. - (void)bannerView:(AUBannerView *)bannerView didTapedItemAtPos:(NSInteger)pos;
  24. - (CGFloat)bannerView:(AUBannerView *)bannerView durationOfItemAtPos:(NSInteger)pos;
  25. @end
  26. @interface AUBannerView : UIView
  27. AU_UNAVAILABLE_INIT
  28. @property (nonatomic, readonly) UIView *contentView; // 内容区视图
  29. @property (nonatomic, readonly) AUPageControl *pageControl; // 分页标识视图
  30. @property (nonatomic, copy) NSString *bizType; // 业务标识
  31. @property (nonatomic, assign) NSInteger currentPage; // 当前页码(从 0 开始)
  32. @property (nonatomic, weak) id<AUBannerViewDelegate> delegate; // 数据源和事件代理
  33. /**
  34. 创建banner视图
  35. @param frame frame
  36. @param bizType 业务标识(不能为空)
  37. @param configOperation 配置 block
  38. @return banner 视图
  39. */
  40. - (instancetype)initWithFrame:(CGRect)frame
  41. bizType:(NSString *)bizType
  42. makeConfig:(void(^)(AUBannerViewConfig *config))configOperation;
  43. /**
  44. 开始自动轮播(如果 autoStartTurn 设置为 NO,才需要调用这个方法)
  45. */
  46. - (void)startTurning;
  47. /**
  48. 重新加载 banner(数据源变更,需调用此方法重新加载数据)
  49. */
  50. - (void)reloadData;
  51. @end
  52. //################################
  53. //####### UIImage ################
  54. //################################
  55. @interface AUBannerView (Image)
  56. /**
  57. 创建图片的banner视图
  58. 注意:需要保持 imageURLs 和 actionURLs 相等,否则创建失败
  59. @param frame frame
  60. @param bizType 业务标识(不能为空)
  61. @param images 图片集合(可为图片链接字符串,或者 image 对象)
  62. @param placeholder 图片占位图(UIImage 对象)
  63. @param actionURLs 图片点击后的跳转链接(字符串,如果某张图不支持跳转,请设置 [NSNull null])
  64. @param configOperation banner 视图的配置参数
  65. @return 轮播图片的 banner 视图
  66. */
  67. + (instancetype)bannerViewWithFrame:(CGRect)frame
  68. bizType:(NSString *)bizType
  69. images:(NSArray *)images
  70. placeholder:(UIImage *)placeholder
  71. actionURLs:(NSArray *)actionURLs
  72. makeConfig:(void(^)(AUBannerViewConfig *config))configOperation;
  73. @end
  74. //################################
  75. //####### Extension ##############
  76. //################################
  77. @interface AUBannerView (Extension)
  78. /**
  79. 更新 bannerview 配置
  80. 会自动触发 reload 事件
  81. @param update update 的 block
  82. */
  83. - (void)updateConfigOperation:(void(^)(AUBannerViewConfig *config))update;
  84. @end

代码示例

  
  1. // 普通 banner(深色)
  2. for (NSInteger i = 0; i < 1; i ++) {
  3. CGRect rect = CGRectMake(10, 10 + (height + spaceY) * i, self.view.width - 20, height);
  4. AUBannerView *bannerView = [[AUBannerView alloc] initWithFrame:rect
  5. bizType:@"demo"
  6. makeConfig:^(AUBannerViewConfig *config)
  7. {
  8. config.duration = 1.5;
  9. // config.contentViewMargin = UIEdgeInsetsMake(5, 5, 10, 5);
  10. // config.contentViewPadding = UIEdgeInsetsMake(0, 50, 0, 50);
  11. config.style = AUBannerStyleDeepColor;
  12. config.autoTurn = YES;
  13. config.autoStartTurn = YES;
  14. }];
  15. bannerView.delegate = self;
  16. bannerView.tag = 1;
  17. bannerView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.1];
  18. [self.view addSubview:bannerView];
  19. }
  20. // 普通 banner(浅色)
  21. for (NSInteger i = 1; i < 2; i ++) {
  22. CGRect rect = CGRectMake(10, 10 + (height + spaceY) * i, self.view.width - 20, height);
  23. AUBannerView *bannerView = [[AUBannerView alloc] initWithFrame:rect
  24. bizType:@"demo"
  25. makeConfig:^(AUBannerViewConfig *config)
  26. {
  27. config.duration = 1.5;
  28. config.style = AUBannerStyleLightColor;
  29. config.autoTurn = NO;
  30. config.pageControlDotTapEnabled = YES;
  31. }];
  32. bannerView.delegate = self;
  33. bannerView.tag = 2;
  34. bannerView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.1];
  35. [self.view addSubview:bannerView];
  36. }
  37. // 纯图片banner
  38. for (NSInteger i = 2; i < 3; i ++) {
  39. CGRect rect = CGRectMake(10, 10 + (height + spaceY) * i, self.view.width - 20, height);
  40. NSMutableArray *images = [NSMutableArray array];
  41. for (NSInteger j = 0; j < 5; j ++) {
  42. UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.jpg", @(j + 1)]];
  43. [images addObject:image];
  44. }
  45. AUBannerView *bannerView = [AUBannerView bannerViewWithFrame:rect
  46. bizType:@"demo"
  47. images:images
  48. placeholder:nil
  49. actionURLs:nil
  50. makeConfig:NULL];
  51. bannerView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.1];
  52. [self.view addSubview:bannerView];
  53. }
  
  1. #pragma mark - AUBannerViewDelegate
  2. - (NSInteger)numberOfItemsInBannerView:(AUBannerView *)bannerView
  3. {
  4. return bannerView.tag == 1 ? 2 : 4;
  5. }
  6. - (UIView *)bannerView:(AUBannerView *)bannerView itemViewAtPos:(NSInteger)pos
  7. {
  8. NSArray *array = nil;
  9. // 深色
  10. if (bannerView.tag == 1) {
  11. array = @[RGB(0x108EE9), RGB_A(0x108EE9, 0.5), [UIColor blueColor], [UIColor yellowColor]];
  12. }
  13. // 浅色
  14. else {
  15. array = @[RGB(0xfFFFFF),RGB_A(0xeFFFFF, 0.7),RGB(0xcFFFFF),RGB_A(0xeFFFFF, 0.5),RGB_A(0xeFFFFF, 0.9)];
  16. }
  17. UIView *view = [[UIView alloc] init];
  18. view.backgroundColor = array[pos];
  19. return view;
  20. }
  21. - (void)bannerView:(AUBannerView *)bannerView didTapedItemAtPos:(NSInteger)pos
  22. {
  23. NSLog(@"didTapedItemAtPos %@", @(pos));
  24. }
  25. //- (CGFloat)bannerView:(AUBannerView *)bannerView durationOfItemAtPos:(NSInteger)pos
  26. //{
  27. // return 1;
  28. //}

请关注公众号获取更多资料

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注