All Products
Search
Document Center

Carousel component

Last Updated: May 25, 2021

AUBannerView is a carousel component.

Sample image

API description

 
  1. typedef NS_ENUM(NSUInteger, AUBannerStyle) {
  2. AUBannerStyleDeepColor, // The deep color style.
  3. AUBannerStyleLightColor // The light color style.
  4. };
  5. @interface AUBannerViewConfig : NSObject
  6. @property (nonatomic, assign) AUBannerStyle style; // The default style.
  7. @property (nonatomic, strong) UIColor *pageControlNormalColor; // The default color.
  8. @property (nonatomic, strong) UIColor *pageControlSelectedColor; // The selected color.
  9. @property (nonatomic, assign) CGFloat pageControlMarginBottom; // The margin between the pagination identifier and bottom.
  10. @property (nonatomic, assign) BOOL pageControlDotTapEnabled; // Specify whether the pagination identifier (dot) is clickable. The default value is NO.
  11. @property (nonatomic, assign) UIEdgeInsets contentViewMargin; // The margin of the content area.
  12. @property (nonatomic, assign) UIEdgeInsets contentViewPadding; // The padding of the content area. An image will pass the padding when it scrolls.
  13. @property (nonatomic, assign) BOOL autoTurn; // Whether to enable automatic carousel. Default value: YES.
  14. @property (nonatomic, assign) BOOL autoStartTurn; // Whether to automatically start carousel.
  15. @property (nonatomic, assign) CGFloat duration; // The automatic carousel interval.
  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; // The content area view.
  29. @property (nonatomic, readonly) AUPageControl *pageControl; // The pagination identifier view.
  30. @property (nonatomic, copy) NSString *bizType; // The business type.
  31. @property (nonatomic, assign) NSInteger currentPage; // The current page, which starts from page 0.
  32. @property (nonatomic, weak) id<AUBannerViewDelegate> delegate; // The data source and event delegate.
  33. /**
  34. Create a banner view.
  35. @param frame frame
  36. @param bizType The business type, which cannot be left empty.
  37. @param configOperation The configuration block.
  38. @return The banner view.
  39. */
  40. - (instancetype)initWithFrame:(CGRect)frame
  41. bizType:(NSString *)bizType
  42. makeConfig:(void(^)(AUBannerViewConfig *config))configOperation;
  43. /**
  44. Start automatic carousel. (Call this method only when autoStartTurn is set to NO.)
  45. */
  46. - (void)startTurning;
  47. /**
  48. Reload the banner. (Call this method to reload data when the data source changes.)
  49. */
  50. - (void)reloadData;
  51. @end
  52. //################################
  53. //####### UIImage ################
  54. //################################
  55. @interface AUBannerView (Image)
  56. /**
  57. Create a banner view for images.
  58. Note: Ensure that the value of images is the same as that of actionURLs, or the banner view will fail to be created.
  59. @param frame The frame.
  60. @param bizType The business type, which cannot be left empty.
  61. @param images The image set, which can be an array of image link strings or image objects.
  62. @param placeholder The image placeholder, or the UIImage object.
  63. @param actionURLs The link to which a user is redirected after the user taps the corresponding image. The link is a string. If an image does not support redirection, set this parameter to [NSNull null].
  64. @param configOperation The banner view configuration parameter.
  65. @return The banner view of image carousel.
  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. Update the banner view configuration.
  80. A reloading event will be automatically triggered.
  81. @param update The update block.
  82. */
  83. - (void)updateConfigOperation:(void(^)(AUBannerViewConfig *config))update;
  84. @end

Code sample

 
  1. // The common deep color 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. // The common light color 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. // The banner with images only.
  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. // The deep color.
  10. if (bannerView.tag == 1) {
  11. array = @[RGB(0x108EE9), RGB_A(0x108EE9, 0.5), [UIColor blueColor], [UIColor yellowColor]];
  12. }
  13. // The light color.
  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. //}