Objective C iOS simple image Carousel Class


/ Published in: Objective C
Save to your folder(s)



Copy this code and paste it in your HTML
  1. //
  2. // Carousel.h
  3. //
  4.  
  5. #import <UIKit/UIKit.h>
  6.  
  7. @interface Carousel : UIView <UIScrollViewDelegate>
  8. {
  9. UIPageControl *pageControl;
  10. NSArray *images;
  11. }
  12.  
  13. @property (nonatomic, retain) UIPageControl *pageControl;
  14. @property (nonatomic, retain) NSArray *images;
  15.  
  16. - (void)setup;
  17.  
  18. @end
  19.  
  20. //
  21. // Carousel.m
  22. //
  23.  
  24. #import "Carousel.h"
  25.  
  26. @implementation Carousel
  27.  
  28. @synthesize pageControl;
  29. @synthesize images;
  30.  
  31. #pragma mark - Override images setter
  32.  
  33. - (void)setImages:(NSArray *)newImages
  34. {
  35. if (newImages != images)
  36. {
  37. [newImages retain];
  38. [images release];
  39. images = newImages;
  40.  
  41. [self setup];
  42. }
  43. }
  44.  
  45. #pragma mark - Carousel setup
  46.  
  47. - (void)setup
  48. {
  49. UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.frame];
  50. [scrollView setDelegate:self];
  51. [scrollView setShowsHorizontalScrollIndicator:NO];
  52. [scrollView setPagingEnabled:YES];
  53. [scrollView setBounces:NO];
  54.  
  55. CGSize scrollViewSize = scrollView.frame.size;
  56.  
  57. for (NSInteger i = 0; i < [self.images count]; i++)
  58. {
  59. CGRect slideRect = CGRectMake(scrollViewSize.width * i, 0, scrollViewSize.width, scrollViewSize.height);
  60.  
  61. UIView *slide = [[UIView alloc] initWithFrame:slideRect];
  62. [slide setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0]];
  63.  
  64. UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.frame];
  65. [imageView setImage:[UIImage imageNamed:[self.images objectAtIndex:i]]];
  66. [slide addSubview:imageView];
  67. [imageView release];
  68.  
  69. [scrollView addSubview:slide];
  70. [slide release];
  71. }
  72.  
  73. UIPageControl *tempPageControll = [[UIPageControl alloc] initWithFrame:CGRectMake(0, scrollViewSize.height - 20, scrollViewSize.width, 20)];
  74. [self setPageControl:tempPageControll];
  75. [tempPageControll release];
  76. [self.pageControl setNumberOfPages:[self.images count]];
  77. [scrollView setContentSize:CGSizeMake(scrollViewSize.width * [self.images count], scrollViewSize.height)];
  78.  
  79. [self addSubview:scrollView];
  80. [scrollView release];
  81. [self addSubview:self.pageControl];
  82. }
  83.  
  84. #pragma mark - UIScrollViewDelegate
  85.  
  86. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  87. {
  88. CGFloat pageWidth = scrollView.frame.size.width;
  89. int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
  90. [self.pageControl setCurrentPage:page];
  91. }
  92.  
  93. #pragma mark - Cleanup
  94.  
  95. - (void)dealloc
  96. {
  97. [pageControl release];
  98. [images release];
  99. [super dealloc];
  100. }
  101.  
  102. @end
  103.  
  104. //
  105. // ViewController.m
  106. // SimpleCarousel
  107. //
  108.  
  109. #import "ViewController.h"
  110. #import "Carousel.h"
  111.  
  112. @implementation ViewController
  113.  
  114. - (void)viewDidLoad
  115. {
  116. [super viewDidLoad];
  117.  
  118. // Initialize carousel object with frame size of images
  119. Carousel *carousel = [[Carousel alloc] initWithFrame:CGRectMake(0, 0, 320, 205)];
  120.  
  121. // Add some images to carousel, we are passing autoreleased NSArray
  122. [carousel setImages:[NSArray arrayWithObjects:@"image1.jpg", @"image2.jpg", @"image3.jpg", nil]];
  123.  
  124. // Add carousel to view
  125. [self.view addSubview:carousel];
  126.  
  127. // Cleanup
  128. [carousel release];
  129. }
  130.  
  131. @end

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.