由于稍微累了,就直接贴粗糙实现的代码了(没有做一些边界检查等),算是backup。
// // CCImageScrollView.h // CCImageScrollView // // Created by Jason Lee on 12-6-13. // Copyright (c) 2012年 _XXX_. All rights reserved. // #import <UIKit/UIKit.h> @interface CCImageScrollView : UIView <UIScrollViewDelegate> @property (nonatomic, retain) NSArray *imageArray; - (id)initWithFrame:(CGRect)frame pageNum:(NSInteger)pageNum pageEnabled:(BOOL)pageEnabled; @end
// // CCImageScrollView.m // CCImageScrollView // // Created by Jason Lee on 12-6-13. // Copyright (c) 2012年 _XXX_. All rights reserved. // #import "CCImageScrollView.h" @interface CCImageScrollView () @property (nonatomic, retain) NSMutableArray *imageViewArray; @property (nonatomic, assign) NSInteger page; @property (nonatomic, assign) BOOL pageEnabled; @property (nonatomic, retain) UIScrollView *scrollView; @property (nonatomic, retain) UIPageControl *pageControl; @end @implementation CCImageScrollView @synthesize imageArray = _imageArray; @synthesize imageViewArray = _imageViewArray; @synthesize page = _page; @synthesize pageEnabled = _pageEnabled; @synthesize scrollView = _scrollView; @synthesize pageControl = _pageControl; - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code } return self; } - (id)initWithFrame:(CGRect)frame pageNum:(NSInteger)pageNum pageEnabled:(BOOL)pageEnabled { self = [super initWithFrame:frame]; if (self) { // Initialization code self.backgroundColor = [UIColor blackColor]; // Default background-color self.page = pageNum; self.pageEnabled = pageEnabled; _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; [self.scrollView setContentSize:CGSizeMake(self.page * self.frame.size.width, self.frame.size.height)]; self.scrollView.scrollEnabled = YES; self.scrollView.pagingEnabled = YES; self.scrollView.showsHorizontalScrollIndicator = NO; self.scrollView.showsVerticalScrollIndicator = NO; self.scrollView.delegate = self; int defaultPageControlHeight = 36; _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.frame.size.height - defaultPageControlHeight, self.frame.size.width, defaultPageControlHeight)]; [self.pageControl setNumberOfPages:self.page]; if (!self.pageEnabled) self.pageControl.hidden = YES; [self addSubview:self.scrollView]; [self addSubview:self.pageControl]; } return self; } - (void)dealloc { [_scrollView release]; [_pageControl release]; [_imageArray release]; [_imageViewArray release]; // [super dealloc]; } /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ - (void)loadImage { if (nil == self.imageViewArray) { self.imageViewArray = [NSMutableArray arrayWithCapacity:self.page]; for (int i = 0; i < self.page; ++i) { UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * self.frame.size.width, 0, self.frame.size.width, self.frame.size.height)]; imageView.contentMode = UIViewContentModeScaleAspectFit; [self.scrollView addSubview:imageView]; [self.imageViewArray addObject:imageView]; [imageView release], imageView = nil; } } for (int i = 0; i < self.page; ++i) { UIImageView *imageView = [self.imageViewArray objectAtIndex:i]; imageView.image = [UIImage imageNamed:[self.imageArray objectAtIndex:i]]; } } #pragma mark - setter & getter - (void)setImageArray:(NSArray *)imageArray { if (_imageArray == imageArray) { return ; } [_imageArray release]; _imageArray = imageArray; [_imageArray retain]; [self loadImage]; } #pragma mark - UIScrollView Delegate - (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat pageWidth = scrollView.frame.size.width; int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1; self.pageControl.currentPage = page; } @end
使用方法如下:
CCImageScrollView *imageScrollView = [[CCImageScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460) pageNum:5 pageEnabled:YES]; imageScrollView.imageArray = [NSArray arrayWithObjects:@"1.jpg", @"2.jpg", @"3.jpg", @"4.jpg", @"5.jpg", nil]; [self.view addSubview:imageScrollView]; [imageScrollView release], imageScrollView = nil;