头文件 :
#import <UIKit/UIKit.h>
@interface ScreenView : UIView <UIScrollViewDelegate>
- (void)refreshData:(NSMutableArray*)imagesArray;
@end
实现文件 :
#import "ScreenView.h"
@interface ScreenView ()
{
NSMutableArray* _screenArray; //图片数组
NSInteger _totalInt; //图片总数
NSInteger _currentInt; //当前页面
NSMutableArray* _currentArray; //当前图片数组 最多三个
UIScrollView* _scrollView;
}
@property (nonatomic,retain)NSMutableArray* screenArray; //图片数组
@end
@implementation ScreenView
@synthesize screenArray = _screenArray;
#pragma mark --
#pragma mark LifeStyle
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
//初始化uiscrollview
if (!_scrollView) {
_scrollView = [[UIScrollViewalloc]initWithFrame:self.bounds];
}
_scrollView.showsHorizontalScrollIndicator =NO;
_scrollView.showsVerticalScrollIndicator =NO;
_scrollView.pagingEnabled =YES;
_scrollView.delegate =self;
[self addSubview:_scrollView];
_currentInt =0; //首次加载后就进入首张图片 (0 1 2)
//初始化当前页面三张图片的array
if (!_currentArray) {
_currentArray = [[NSMutableArrayalloc]initWithCapacity:3];
}
//初始化总的图片数组
if (!_screenArray) {
_screenArray = [[NSMutableArrayalloc]initWithCapacity:5];
}
}
return self;
}
- (void)dealloc
{
[_scrollView release];
[_currentArray release];
[_screenArray release];
[super dealloc];
}
#pragma mark --
#pragma mark Methods
- (void)refreshData:(NSMutableArray*)imagesArray
{
if (!imagesArray || (imagesArray.count ==0))return;
if (_screenArray &&_screenArray.count >0) {
[_screenArrayremoveAllObjects];
}
self.screenArray = imagesArray;
_totalInt = self.screenArray.count;
[selfrefreshDataForScrollviewDelegate];
}
//当UIScrollViewDelegate被调用时也会调用这个方法,不仅仅是上边那个函数会调用
- (void)refreshDataForScrollviewDelegate
{
if (_scrollView &&_scrollView.subviews.count >0) {
for (UIView *viewin_scrollView.subviews) {
[view removeFromSuperview];
}
}
if (_currentArray &&_currentArray.count >0) {
[_currentArrayremoveAllObjects];
}
if (_totalInt ==1) { //如果只返回包含一个值的数组,就不应该再让它左右滑动了 (这个地方应该也有改进的空间,如果只返回一张图的话可以直接让scrollview停止滚动应该就行,不需要再单独加入这个图片,以后有可能 试试)
UIImageView *imgView = [[UIImageViewalloc]initWithFrame:self.bounds];
imgView.image = [UIImageimageNamed:_screenArray[0]];
_scrollView.contentSize =self.bounds.size;
[_scrollView addSubview:imgView];
[imgView release];
} else { //返回两个及以上个值的数组时均可左右滑动
_scrollView.contentSize =CGSizeMake(self.bounds.size.width *3, self.bounds.size.height);
NSInteger _previousInt = [selfavailablePicInt:_currentInt -1]; //前一张图片
NSInteger _behindInt = [selfavailablePicInt:_currentInt +1]; //后一张图片
[_currentArray addObject:_screenArray[_previousInt]];
[_currentArray addObject:_screenArray[_currentInt]];
[_currentArray addObject:_screenArray[_behindInt]];
for (int i =0; i <3; i++) {
UIImageView* imgView = [[UIImageViewalloc]initWithFrame:CGRectMake(self.bounds.size.width * i, 0, self.bounds.size.width,self.bounds.size.height)];
imgView.image = [UIImageimageNamed:_currentArray[i]];
[_scrollView addSubview:imgView];
[imgView release];
}
[_scrollViewsetContentOffset:CGPointMake(self.bounds.size.width,0)];
}
}
//获取实际的图片值
- (NSInteger)availablePicInt:(NSInteger)curInt
{
if (curInt < 0) {
return _totalInt -1;
}
if (curInt == _totalInt) {
return 0;
}
return curInt;
}
#pragma mark --
#pragma mark UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView.contentOffset.x >=self.bounds.size.width *2) {
_currentInt = [selfavailablePicInt:_currentInt +1];
[selfrefreshDataForScrollviewDelegate];
} else if (scrollView.contentOffset.x <=0 ) {
_currentInt = [selfavailablePicInt:_currentInt -1];
[selfrefreshDataForScrollviewDelegate];
}
}
//看齐冀写的这个地方共用了三个uiscrollviewdelegate代理方法,个人实验后应该用不着那么多 ,只用上边一个即可实现要求
@end