在开发的过程中,很多地方都可以用到轮播图的,这样让用户体验的感觉更加完美。
下面这个是我封装的一个简单的轮播图,技术难点很少,只要捋顺了,就很简单了。
#import
#define kWidth self.frame.size.width
#define kHeight self.frame.size.height
@interfaceCycleScrollVIew : UIView
//@property (nonatomic, retain) NSArray *imagesArray;
//@property (nonatomic, retain) UIScrollView *scrollView;
/**
*根据出入的图片数组,初始化一个轮播图对象
*
*@param frame轮播图对象的frame
*@param imagesArray 图片数组,包含了要进行轮播的图片
*@param animationDuration 自动滚动的时间间隔,如果给的值为0的时候,不能自动进行轮播
*@return 初始化完成的轮播图对象
*/
- (instancetype)initWithFrame:(CGRect)frame imagesArray:(NSArray *)imagesArray animationDuration:(NSTimeInterval)animationDuration;
@property(nonatomic,retain,readonly) NSArray *imagesArray;
@end
#import"CycleScrollVIew.h"
#import"NSTimer+Addition.h"
#definekWidth self.frame.size.width
#definekHight self.frame.size.hight
@interfaceCycleScrollVIew ()
{
NSInteger _imagesCount;// 记录传入的数组元素的个数
NSTimeInterval _animationDuration;// 定义自动滚动的时间间隔
}
@property(nonatomic,retain) NSArray *imagesArray;// 接受传入的图片数组
@property(nonatomic,retain) UIScrollView *scrollView;
@property(nonatomic,retain) NSTimer *timer;//使用NSTimer让图片自动滚动
@end
@implementationCycleScrollVIew
- (instancetype)initWithFrame:(CGRect)frame imagesArray:(NSArray *)imagesArray animationDuration:(NSTimeInterval)animationDuration
{
self= [superinitWithFrame:frame];
if(self) {
_imagesArray = [imagesArray retain];
_imagesCount = imagesArray.count;
// 初始化UIScrollView对象
[selfcreateScrollView];
// 初始化NSTimer对象,并且让它进行滚动,如果给的时间间隔为0的话,那么不能自动滚动
if(animationDuration !=0) {
_timer = [NSTimer scheduledTimerWithTimeInterval:animationDuration target:selfselector:@selector(autoChangeFrame:) userInfo:nilrepeats:YES];
}
}
returnself;
}
- (void)autoChangeFrame:(NSTimer *)timer
{
CGPoint newOffset = CGPointMake(_scrollView.contentOffset.x + kWidth,0);
[_scrollView setContentOffset:newOffset animated:YES];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
[_timer resumeTimerAfterTimeInterval:_animationDuration];
}
#pragma mark - UIScrollView的初始化
- (void)createScrollView
{
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0, kWidth, kHeight)];
_scrollView.contentSize = CGSizeMake((_imagesCount +2) * kWidth,0);
// 隐藏水平滚动条
_scrollView.showsHorizontalScrollIndicator =NO;
// 设置一页滚动一次
_scrollView.pagingEnabled =YES;
// 设置偏移量
_scrollView.contentOffset = CGPointMake(kWidth,0);
// 设置代理delegate为当前对象
_scrollView.delegate =self;
// 循环设置图片
[selfcreateImageViews];
// 把scrollView添加到self上
[selfaddSubview:_scrollView];
}
#pragma mark - 循环设置UIImageView
- (void)createImageViews
{
for(inti =0; i < _imagesCount +2; i++) {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(kWidth * i,0, kWidth, kHeight)];
if(i ==0) {
imageView.image = [_imagesArray lastObject];
}elseif(i == _imagesCount +1){
imageView.image = [_imagesArray firstObject];
}else{
imageView.image = _imagesArray[i -1];
}
[_scrollView addSubview:imageView];
[imageView release];
}
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
// 首先得到当前的偏移量
CGPoint currentPoint = scrollView.contentOffset;
if(currentPoint.x <= kWidth /2) {
scrollView.contentOffset = CGPointMake(_imagesCount * kWidth+ currentPoint.x,0);
}elseif(currentPoint.x > (_imagesCount +0.5) * kWidth){
scrollView.contentOffset = CGPointMake(currentPoint.x - _imagesCount * kWidth,0);
}
}
- (void)dealloc
{
[_scrollView release];
[_imagesArray release];
[_timer release];
[superdealloc];
}
@end
封装一个计时器
#import
@interfaceNSTimer (Addition)
/**
*让NSTimer暂停
*/
- (void)pauseTimer;
/**
*重新启动
*/
- (void)resumeTimer;
/**
*让NSTimer几秒之后重新启动
*@param interval 间隔时间
*/
- (void)resumeTimerAfterTimeInterval:(NSTimeInterval)interval;
@end
#import"NSTimer+Addition.h"
@implementationNSTimer (Addition)
- (void)pauseTimer
{
if(!self.isValid) {// 如果当前的NSTimer对象不可用,直接返回
return;
}
[selfsetFireDate:[NSDate distantFuture]];
}
- (void)resumeTimer
{
if(!self.isValid) {
return;
}
[selfsetFireDate:[NSDate date]];
}
- (void)resumeTimerAfterTimeInterval:(NSTimeInterval)interval
{
if(!self.isValid) {
return;
}
[selfsetFireDate:[NSDate dateWithTimeIntervalSinceNow:interval]];
}
@end
虽然这个很简单,但是也还是用的到的。接下来我会继续更新一些其他的实用性代码。感谢您的阅览。
如果喜欢就点个爱心吧,谢谢。