您的位置:首页 > 移动开发 > IOS开发

iOS-工具类封装-图片轮播无限循环

2016-11-27 19:23 579 查看

UIScrollView图片轮播无限循环

为了方便别人使用只提供以下接口:图片的frame,图片名,分页控件的currentPageIndicatorTintColor和pageIndicatorTintColor,如下列代码:

- (void)viewDidLoad {
[super viewDidLoad];
STPageView * pageView = [STPageView pageView];
pageView.frame = CGRectMake(50, 70, 270, 150);
pageView.imageNames = @[@"img_00", @"img_01", @"img_02"];
pageView.currentColor = [UIColor greenColor];
pageView.otherColor = [UIColor yellowColor];
[self.view addSubview:pageView];
}


具体实现的步骤(用xib创建)

1.创建STPageView继承于UIView

.h文件里提供如下接口

@interface STPageView : UIView
/** 创建pageView */
+ (instancetype)pageView;
/** 图片名字  */
@property (nonatomic, strong) NSArray * imageNames;
/** 其他圆点的颜色 */
@property (nonatomic, strong) UIColor *otherColor;
/** 当前圆点的颜色 */
@property (nonatomic, strong) UIColor *currentColor;
@end


2.创建xib文件

先创建UIView控件,且继承自己所创建的类



创建UIScrollerView控件和page control控件

在.m文件中拿到UIscrollView和pageControl

注意下列选项





3.方法的实现.m文件中

设置代理,遵守协议



读取xib文件

+ (instancetype)pageView{

return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];
}


setter方法的重写

#pragma mark - setter方法的重写
- (void)setImageNames:(NSArray *)imageNames{

_imageNames = imageNames;
//移除之前所有的imageView
// 让subviews数组中的所有对象都执行removeFromSuperview方法
[self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

//根据图片名创建多个imageView
for (int i = 0; i < imageNames.count; i ++) {
UIImageView * imageView = [[UIImageView alloc] init];
imageView.image = [UIImage imageNamed:imageNames[i]];
[self.scrollView addSubview:imageView];
}
//设置总页数
self.pageControl.numberOfPages = imageNames.count;
}

- (void)setCurrentColor:(UIColor *)currentColor{
_currentColor = currentColor;
self.pageControl.currentPageIndicatorTintColor = currentColor;
}

- (void)setOtherColor:(UIColor *)otherColor{

_otherColor = otherColor;
self.pageControl.pageIndicatorTintColor = otherColor;
}


设置图片的frame

- (void)layoutSubviews{
[super layoutSubviews];
//设置scrollView的frame
self.scrollView.frame = self.bounds;
//获得scrollView的尺寸
CGFloat scrollViewW = self.scrollView.frame.size.width;
CGFloat scrollViewH = self.scrollView.frame.size.height;

//设置pageControl
CGFloat pageControlW = 100;
CGFloat pageControlH = 20;
CGFloat pageControlX = scrollViewW - pageControlW;
CGFloat pageControlY = scrollViewH - pageControlH;
self.pageControl.frame = CGRectMake(pageControlX, pageControlY, pageControlW, pageControlH);

//设置内容大小
self.scrollView.contentSize = CGSizeMake(self.imageNames.count * scrollViewW, 0);

for (int i = 0; i < self.scrollView.subviews.count; i ++) {
UIImageView * imageView = self.scrollView.subviews[i];
imageView.frame = CGRectMake(i * scrollViewW, 0, scrollViewW, scrollViewH);
}
}


实现scrollViewDelegate方法

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
self.pageControl.currentPage = (int)(scrollView.contentOffset.x / scrollView.frame.size.width + 0.5);
}


4.添加定时器(无限循环)

- (void)startTimer{
//创建一个定时器
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}
- (void)stopTimer{
[self.timer invalidate];
self.timer = nil;
}
- (void)nextPage{
NSInteger page = self.pageControl.currentPage + 1;
if (page == self.pageControl.numberOfPages) {
page = 0;
}
CGPoint offset = self.scrollView.contentOffset;
offset.x = page * self.scrollView.frame.size.width;
[self.scrollView setContentOffset:offset animated:YES];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息