- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self createSubview];
[self setDefaultImage];
}
return self;
}
- (void)createSubview{
[self addScrollView];
[self addImageViews];
[self addPageControl];
[self addLabel];
}
#pragma mark - 加载图片数据
- (void)loadImageData:(NSArray *)imageArr{
_imageArr = [NSMutableArray arrayWithArray:imageArr];
_imageCount = _imageArr.count;
}
#pragma mark - 添加ScrollView控件
- (void)addScrollView{
_scrollView = [[UIScrollView alloc]initWithFrame:[UIScreen mainScreen].bounds];
[self addSubview:_scrollView];
//设置代理
_scrollView.delegate = self;
//设置contentSize
_scrollView.contentSize = CGSizeMake(3 * ScreenWidth, ScreenHeight);
//设置当前显示的位置为中间的图片
[_scrollView setContentOffset:CGPointMake(ScreenWidth, 0) animated:NO];
//设置分页
_scrollView.pagingEnabled = YES;
//去掉水平滚动条
_scrollView.showsHorizontalScrollIndicator = NO;
}
#pragma mark - 添加图片三个控件
- (void)addImageViews{
_leftImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
_leftImageView.contentMode=UIViewContentModeScaleAspectFit;
[_scrollView addSubview:_leftImageView];
_centerImageView=[[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth, 0, ScreenWidth, ScreenHeight)];
_centerImageView.contentMode=UIViewContentModeScaleAspectFit;
[_scrollView addSubview:_centerImageView];
_rightImageVeiw=[[UIImageView alloc]initWithFrame:CGRectMake(2*ScreenWidth, 0, ScreenWidth, ScreenHeight)];
_rightImageVeiw.contentMode=UIViewContentModeScaleAspectFit;
[_scrollView addSubview:_rightImageVeiw];
}
#pragma mark - 设置默认显示图片
- (void)setDefaultImage{
//加载默认图片
_leftImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"xifuer%ld.jpeg",_imageCount - 1]];
_centerImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"xifuer%d.jpeg",0]];
_rightImageVeiw.image = [UIImage imageNamed:[NSString stringWithFormat:@"xifuer%d.jpeg",1]];
_currentImageIndex = 0;
NSString * name=[NSString stringWithFormat:@"xifuer%ld.jpeg",_currentImageIndex];
_label.text = name;
//设置当前页
_pageControl.currentPage = _currentImageIndex;
}
#pragma mark - 添加分页控件
- (void)addPageControl{
_pageControl = [[UIPageControl alloc]init];
//此方法可以根据页数返回UIPageControl合适的大小
CGSize size = [_pageControl sizeForNumberOfPages:_imageCount];
_pageControl.bounds = CGRectMake(0, 0, size.width, size.height);
_pageControl.center = CGPointMake(ScreenWidth/2, ScreenHeight-100);
//设置颜色
_pageControl.pageIndicatorTintColor = [UIColor colorWithRed:193/255.0 green:219/255.0 blue:249/255.0 alpha:1];;
//设置当前页颜色
_pageControl.currentPageIndicatorTintColor=[UIColor colorWithRed:0 green:150/255.0 blue:1 alpha:1];
//设置总页数
_pageControl.numberOfPages=_imageCount;
[self addSubview:_pageControl];
}
#pragma mark - 添加文字描述
- (void)addLabel{
_label=[[UILabel alloc]initWithFrame:CGRectMake(0, ScreenHeight - 180, ScreenWidth,30)];
_label.textAlignment=NSTextAlignmentCenter;
_label.textColor=[UIColor colorWithRed:0 green:150/255.0 blue:1 alpha:1];
[self addSubview:_label];
}
#pragma mark - 滚动停止代理方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
//重新加载图片
[self reloadImage];
//移动到中间
[_scrollView setContentOffset:CGPointMake(ScreenWidth, 0) animated:NO];
//设置分页
_pageControl.currentPage = _currentImageIndex;
//设置描述
NSString * name=[NSString stringWithFormat:@"xifuer%ld.jpeg",_currentImageIndex];
_label.text = name;
}
#pragma mark - 重新加载图片
- (void)reloadImage{
NSInteger leftImageIndex,rightImageIndex;
CGPoint offset = [_scrollView contentOffset];
if (offset.x>ScreenWidth) {
//向右滑动
_currentImageIndex = (_currentImageIndex + 1)%_imageCount;
}else if (offset.x
//控制器里调用
TGSDSCrollView * tgView = [[TGSDSCrollView alloc]initWithFrame:[UIScreen mainScreen].bounds];
NSArray * imageArr1 = @[@"xifuer0.jpeg",@"xifuer1.jpeg",@"xifuer2.jpeg",@"xifuer3.jpeg",@"xifuer4.jpeg"];
[tgView loadImageData:imageArr1];
[self.view addSubview:tgView];